INFO: Socket Overlapped I/O Versus Blocking/Non-blocking Mode

ID: Q181611

The information in this article applies to:

SUMMARY

This article explains the difference between a socket's overlapped I/O attribute and the socket's blocking or non-blocking mode.

MORE INFORMATION

When a socket is created, by default it is a blocking socket. You can use the FIONBIO command in the ioctlsocket API call, WSAEventSelect, or WSAAysncSelect to change the socket mode from blocking to non-blocking. If a Winsock call cannot complete immediately, the call fails and WSAGetLastError returns a WSAEWOULDBLOCK error if it's a non-blocking socket, or the call blocks until the operation completes if it's a blocking socket.

The socket overlapped I/O attribute is different from the socket's blocking or non-blocking mode. Although the current Winsock implementation requires overlapped I/O attribute for non-blocking socket mode, they are conceptually independent and their programming model is different too. To create a socket with the overlapped I/O attribute, you can either use the socket API or the WSASocket API with the WSA_FLAG_OVERLAPPED flag set. If an overlapped I/O operation can not complete immediately, the call fails and WSAGetLastError or GetLastError return WSA_IO_PENDING or ERROR_IO_PENDING, which is actually the same define as WSA_IO_PENDING. For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q179942
   TITLE     : INFO: WSA_FLAG_OVERLAPPED Is Needed For Non-Blocking
               Sockets

Please note that once a socket is created, there is no way to change the socket overlapped attribute. However, you can call the setsockopt API with SO_OPENTYPE option on any socket handles including an INVALID_SOCKET to change the overlapped attributes for all successive socket calls in the same thread. The default SO_OPENTYPE option value is 0, which sets the overlapped attribute. All non-zero option values make the socket synchronous and make it so that you cannot use a completion function.

By setting a socket's overlapped I/O attribute it doesn't mean that the socket will perform an overlapped I/O operation. For example, if you specify NULL for both the completion function and the overlapped structure in WSARecv and WSASend, or you simply call recv or send functions, they will complete in a blocking fashion. To make sure the I/O is performed in an overlapped fashion you need to provide an overlapped structure in your I/O function, depending on the function you use.

Overlapped I/O

In Winsock 1, you create an overlapped socket using the socket API, and use Win32 file I/O API ReadFile, ReadFileEx, WriteFile, WriteFileEx to perform overlapped I/O on the socket handle. In Winsock 2, you create an overlapped socket using WSASocket with the WSA_FLAG_OVERLAPPED flag, or simply using the socket API. You can use the above Win32 file I/O APIs or Winsock 2 WSASend, WSASendTo, WSARecv, and WSARecvFrom to initiate an overlapped I/O operation.

If you use the SO_RCVBUF and SO_SNDBUF option to set zero TCP stack receive and send buffer, you basically instruct the TCP stack to directly perform I/O using the buffer provided in your I/O call. Therefore, in addition to the non- blocking advantage of the overlapped socket I/O, the other advantage is better performance because you save a buffer copy between the TCP stack buffer and the user buffer for each I/O call. But you have to make sure you don't access the user buffer once it's submitted for overlapped operation and before the overlapped operation completes.

To determine whether the overlapped I/O operation is completed, you can use one of the following options:

Blocking and Non-Blocking Mode

When a socket is created, by default it is a blocking socket. Under blocking mode socket I/O operations, connect and accept operations all block until the operation in question is completed. To change the socket operation mode from blocking mode to non-blocking mode, you can either use WSAAsyncSelect, WSAEventSelect, or the FIONBIO command in the ioctlsocket API call.

WSAAsyncSelect maps socket notifications to Windows messages and is the best model for a single threaded GUI application.

WSAEventSelect uses WSAEnumNetworkEvents to determine the nature of the socket notification on the signaling event and maps socket notifications by signaling an event. This is a useful model for non-GUI applications that lack a message pump, such as a Windows NT service application.

The FIONBIO command in the ioctlsocket API call puts the socket into non- blocking mode as well. But you need to poll the status of the socket by using the select API.

Additional query words:

Keywords          : kbnetwork kbAPI kbNTOS400 kbSDKPlatform kbWinOS95 kbWinsock kbGrpNet 
Issue type        : kbinfo

Last Reviewed: July 31, 1998