1.打印inet_ntoa()获取的ip,出现段错误
解决方法,添加头文件
#include <arpa/inet.h>
2.bind: Address already in use
服务器和已经建立连接的情况下,服务器断开:
服务器处于FIN_WAIT2状态,重器服务器会报错
bind: Address already in use
root@qiujun:/home/qiujun/AllwinnerT5/TCP# ./server
my ip = 192.168.1.55;port = 6666
wait client
new client ip=192.168.1.55 port =49896 ok
^C
root@qiujun:/home/qiujun/AllwinnerT5/TCP# ./server
bind: Address already in use
root@qiujun:/home/qiujun/AllwinnerT5/TCP# netstat | grep 6666
tcp 0 0 192.168.1.55:6666 192.168.1.55:49896 FIN_WAIT2
tcp 1 0 192.168.1.55:49896 192.168.1.55:6666 CLOSE_WAIT
解决方法,设置socket选项SO_REUSEADDR:
int flag = 1;
if (setsockopt(serfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) < 0) {
perror("setsockopt");
exit(1);
}
其中serfd为socket获取到的套接字描述符