socket connection getting closed abruptly with code 141

后端 未结 2 1732
失恋的感觉
失恋的感觉 2021-01-14 04:53

What im trying to do is connect to a remote server , read contents from a file on the local machine and send it over to the server. Then capture the server response and sav

相关标签:
2条回答
  • 2021-01-14 05:16

    sizeof(&nc_args->destaddr) is the wrong thing to pass to connect. It wants the size of the address, not the size of a pointer to the address.

    And this loop:

    for (i=0 ; i<sizeof(buffer); i++)
    {
        printf("\n\t\t %s", buffer);
    }
    

    is baffling. buffer is a pointer, as we can see from when it was assigned a vlue returned by malloc. So its size is going to be 4 or 8 bytes on 32-bit and 64-bit architectures respectively; not related to the size of the malloc'ed object it points to. The loop runs 4 or 8 times, and prints... the same thing each time. Why?

    0 讨论(0)
  • 2021-01-14 05:19

    On Linux, and probably other Unixes, the return code encodes a signal that the process received. Here it is 141 - 128 so 13 which corresponds to SIGPIPE.

    If you don't want that signal to be raised because you capture the error return of send, anyhow, on Linux you can use MSG_NOSIGNAL in the flags argument to send to inhibit that signal. On other platforms you might have to program more complicated signal handlers to deal with that situation.

    0 讨论(0)
提交回复
热议问题