The information in this article applies to:
SUMMARYThis 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.
Q179942 INFO: WSA_FLAG_OVERLAPPED Is Needed For Non-Blocking SocketsPlease 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/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 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 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 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 |
Last Reviewed: October 15, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |