文章目录
问题一 套接字(已解决)
有关套接字,服务器端套接字创建的时候用的是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.
#0 0x7f3d94e948ef (/usr/local/lib/libmysqlclient.so.21+0x3a8ef)
#1 0x4015ca in main /home/erfenjiao/vs/c/ChatRoom/mysql.c:56
#2 0x7f3d94c9709a in __libc_start_main ../csu/libc-start.c:308
#3 0x4011f9 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'
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 '' at 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))
{
recv_pack.type = 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.
#0 0x7f11cc8c3551 (/lib/x86_64-linux-gnu/libasan.so.5+0x103551)
#1 0x7f11cc81388c (/lib/x86_64-linux-gnu/libasan.so.5+0x5388c)
#2 0x7f11cc81476e in vsprintf (/lib/x86_64-linux-gnu/libasan.so.5+0x5476e)
#3 0x7f11cc8149f6 in __interceptor_sprintf (/lib/x86_64-linux-gnu/libasan.so.5+0x549f6)
#4 0x40ca07 in add_fir /home/erfenjiao/vs/c/ChatRoom/my_friends.c:76
#5 0x403fc6 in deal /home/erfenjiao/vs/c/ChatRoom/service.c:286
#6 0x7f11cbea1fa2 in start_thread /build/glibc-2.28.11/nptl/pthread_create.c:486
#7 0x7f11cbdd260e 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:
#0 0x7f11cc810db0 in __interceptor_pthread_create (/lib/x86_64-linux-gnu/libasan.so.5+0x50db0)
#1 0x40338d in main /home/erfenjiao/vs/c/ChatRoom/service.c:157
#2 0x7f11cbcfd09a 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
#0 0x40e10e in look_list /home/erfenjiao/vs/c/ChatRoom/my_friends.c:242
#1 0x4044f7 in deal /home/erfenjiao/vs/c/ChatRoom/service.c:344
#2 0x7fe58d650fa2 in start_thread /build/glibc-2.28.11/nptl/pthread_create.c:486
#3 0x7fe58d58160e in clone (/lib/x86_64-linux-gnu/libc.so.6+0xf960e)
0x6270000037b4 is located 0 bytes to the right of 14004-byte region [0x627000000100,0x6270000037b4)
allocated by thread T2 here:
#0 0x7fe58e058330 in __interceptor_malloc (/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
#1 0x40dd41 in look_list /home/erfenjiao/vs/c/ChatRoom/my_friends.c:219
#2 0x4044f7 in deal /home/erfenjiao/vs/c/ChatRoom/service.c:344
#3 0x7fe58d650fa2 in start_thread /build/glibc-2.28.11/nptl/pthread_create.c:486
Thread T2 created by T0 here:
#0 0x7fe58dfbfdb0 in __interceptor_pthread_create (/lib/x86_64-linux-gnu/libasan.so.5+0x50db0)
#1 0x40338d in main /home/erfenjiao/vs/c/ChatRoom/service.c:157
#2 0x7fe58d4ac09a 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: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c4e7fff86b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c4e7fff86c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c4e7fff86d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c4e7fff86e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c4e7fff86f0: 00 00 00 00 00 00[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: 01 02 03 04 05 06 07
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])的值,发现会无限循环
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
atoi(row[1]) = 1155523957
。。。。。。
无限循环是因为永远只读取了一行,数据库读取需要不断地执行读取语句,但是放到循环内部就无法读取下一个,变成这样,如下,就可以实现不断向下读取的问题
while (row= mysql_fetch_row(result))
问题十四 无法发送文件(待解决)
客户端报错如下
AddressSanitizer:DEADLYSIGNAL
=================================================================
==21484==ERROR: AddressSanitizer: SEGV on unknown address 0x0000bebebebe (pc 0x7fd38788570c bp 0x7fd3844feb10 sp 0x7fd3844fe420 T1)
==21484==The signal is caused by a WRITE memory access.
#0 0x7fd38788570b in _IO_vfscanf_internal /build/glibc-2.28.11/stdio-common/vfscanf.c:1895
#1 0x7fd387890c1b in __isoc99_vscanf /build/glibc-2.28.11/stdio-common/isoc99_vscanf.c:30
#2 0x7fd3883ba00c in __interceptor___isoc99_vscanf (/lib/x86_64-linux-gnu/libasan.so.5+0xb000c)
#3 0x7fd3883ba10e in __interceptor___isoc99_scanf (/lib/x86_64-linux-gnu/libasan.so.5+0xb010e)
#4 0x405c8f in sendMsg /home/erfenjiao/vs/c/ChatRoom/client.c:863
#5 0x7fd3879ebfa2 in start_thread /build/glibc-2.28.11/nptl/pthread_create.c:486
#6 0x7fd38791c60e in clone (/lib/x86_64-linux-gnu/libc.so.6+0xf960e)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /build/glibc-2.28.11/stdio-common/vfscanf.c:1895 in _IO_vfscanf_internal
Thread T1 created by T0 here:
#0 0x7fd38835adb0 in __interceptor_pthread_create (/lib/x86_64-linux-gnu/libasan.so.5+0x50db0)
#1 0x408243 in main /home/erfenjiao/vs/c/ChatRoom/client.c:1416
#2 0x7fd38784709a in __libc_start_main ../csu/libc-start.c:308
==21484==ABORTING
总结
写完聊天室再看出现的问题,很多问题历历在目,很多卡住了我我好久,但是只要一点一点排查错误,总能将其修正。
ps
还有文件发送与传输问题没有处理,写完再找错。