c – send()崩溃了我的程序
发布时间:2020-12-16 10:45:51 所属栏目:百科 来源:网络整理
导读:我正在运行服务器和客户端.我在我的电脑上测试我的程序. 这是服务器中向客户端发送数据的函数: int sendToClient(int fd,string msg) { cout "sending to client " fd " " msg endl; int len = msg.size()+1; cout "10n"; /* send msg size */ if (send(fd
我正在运行服务器和客户端.我在我的电脑上测试我的程序.
这是服务器中向客户端发送数据的函数: int sendToClient(int fd,string msg) { cout << "sending to client " << fd << " " << msg <<endl; int len = msg.size()+1; cout << "10n"; /* send msg size */ if (send(fd,&len,sizeof(int),0)==-1) { cout << "error sendToClientn"; return -1; } cout << "11n"; /* send msg */ int nbytes = send(fd,msg.c_str(),len,0); //CRASHES HERE cout << "15n"; return nbytes; } 当客户端退出时,它发送到服务器“BYE”,服务器正在使用上述功能回复它.我将客户端连接到服务器(在一台计算机上完成,2个终端),当客户端退出服务器崩溃时 – 它从不打印15. 谢谢. 编辑:这是我关闭客户端的方式: void closeClient(int notifyServer = 0) { /** notify server before closing */ if (notifyServer) { int len = SERVER_PROTOCOL[bye].size()+1; char* buf = new char[len]; strcpy(buf,SERVER_PROTOCOL[bye].c_str()); //c_str - NEED TO FREE???? sendToServer(buf,len); delete[] buf; } close(_sockfd); } 顺便说一下,如果我跳过这段代码,意味着离开关闭(_sockfd)而不通知服务器一切正常 – 服务器不会崩溃. 编辑2:这是strace.out的结束: 5211 recv(5,"BYE ",4,0) = 4 5211 write(1,"received from client 5 n",24) = 24 5211 write(1,"command: BYE msg: n",19) = 19 5211 write(1,"BYEBYEn",7) = 7 5211 write(1,"response = ALALA!!!n",20) = 20 5211 write(1,"sending to client 5 ALALA!!!n",29) = 29 5211 write(1,"10n",3) = 3 5211 send(5,"t ",0) = 4 5211 write(1,"11n","ALALA!!! ",9,0) = -1 EPIPE (Broken pipe) 5211 --- SIGPIPE (Broken pipe) @ 0 (0) --- 5211 +++ killed by SIGPIPE +++ 破管可以杀死我的程序??为什么不通过send()返回-1? 解决方法
可能客户端在服务器完成发送之前退出,从而打破了它们之间的套接字.从而使发送崩溃.
link
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |