简体   繁体   中英

Linux TCP socket crash

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

Looks like your application is ignoring SIGPIPE. Please see this thread for further information:

How to prevent SIGPIPEs (or handle them properly)

SOLVED: USE MSG_EOR , MSG_NOSIGNALflag in send function as below

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? Do you catch or ignore any signals? What about errno global variable?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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