c++socketsposix-select

C++ TCP server still reading data after client disconnects


I made a server that uses select() to check which of the socket descriptors have data in them, but apparently select marks a socket to be ready to read from even after the client disconnects, and I get garbage values.

I have found this post on Stack Overflow:

select (with the read mask set) will return with the handle signalled, but when you use ioctl* to check the number of bytes pending to be read, it will be zero.

My question is what is ioctl and how to use it? And an example would be very good.


Solution

  • If a call to read() on a socket (file) descriptor returns 0, that simply means the other side of the connection had shutdown and closed the connection.

    Note: A select() waiting for possible "events" on set(s) of socket (file) descriptors will also return when a connection represented by one of the fd_set's passed to select() had been shutdown.