Article ID: 181611 - View products that this article applies to.
This article was previously published under Q181611
This article explains the difference between a socket's overlapped I/O attribute and the socket's blocking or nonblocking mode.
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 nonblocking. If a Winsock call cannot complete immediately, the call fails and WSAGetLastError returns a WSAEWOULDBLOCK error if it's a nonblocking 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 nonblocking mode. Although the current Winsock implementation requires overlapped I/O attribute for nonblocking 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:
179942Please 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 nonzero option values make the socket synchronous and make it so that you cannot use a completion function.
(http://support.microsoft.com/kb/179942/EN-US/ )INFO: WSA_FLAG_OVERLAPPED Is Needed For Non-Blocking Sockets
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/OIn 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 nonblocking 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 nonblocking modeWhen 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 nonblocking 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 nonblocking mode as well. But you need to poll the status of the socket by using the select API.
Update: Setting SO_RCVBUF to zero with overlapped recv is not necessary for Windows 2000, which can directly copy the data to application's recv buffer. Setting SO_SNDBUF to zero with overlapped send on Windows 2000 still has the same benefit of saving one memory copy as on Windows NT 4.0. It is also important that the application issues several outstanding overlapped send's or recv's if the application sets the corresponding stack buffer to 0.