写聊天室的过程中出现的问题
⽂章⽬录
问题⼀套接字(已解决)
有关套接字,服务器端套接字创建的时候⽤的是fd,创建线程时换成了sock_fd,运⾏服务器端没有出问题,客户端⼀运⾏就直接退出,后来⽤了gcc -I/usr/local/mysql/include/mysql main_cli.c -L/usr/local/mysql/lib -lmysqlclient -ldl -lpthread -o main_cli -g -fsanitize=address这条命令后⾯的错误检查功能,发现问题是没有套接字,把fd换掉,可以运⾏。
问题⼆不能开多个客户端(已解决)
服务器端开启,再开启客户端,发现只能开⼀个,套接字conn_fd = 0,再开第⼆个,创建的还是套接字conn_fd = 0。
猜想:应该是套接字那⾥出了问题,先单独摘出来错误。
1.报错内容如下:
连接成功!套接字conn_fd =0
连接成功!套接字conn_fd =0
epoll_ctl: File exists
line :126
line:126⾏为:
epoll_ctl(kdpfd , EPOLL_CTL_ADD , conn_fd ,&ev);
2.接下来,检查epoll的创建过程,看看有⽆问题
if(events[i].data.fd == sock_fd)//⽤于监听的套接字
{
if((conn_fd =accept(sock_fd ,(struct sockaddr *)&cli ,&socklen ))<0)
/实际上是这⼀⾏有问题,conn_fd没有赋值成功/
{
my_err("accept",__LINE__);
}
printf("连接成功!套接字conn_fd = %d\n",conn_fd);
acceptcont++;//连接数++
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = conn_fd;
if(epoll_ctl(kdpfd , EPOLL_CTL_ADD , conn_fd ,&ev)<0)
{
my_err("epoll_ctl",__LINE__);
}
这⼀⾏报错//
curfds++;
continue;
}
加上⼀个括号问题解决。
问题三数据库创建(已解决)
将数据库单独放出来检查,出现如下问题
AddressSanitizer:DEADLYSIGNAL
=================================================================
==14116==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000050(pc 0x7f3d94e948f0 bp 0x7ffc3fee8fe0 sp 0x7ffc3fee89c8 T0) ==14116==The signal is caused by a READ memory access.
==14116==Hint: address points to the zero page.
#00x7f3d94e948ef(/usr/local/lib/libmysqlclient.so.21+0x3a8ef)
#10x4015ca in main /home/erfenjiao/vs/c/ChatRoom/mysql.c:56
#20x7f3d94c9709a in __libc_start_main ../csu/libc-start.c:308
#30x4011f9 in _start(/home/erfenjiao/vs/c/ChatRoom/mysql+0x4011f9)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer:SEGV(/usr/local/lib/libmysqlclient.so.21+0x3a8ef)
==14116==ABORTING
1.搜索得,可能是内存泄漏的问题。
2.继续检查得出,是因为数据库以及表需要⾃⼰创建。
3.创建表,此代码只完成查功能,需⼿动填写数据
4.⼿动填写时,出现问题
MariaDB [happy]>  insert into tbb_user(username , userid)values(’zhangsan’,1);
ERROR 1054(42S22): Unknown column '’zhangsan’' in 'field list'
MariaDB [happy]>  insert into tbb_user(username , userid)values(zhangsan,1);
ERROR 1054(42S22): Unknown column 'zhangsan' in 'field list'
5.解决问题,是单引号和点的问题
6.成功填⼊信息
7.运⾏结果⼀致,完成
问题五 deal函数中的遗漏(已解决)
在服务器的deal函数那⾥,还有数据库的函数mysql_accept(),已补充。
此问题是由检查问题⼆出来的,本来以为填了数据库函数就能解决问题⼆,但显然是我太天真
问题六注册卡住不动(已解决)
+------------------+
|1. login        |
|2. regisister  |
|3. admin        |
|4. exit        |
+------------------+
进⼊到这个界⾯,⽆论选哪⼀个,都会在随着提⽰输⼊完账号和密码后,卡住,⽆法进⾏到下⼀步。
这个问题随着第⼆个问题的解决有了新的进展,注册时跟着指引完成,服务器端反⽽退出了.
解决掉⽂件⽆法打开以及数据库sql语句错误后,注册这⼀步成功完成。
问题七注册部分代码⽆法继续运⾏(已解决)
printf("注册成功!!\n");
printf("您的账号为:%d\n", send_pack->data.send_account);
这两⾏没有打印
问题⼋ epoll_wait(已解决)
nfds =epoll_wait(kdpfd , events , size ,-1);
printf("%d\n",nfds);
运⾏结果
109、、、、、、、、、、、、、
nfds =1 curfds =1
连接成功!套接字conn_fd =6
nfds =1 curfds =2
645、、、、、、出错、、、、、、、
line :648
fopen: No such file or directory
nfds =-1 curfds =2
line :122
epoll_wait: Interrupted system call
猜想:是不是因为⽂件⽆法打开,数据没有办法写⼊,然后出错,在下⼀轮循环中才产⽣了epoll_wait 报错?
在本⽬录创建⼀个⽂件后,产⽣了⼀个新的sql语句问题,如下:
nfds =1 curfds =1
连接成功!套接字conn_fd =6
nfds =1 curfds =2
Query failed(You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' a t line 1)
nfds =-1 curfds =2
120、、、、、、出错、、、、、、、
line :121
epoll_wait: Interrupted system call
检查得,数据库sql语句少了⼀个括号,补充后,注册账号成功,检查数据库的user_data表,发现填充成功。
问题九 $$sad(已解决)
输⼊账号和密码后,会出现$$sad 这个符号,然后就⽆法往下进⾏,错误信息如下:
连接成功!套接字conn_fd =7
nfds =1 curfds =3
$$sad
到了sad的位置
if(!mysql_fetch_row(result))
{
pe = ACCOUNT_ERROR;
memset(recv_pack.data.write_buff,0,sizeof(recv_pack.data.write_buff));
printf("$$sad\n");
strcpy(recv_pack.data.write_buff,"passwd error");
if((send(events[i].data.fd,&recv_pack,sizeof(PACK),0))<0)
{
my_err("send",__LINE__);
}
pthread_mutex_unlock(&mutex);
continue;
}
问题⑩账号与密码问题(已解决)
number = 100 取消了之后也没啥⽤哎,他界⾯上⽽不是数据库⾥显⽰的账号也没有办法登上去。
为什么我的账号他登不上去…T__T
1.灵魂拷问:account到底算账号,还是算昵称?
答:是账号,那我数据库⾥那个咋存的是昵称?
2.既然account是账号,那么user_number就不是账号了,等等,是不是名称就错了,应该是user_name?
3.那user_number确实是分配账号了呀?
答:user_number确实是在分配数字,但是数据表⾥⾯不⼀定需要的是这个名字,顶着account这个名字。
4.重新建⼀张表,如下:
+------------+-----------+--------+------------+--------+
| account    | user_name | passwd | user_state | socket |
+------------+-----------+--------+------------+--------+
|1155523951|⼩王|1234|0|6|
+------------+-----------+--------+------------+--------+
问题⼗⼀添加好友建表(已解决)
添加好友步骤,在没有添加好友之前,输⼊账号,却总是报错“已经有此好友或者此好友不存在”。
检查得:总是写不到数据库中,在数据库相关的部分查,发现是数据库的名字写错了,少了⼀个‘s’
问题⼗⼆%d 和 %s ⼀定不能搞混了!!(已解决)
在服务器端的add_friend()函数中,出现了⼀个问题,导致这个函数⼀直不能运⾏,终端给出的报错如下:
AddressSanitizer:DEADLYSIGNAL
=================================================================
==16840==ERROR: AddressSanitizer: SEGV on unknown address 0x000044dfe575(pc 0x7f11cc8c3552 bp 0x7f11c89fd500 sp 0x7f11c89fcc38 T2)
==16840==The signal is caused by a READ memory access.
#00x7f11cc8c3551(/lib/x86_64-linux-gnu/libasan.so.5+0x103551)
#10x7f11cc81388c(/lib/x86_64-linux-gnu/libasan.so.5+0x5388c)
#20x7f11cc81476e in vsprintf(/lib/x86_64-linux-gnu/libasan.so.5+0x5476e)
#30x7f11cc8149f6 in __interceptor_sprintf(/lib/x86_64-linux-gnu/libasan.so.5+0x549f6)
#40x40ca07 in add_fir /home/erfenjiao/vs/c/ChatRoom/my_friends.c:76
#50x403fc6 in deal /home/erfenjiao/vs/c/ChatRoom/service.c:286
#60x7f11cbea1fa2 in start_thread /build/glibc-2.28.11/nptl/pthread_create.c:486
#70x7f11cbdd260e in clone(/lib/x86_64-linux-gnu/libc.so.6+0xf960e)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer:SEGV(/lib/x86_64-linux-gnu/libasan.so.5+0x103551)
Thread T2 created by T0 here:
#00x7f11cc810db0 in __interceptor_pthread_create(/lib/x86_64-linux-gnu/libasan.so.5+0x50db0)
#10x40338d in main /home/erfenjiao/vs/c/ChatRoom/service.c:157
#20x7f11cbcfd09a in __libc_start_main ../csu/libc-start.c:308
==16840==ABORTING
很长⼀堆,⼤意应该是地址的问题,
1.⼀开始以为是链表的问题,,因为链表地址最容易出错,但是检查过后发现链表正常,那个创建就是要在链表为空的时候创建,if条件没有问题.
2.以为是数据库函数
result  =mysql_store_result(&mysql);
row1    =mysql_fetch_row(result);
这两⾏导致的问题,直接注释掉,发现代码还是不能运⾏。
3.偶然间看到,%s 和 %d 的问题,⽴马交换位置,再次编译运⾏,成功。
4.这个错误告诉我,⼀点点马虎都不⾏,不出⼤问题,那么就是⼩问题,对于细节⼀定要好好排查!
问题⼗三 LOOK_LIST(已解决)
好友列表⽆法显⽰出来,终端报错如下
=================================================================
==18955==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6270000037b4 at pc 0x00000040e10f bp 0x7fe58a1fde90 sp 0x7fe58a1fde88 WRITE of size 4 at 0x6270000037b4 thread T2
#00x40e10e in look_list /home/erfenjiao/vs/c/ChatRoom/my_friends.c:242
#10x4044f7 in deal /home/erfenjiao/vs/c/ChatRoom/service.c:344
#20x7fe58d650fa2 in start_thread /build/glibc-2.28.11/nptl/pthread_create.c:486
#30x7fe58d58160e in clone(/lib/x86_64-linux-gnu/libc.so.6+0xf960e)socket编程聊天室基本流程
0x6270000037b4 is located 0 bytes to the right of 14004-byte region [0x627000000100,0x6270000037b4)
allocated by thread T2 here:
#00x7fe58e058330 in __interceptor_malloc(/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
#10x40dd41 in look_list /home/erfenjiao/vs/c/ChatRoom/my_friends.c:219
#20x4044f7 in deal /home/erfenjiao/vs/c/ChatRoom/service.c:344
#30x7fe58d650fa2 in start_thread /build/glibc-2.28.11/nptl/pthread_create.c:486
Thread T2 created by T0 here:
#00x7fe58dfbfdb0 in __interceptor_pthread_create(/lib/x86_64-linux-gnu/libasan.so.5+0x50db0)
#10x40338d in main /home/erfenjiao/vs/c/ChatRoom/service.c:157
#20x7fe58d4ac09a in __libc_start_main ../csu/libc-start.c:308
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/erfenjiao/vs/c/ChatRoom/my_friends.c:242 in look_list
Shadow bytes around the buggy address:
0x0c4e7fff86a0:00000000000000000000000000000000
0x0c4e7fff86b0:00000000000000000000000000000000
0x0c4e7fff86c0:00000000000000000000000000000000
0x0c4e7fff86d0:00000000000000000000000000000000
0x0c4e7fff86e0:00000000000000000000000000000000
=>0x0c4e7fff86f0:000000000000[04]fa fa fa fa fa fa fa fa fa
0x0c4e7fff8700: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c4e7fff8710: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c4e7fff8720: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c4e7fff8730: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c4e7fff8740: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend(one shadow byte represents 8 application bytes):
Addressable:00
Partially addressable:01020304050607
Heap left redzone:      fa
Freed heap region:      fd
Stack left redzone:      f1
Stack mid redzone:      f2
Stack right redzone:    f3
Stack after return:      f5
Stack use after scope:  f8
Global redzone:          f9
Global init order:      f6
Poisoned by user:        f7
Container overflow:      fc
Array cookie:            ac
Intra object redzone:    bb
ASan internal:          fe
Left alloca redzone:    ca
Right alloca redzone:    cb
==18955==ABORTING
使⽤printf打印atoi(row[1])的值,发现会⽆限循环

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。