Check if socket is disconnected in C without select ()

Is there a way to check if a socket is disconnected on the remote end without select () in C?

The reason I don't want to use select () is because if my buffers are full, there might be data to read on the socket that I intentionally ignore and select (readfds = [socket_fd]) always returned giving me know.

0


a source to share


2 answers


If you have a call to the read () blocking function on a socket and it returns an integer value of 0, that means the socket connection is closed.

while ( n = read(sockfd, buffer, BUFFER_SIZE) ) {
     //use buffer contents
}

      



This cycle will continue while data is sent from the other end.

+4


a source


So, use poll()

by setting events

to whatever you want and check the results in revents

. FYI, you can call select()

or poll()

without damaging the sockets or altering their buffers.



+1


a source







All Articles