tcpudpiptftp

What happens to select() when socket is set to handle timeout


According to the manual page for recv(), errno is set to EAGAIN or EWOULDBLOCK if a timeout has been set for receive using setsockopt(SO_RCVTIMEO).

My question is what happens if multiple such sockets are used with select(). Would select return if one of the socket times out due to inactivity. What would be returned by select().

I am trying to implement a tftp server with feature to detect timeouts. One way could be to use a timeout with select() but then I would have to use a different value of timeout for each socket and keep updating the timer to the minimum value, and then do some more juggling.... etc.. etc... Just feels like a lot of unnecessary work.

PS: The tftp server is a concurrent server with multiple clients being handled using I/O Multiplexing.


Solution

  • The timeout parameter of select() determines the maximum time that the select() call itself will wait for something to happen before the call returns, not how long individual sockets will wait before returning a timeout error.

    It sounds like you are wanting to declare some kind of an error condition if you don't hear from a client for some period of time. With UDP, you will have to keep track of that yourself. For each client, keep a record of the last time you heard from it. Put select() in a loop with a timeout of something like 1 second, then every time it returns check the difference between the current time and the last time you heard from each client. When that difference exceeds whatever threshold you want, you have your error condition.