Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if a socket is already in Non-Blocking mode in Windows?

Tags:

c

windows

sockets

Is there a way to find out if a socket is already in Non-Blocking mode in Windows?

I know this can be done in case of Linux, but, I am unable to find any way for this Windows.

All my coding is in 'C' language. Is there a way?

like image 539
Jay Avatar asked Oct 20 '10 01:10

Jay


People also ask

What is a non-blocking socket?

In blocking socket mode, a system call event halts the execution until an appropriate reply has been received. In non-blocking sockets, it continues to execute even if the system call has been invoked and deals with its reply appropriately later.

How do I make a TCP socket non-blocking?

To mark a socket as non-blocking, we use the fcntl system call. Here's an example: int flags = guard(fcntl(socket_fd, F_GETFL), "could not get file flags"); guard(fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK), "could not set file flags"); Here's a complete example.

Is socket read blocking?

By default, TCP sockets are in "blocking" mode. For example, when you call recv() to read from a stream, control isn't returned to your program until at least one byte of data is read from the remote site. This process of waiting for data to appear is referred to as "blocking".

Is listen () a blocking call?

listen() is non-blocking.


1 Answers

The only way you can check this is by doing something illegal on a nonblocking socket and checking that it fails in an expected way. Hardly the most robust design.

The socket will be blocking unless you explicitly set it nonblocking using WSAIoctl or ioctlsocket with FIONBIO. That cannot be too hard to check in your code, I would have thought. If you have to track this at runtime, a flag per socket as suggested by @jweyrich is the way to go.

like image 66
Steve Townsend Avatar answered Sep 25 '22 18:09

Steve Townsend