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?
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.
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.
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".
listen() is non-blocking.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With