Linux TCP 套接字崩溃
我编写通过 Linux TCP 套接字进行通信的网络应用程序。最近我注意到发送系统调用使我的应用程序崩溃。当两个对等点都启动时它工作正常(我现在正在测试崩溃恢复)。但是,当一个对等点宕机时,执行这段代码就会崩溃。
fprintf(stderr, "out_tcp %d\n", out_tcp);
if(send(out_tcp, &packet, sizeof(packet), 0) == -1)
fprintf(stderr, "send TCP error");
fprintf(stderr, "after send");
套接字已经准备好并连接,并在第二个对等点关闭之前执行了几次。我预计此代码返回 -1 值,但它仅在输出上生成“out_tcp 11”,然后应用程序退出。没有错误消息,发送没有返回值。我在 Valgrind 下运行它,它说应用程序正常退出 - 没有错误/警告消息。
有谁知道如何调试它?使用工具?由于我没有得到任何信息,我对此很困惑。
提前致谢 哈嫩
I write network application which communicates via Linux TCP socket. Recently I've noticed send system call crashing my application. It works fine when both peers are up (I'm testing crash recovery now). But when one peer is down second crashes executing this piece of code.
fprintf(stderr, "out_tcp %d\n", out_tcp);
if(send(out_tcp, &packet, sizeof(packet), 0) == -1)
fprintf(stderr, "send TCP error");
fprintf(stderr, "after send");
Socket is already prepared and connected and was executed several times before second peer went down. I've expected this code returning -1 value, but it produces on output only "out_tcp 11" then application exits. No error message, no returned value from send. I run it under Valgrind, it says application exits normally - no error/warning message.
Does anyone has any idea how to debug it? Tools to use? I'm pretty stuck with this as I get no informations.
Thanks in advance
Harnen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您的应用程序忽略了 SIGPIPE。请参阅此线程以获取更多信息:
如何防止 SIGPIPE(或处理它们)正确)
Looks like your application is ignoring SIGPIPE. Please see this thread for further information:
How to prevent SIGPIPEs (or handle them properly)
已解决:
在发送函数中使用
MSG_EOR
,MSG_NOSIGNALflag
如下希望有帮助
SOLVED:
USE
MSG_EOR
,MSG_NOSIGNALflag
in send function as belowHope it helps
您是否尝试过 RTFM(阅读详细手册)了解错误情况?您是否捕捉或忽略任何信号?那么 errno 全局变量呢?
而且 TCP 是一种流协议,因此如果不需要任何特殊标志,建议使用常用的流访问命令,如 read()、write()。
Have you tried to RTFM (read the fine manual) about error conditions? Do you catch or ignore any signals? What about errno global variable?
And also TCP is a streaming protocol, therefore it is recommended to use usual streaming access commands like read(), write() if you do not need any special flags.