套接字send()卡在C中
我正在尝试连接两台机器,例如机器 A 和 B。我正在尝试将 TCP 消息从 A 发送到 B(一种方式)。在正常情况下,这工作得很好。当通信顺利时,如果 B 中的套接字关闭,则 A 中的 send() 将永远卡住。它将进程置于僵尸状态。我的机器 A 中的套接字处于阻塞模式。下面是永远卡住的代码。
if (send (txSock,&txSockbuf,sizeof(sockstruct),0) == -1) {
printf ("Error in sending the socket Data\n");
}
else {
printf ("The SENT String is %s \n",sock_buf);
}
如何确定另一侧插座是否关闭?如果目标套接字关闭,send 返回什么?会选择有帮助。
I'm trying to connect two machines, say machine A and B. I'm trying to send TCP message from A to B (One way). In normal scenario this works fine. When the communication is smooth, if the socket in B is closed, send() from A is stuck forever. And it puts process into Zombie state. I have socket in blocked mode in machine A. Below is the code that stuck forever.
if (send (txSock,&txSockbuf,sizeof(sockstruct),0) == -1) {
printf ("Error in sending the socket Data\n");
}
else {
printf ("The SENT String is %s \n",sock_buf);
}
How do I find if the other side socket is closed?? What does send return if the destination socket is closed?? Would select be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
处于“僵尸”状态的进程意味着它已经退出,但其父进程尚未读取其返回码。可能发生的情况是,您的进程正在接收 SIGPIPE 信号(这是当您写入关闭的套接字时默认收到的信号),您的程序已经终止,但僵尸状态尚未解决。
此相关问题提供了有关 SIGPIPE 及其处理方法的更多信息:SIGPIPE,损坏的管道
A process in the "zombie" state means that it has already exited, but its parent has not yet read its return code. What's probably happening is that your process is receiving a SIGPIPE signal (this is what you'll get by default when you write to a closed socket), your program has already terminated, but the zombie state hasn't yet been resolved.
This related question gives more information about SIGPIPE and how to handle it: SIGPIPE, Broken pipe