Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Socket File Descriptor is Available?

If I got a file descriptor (socket fd), how to check this fd is avaiable for read/write? In my situation, the client has connected to server and we know the fd. However, the server will disconnect the socket, are there any clues to check it ?

like image 942
qrtt1 Avatar asked Dec 12 '25 04:12

qrtt1


2 Answers

You want fcntl() to check for read/write settings on the fd:

#include <unistd.h>
#include <fcntl.h>

int    r;

r = fcntl(fd, F_GETFL);
if (r == -1)
        /* Error */
if (r & O_RDONLY)
    /* Read Only */
else if (r & O_WRONLY)
    /* Write Only */
else if (r & O_RDWR)
    /* Read/Write */

But this is a separate issue from when the socket is no longer connected. If you are already using select() or poll() then you're almost there. poll() will return status nicely if you specify POLLERR in events and check for it in revents.

If you're doing normal blocking I/O then just handle the read/write errors as they come in and recover gracefully.

like image 60
dwc Avatar answered Dec 14 '25 18:12

dwc


In C#, this question is answered here

In general, socket disconnect is asynchronous and needs to be polled for in some manner. An async read on the socket will typically return if it's closed as well, giving you a chance to pick up on the status change quicker. Winsock (Windows) has the ability to register to receive notification of a disconnect (but again, this may not happen for a long time after the other side "goes away", unless you use some type of 'keepalive' (SO_KEEPALIVE, which by default may not notice for hours, or an application-level heartbeat).

like image 40
jesup Avatar answered Dec 14 '25 18:12

jesup