我正在运行服务器和客户端.我在我的电脑上测试我的程序.
这是服务器中向客户端发送数据的函数:
int sendToClient(int fd, string msg) {
cout << "sending to client " << fd << " " << msg <<endl;
int len = msg.size()+1;
cout << "10\n";
/* send msg size */
if (send(fd,&len,sizeof(int),0)==-1) {
cout << "error sendToClient\n";
return -1;
}
cout << "11\n";
/* send msg */
int nbytes = send(fd,msg.c_str(),len,0); //CRASHES HERE
cout << "15\n";
return nbytes;
}
Run Code Online (Sandbox Code Playgroud)
当客户端退出时,它发送到服务器"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);
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,如果我跳过这段代码,意味着只需离开close(_sockfd)通知服务器一切正常 - 服务器不会崩溃.
编辑2:这是strace.out的结束:
5211 recv(5, "BYE\0", 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, "BYEBYE\n", 7) = 7
5211 write(1, "response = ALALA!!!\n", 20) = 20
5211 write(1, "sending to client 5 ALALA!!!\n", 29) = 29
5211 write(1, "10\n", 3) = 3
5211 send(5, "\t\0\0\0", 4, 0) = 4
5211 write(1, "11\n", 3) = 3
5211 send(5, "ALALA!!!\0", 9, 0) = -1 EPIPE (Broken pipe)
5211 --- SIGPIPE (Broken pipe) @ 0 (0) ---
5211 +++ killed by SIGPIPE +++
Run Code Online (Sandbox Code Playgroud)
破管可以杀死我的程序?? 为什么不通过send()返回-1?
eti*_*nne 12
您可能希望在标志中指定MSG_NOSIGNAL:
int nbytes = send(fd,msg.c_str(), msg.size(), MSG_NOSIGNAL);
Run Code Online (Sandbox Code Playgroud)
您之所以收到 SIGPIPE,是因为 Unix 中的一项“功能”在尝试在远程对等方已关闭的套接字上发送时会引发 SIGPIPE。由于您不处理信号,因此会调用默认的信号处理程序,它会中止/崩溃您的程序。
要获得您想要的行为(即使 send() 返回错误,而不是发出信号),请将其添加到程序的启动例程中(例如 main() 的顶部):
#include <signal.h>
int main(int argc, char ** argv)
{
[...]
signal(SIGPIPE, SIG_IGN);
Run Code Online (Sandbox Code Playgroud)