Windows – In Win32, is there a way to test if a socket is non-blocking

nonblockingsocketswinapiwindowswinsock

In Win32, is there a way to test if a socket is non-blocking?

Under POSIX systems, I'd do something like the following:

int is_non_blocking(int sock_fd) {
    flags = fcntl(sock_fd, F_GETFL, 0);
    return flags & O_NONBLOCK;
}

However, Windows sockets don't support fcntl(). The non-blocking mode is set using ioctl with FIONBIO, but there doesn't appear to be a way to get the current non-blocking mode using ioctl.

Is there some other call on Windows that I can use to determine if the socket is currently in non-blocking mode?

Best Answer

A slightly longer answer would be: No, but you will usually know whether or not it is, because it is relatively well-defined.

All sockets are blocking unless you explicitly ioctlsocket() them with FIONBIO or hand them to either WSAAsyncSelect or WSAEventSelect. The latter two functions "secretly" change the socket to non-blocking.

Since you know whether you have called one of those 3 functions, even though you cannot query the status, it is still known. The obvious exception is if that socket comes from some 3rd party library of which you don't know what exactly it has been doing to the socket.

Sidenote: Funnily, a socket can be blocking and overlapped at the same time, which does not immediately seem intuitive, but it kind of makes sense because they come from opposite paradigms (readiness vs completion).

Related Topic