Closed connection detection on a non-blocking socket

I am very sorry if my question is a duplicate, but I did not find useful information on the site.

I use non-blocking sockets and select (). How can I determine if a client has closed a connection on a non-blocking socket? I have seen that read () returns -1 with errno = EWOULDBLOCK when there is no data to read and also when the connection is closed.

How can I distinguish the above cases?

+4
source share
2 answers

When the partner established the connection:

  • select() will return the socket as readable.
  • A recv()or read()in the socket will return zero.

I saw that read () returns -1 with errno = EWOULDBLOCK when there is no data to read

Right, but the connection is not closed.

.

, . . .

?

, .

+2

, read() 0. .

man 2 read ( ):

( )

0

All Articles