Dealing with blocking

Blocked Function Calls

Some WinSock2 library functions can become blocked waiting for one or more required conditions to be satisfied. For example, the recv function, which reads data from sockets, requires that there be data in the sockets to be read. The recv function will become blocked, if it is called to read data from a socket, and the socket's non-blocking mode is disabled (see non-blocking mode below), and there is no data in the socket. In this case the recv function will not return to the caller until there is data in the socket, or until the connection to the socket is closed at the other end. NOTE: If your program calls a WinSock2 library function that becomes blocked then your program will also be blocked waiting for the function to become unblocked and return from the call.

Sometimes you might want to allow your program to become blocked. For example if the sole purpose of your program is to read some data and process it, and your program can't sensibly continue until it has read the data, then you might choose to allow your program to become blocked if no data is available to be read. NOTE: When your program is blocked it does not use up any processor cycles, and so in the example above, being blocked can be a very effecient way for your program to wait until there is data for it to process.

On the other hand, sometimes you will not want your program to ever become blocked. This is true if your program must be always responsive. For example, if your program is a service, then it must always be ready to respond to a request to stop.

Non-Blocking Mode

Sockets have a non-blocking mode that can be enabled or disabled. When a socket is created, its non-blocking mode is disabled. One way to prevent WinSock2 library functions from becoming blocked is to use sockets with non-blocking mode enabled. If a WinSock2 library function is called, on a socket with non-blocking mode enabled, and conditions are such that the function would block were non-blocking mode disabled, then instead of becoming blocked the function call will fail. If you call WSAGetLastError after the function call fails, the return code will be WSAEWOULDBLOCK. So for example, if you call the recv function to read data from a socket, and the socket has non-blocking mode enabled, and there is no data waiting to be read from the socket, then the recv function will return SOCKET_ERROR to indicate that the call failed. If you then call the WSAGetLastError function, the error code returned will be WSAEWOULDBLOCK. NOTE: The constant WSAEWOULDBLOCK is declared in the system include file

How To Avoid Getting Blocked

There are basically two ways you can ensure that your program will never becoming blocked:

  1. You can enable non-blocking mode for sockets that might otherwise become blocked. See the ioctlsocket function for more information.
  2. You can make sure that the conditions that might cause a socket to block never occur. The conditions that might cause a socket to block are:  See the select function for more information.