CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2018
    Posts
    158

    return value of recv when process is killed

    In network where process C is client and another one S is server,
    C is waiting network message or user input by select function, idem S.

    If I kill process A, RECV of server S receives -1 value while I'd be to wait 0 (closed socket).
    The behaviour is the same when I killed process by click ctrl-C keys sequence.

    My target is in Server (old std C89), where I want to detect remote closed socket so that I can close bad local socket.
    How can I distinct this action if result value is always -1 ?

  2. #2
    Join Date
    Nov 2018
    Posts
    116

    Re: return value of recv when process is killed

    https://man7.org/linux/man-pages/man....2.html#ERRORS
    When a function returns -1, it also sets the global integer errno (in errno.h) to indicate the cause.

    See also
    https://man7.org/linux/man-pages/man7/tcp.7.html#ERRORS
    https://man7.org/linux/man-pages/man7/ip.7.html#ERRORS

  3. #3
    Join Date
    May 2018
    Posts
    158

    Re: return value of recv when process is killed

    I noted select return 1 when remote process is receiving ctrl-c while I thought it received -1 value.
    I create server process where stdin and communication sockets are inserted into fd_set to monitor, same thing for client process.
    In both cases if I kill one process of the two processes , select return 1 and I found out ready process to read is the communication socket.
    So select doesn't know info about remote socket is closed but I gave it by recv function which will return 0.
    Right ?

    My doubt is because socket is always ready? I know I have to remove this socket to monitor but I thought if recv is executed there is no reason to see it as ready socket from select function.
    Last edited by zio_mangrovia; July 17th, 2023 at 09:10 AM.

  4. #4
    Join Date
    Nov 2018
    Posts
    116

    Re: return value of recv when process is killed

    > I noted select return 1 when remote process is receiving ctrl-c while I thought it received -1 value.
    Select() only tells you that something happened on a monitored fd.

    You then need to call recv() (or whatever is appropriate for that fd) to find out what actually happened.

    So for example, if recv() returned 0, the remote closed the connection and there will be no more data.
    Meaning you should also close(), and remove the fd from the set of fd's being monitored by select()

    If recv() returned -1, it's an error. You look at errno and decide whether to give up or try to carry on.
    For example, EAGAIN and EINTR are usually worth a retry. Most everything else is permanently fatal.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured