What socket error do I get when TCP keep-alive disconnects?

I have a set of TCP sockets with keep-alive (1 min interval) controlled by a select(2) loop (select to read).

  • Will select(2) return an error if a keep-alive timeout occurs for one of the sockets in the event set?
  • What error will read(2) return?
+8
c linux sockets tcp
source share
2 answers

  • select() itself does not return an error if an error is specified for one of the sockets it selects. [Indeed, the API cannot indicate errors in each socket, because two different sockets can each receive an incomplete error during a single select() call. Which one of select() will return?]
  • After each iteration of the select() loop, instead, you use the FD_ISSET macro to try to execute read() on every socket that can be read.
  • Each time a socket waiting for errors is installed on its socket, its read event (and write event) is signaled, and select() returned, which allows you to receive errors when allocating time due to immediate saving. Please note that selecting the marking of the socket for reading does not mean that there is data to read only if the attempt to read will not be blocked. If the socket has a pending error to retrieve, reading will not be blocked. Both read(2) and write(2) first retrieve any pending error on the socket before even trying to process any data.

    The descriptor is considered read ready when the call to the input function with clearing O_NONBLOCK will not be blocked, regardless of whether the function successfully transfers data. (A function may return data, an indication of the end of a file, or an error other than one, indicating that it is locked, and in each of these cases the handle is considered read ready to be read.) [ POSIX: select () ]

  • Finally, what error is returned? Really, it depends on how keepalive failed. You will get ETIMEDOUT if the other end disappears completely. If a packet delivery error occurs, you will receive it by (so if the keep-alive packet receives an ICMP error response, such as "unreachable host", you will get EHOSTUNREACH ). [For more on these cases, see Stevens, “Unix Network Programming, Volume 1.”]
+5
source share

select() sets the bit to FDSET , which indicates which socket was started. Use the FD_ISSET macro to determine which socket the service requested.

+1
source share

All Articles