tags:

views:

45

answers:

2

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?

A: 

From MSDN, the return value of connect():

  • On a blocking socket, the return value indicates success or failure of the connection attempt.

  • With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError() will return WSAEWOULDBLOCK.

Vantomex
A: 

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.

Steve Townsend
A WinSock2 socket is also put into non-blocking mode if ony of the WSAAsync...() functions are called on it.
Remy Lebeau - TeamB