Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux socket - set non blocking using O_NONBLOCK

If I make a TCP socket descriptor non blocking using fcntl(sockfd, F_SETFL, O_NONBLOCK);, does that make all the subsequent socket API's like read()/listen()/accept()/ etc.. also non blocking?

like image 739
Franc M Avatar asked Aug 09 '26 02:08

Franc M


1 Answers

There's an official answer in a man page, specifically socket(7):

It is possible to do nonblocking I/O on sockets by setting the O_NONBLOCK flag on a socket file descriptor using fcntl(2). Then all operations that would block will (usually) return with EAGAIN (operation should be retried later); connect(2) will return EINPROGRESS error. The user can then wait for various events via poll(2) or select(2).

So yes, setting O_NONBLOCK will cause blocking operations to return EAGAIN/EINPROGRESS instead of blocking.

like image 76
Malt Avatar answered Aug 11 '26 14:08

Malt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!