4

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

harnen
  • 403
  • 4
  • 12
  • 1
    are you using signalling? have you checked it with `gdb`? – Karoly Horvath Aug 04 '11 at 09:22
  • 1
    Valgrind? It's not a memory leak, run it under `gdb`. You might be exiting due to `SIG_PIPE`. – Steve-o Aug 04 '11 at 09:23
  • 2
    possible duplicate of [How to prevent SIGPIPE or prevent the server from ending?](http://stackoverflow.com/questions/6821469/how-to-prevent-sigpipe-or-prevent-the-server-from-ending) – Steve-o Aug 04 '11 at 09:25

3 Answers3

6

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

How to prevent SIGPIPEs (or handle them properly)

Community
  • 1
  • 1
otto
  • 1,138
  • 6
  • 14
1

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

Mayur Birari
  • 5,837
  • 8
  • 34
  • 61
0

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.

gw0
  • 1,533
  • 2
  • 12
  • 13
  • I did. It's a bit difficult to do anything(check errno) when send doesn't return and application exits... – harnen Aug 04 '11 at 09:24