简体   繁体   English

Linux TCP套接字崩溃

[英]Linux TCP socket crash

I write network application which communicates via Linux TCP socket. 我编写通过Linux TCP套接字进行通信的网络应用程序。 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. Socket已准备好并已连接,并且在第二个对等体出现故障之前已执行多次。 I've expected this code returning -1 value, but it produces on output only "out_tcp 11" then application exits. 我希望这段代码返回-1值,但它只在输出上产生“out_tcp 11”然后应用程序退出。 No error message, no returned value from send. 没有错误消息,没有发送的返回值。 I run it under Valgrind, it says application exits normally - no error/warning message. 我在Valgrind下运行它,它说应用程序正常退出 - 没有错误/警告消息。

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 在此先感谢哈嫩

Looks like your application is ignoring SIGPIPE. 看起来您的应用程序忽略了SIGPIPE。 Please see this thread for further information: 请参阅此主题以获取更多信息:

How to prevent SIGPIPEs (or handle them properly) 如何防止SIGPIPE(或正确处理它们)

SOLVED: USE MSG_EOR , MSG_NOSIGNALflag in send function as below 已解决:在发送功能中使用MSG_EORMSG_NOSIGNALflag ,如下所示

if(send(out_tcp, &packet, sizeof(packet), **MSG_EOR|MSG_NOSIGNAL**) == -1)

Hope it helps 希望能帮助到你

Have you tried to RTFM (read the fine manual) about error conditions? 您是否尝试过有关错误情况的RTFM(阅读精细手册)? Do you catch or ignore any signals? 你抓住或忽略任何信号吗? What about errno global variable? 那么errno全局变量呢?

man send

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. TCP也是一种流协议,因此如果您不需要任何特殊标志,建议使用常见的流式访问命令,如read(),write()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM