C/C++ socket send causes program to exit - Networking

This is a discussion on C/C++ socket send causes program to exit - Networking ; I have two programs, a "client" and a "server". The server just sends some data to the client. When I kill the client by pressing Ctrl+C, the connection is not always properly terminated on the server's side. When the server ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: C/C++ socket send causes program to exit

  1. C/C++ socket send causes program to exit

    I have two programs, a "client" and a "server". The server just sends
    some data to the client. When I kill the client by pressing Ctrl+C, the
    connection is not always properly terminated on the server's side.

    When the server is in the middle of a send() or write() call, the
    process exists with error code 141. No Segmentation faults, no
    exceptions thrown, not even a return value from the send/write call. I
    expect send or write to return -1 (see manpages) but it does not return,
    the whole process just exits.

    Any ideas what could be causing this? Can this be caused by a bug in the
    OS (Ubuntu 7.04)?


    Program code:
    printf("1"); fflush(stdout);
    //int retval = send(s, pData, uLength, 0);
    int retval = write(s, pData, uLength);
    printf("2"); fflush(stdout);

    Typical output at the server side:
    121212121212 (kill client)(server exits correctly with code 0)
    12121212121 (kill client when server calls send)(server stops executing
    code, i.e. printf etc and process exits with code 141)


    Bjorn Nuyttens

  2. Re: C/C++ socket send causes program to exit

    On Jul 16, 9:12 am, Bjorn Nuyttens wrote:

    > When the server is in the middle of a send() or write() call, the
    > process exists with error code 141. No Segmentation faults, no
    > exceptions thrown, not even a return value from the send/write call. I
    > expect send or write to return -1 (see manpages) but it does not return,
    > the whole process just exits.
    >
    > Any ideas what could be causing this? Can this be caused by a bug in the
    > OS (Ubuntu 7.04)?


    No, the OS is doing the right thing. Since your program can no longer
    write to the place it was trying to write, the OS assumes that it can
    no longer make useful progress. You didn't tell the OS anything else,
    so you get the default behavior.

    Google for 'SIGPIPE'.

    DS


  3. Re: C/C++ socket send causes program to exit

    David Schwartz wrote:
    > On Jul 16, 9:12 am, Bjorn Nuyttens wrote:
    >
    >> When the server is in the middle of a send() or write() call, the
    >> process exists with error code 141. No Segmentation faults, no
    >> exceptions thrown, not even a return value from the send/write call. I
    >> expect send or write to return -1 (see manpages) but it does not return,
    >> the whole process just exits.
    >>
    >> Any ideas what could be causing this? Can this be caused by a bug in the
    >> OS (Ubuntu 7.04)?

    >
    > No, the OS is doing the right thing. Since your program can no longer
    > write to the place it was trying to write, the OS assumes that it can
    > no longer make useful progress. You didn't tell the OS anything else,
    > so you get the default behavior.
    >
    > Google for 'SIGPIPE'.
    >
    > DS
    >


    For those interested: you could either ignore or handle the SIGPIPE
    signal or specifiy the MSG_NOSIGNAL option with the send command. For
    the time being, I chose the latter.

    printf("1"); fflush(stdout);
    int retval = send(s, pData, uLength, MSG_NOSIGNAL);
    printf("2"); fflush(stdout);


    Thanks to David for pointing me in the right direction!

+ Reply to Thread