WSAAsyncSelect function
The WSAAsyncSelect function requests Windows message-based notification of network events for a socket.
SyntaxC++
int WSAAsyncSelect( _In_ SOCKET s, _In_ HWND hWnd, _In_ unsigned int wMsg, _In_ long lEvent);
Parameters
-
s [in]
-
A descriptor that identifies the socket for which event notification is required.
-
hWnd [in]
-
A handle that identifies the window that will receive a message when a network event occurs.
-
wMsg [in]
-
A message to be received when a network event occurs.
視窗訊息迴圈要接受的訊息。
-
lEvent [in]
-
A bitmask that specifies a combination of network events in which the application is interested.
Return value
If the WSAAsyncSelect function succeeds, the return value is zero, provided that the application‘s declaration of interest in the network event set was successful. Otherwise, the value SOCKET_ERROR is returned, and a specific error number can be retrieved by calling WSAGetLastError.
成功返0,得到之前聲明感興趣的網路事件
失敗返SOCKET_ERROR
| Error code |
Meaning |
-
WSANOTINITIALISED
|
A successful WSAStartup call must occur before using this function. |
-
WSAENETDOWN
|
The network subsystem failed. |
-
WSAEINVAL
|
One of the specified parameters was invalid, such as the window handle not referring to an existing window, or the specified socket is in an invalid state. |
-
WSAEINPROGRESS
|
A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. |
-
WSAENOTSOCK
|
The descriptor is not a socket. |
Additional error codes can be set when an application window receives a message. This error code is extracted from the lParam in the reply message using theWSAGETSELECTERROR macro. Possible error codes for each network event are listed in the following table.
WSAGETSELECTERROR宏取lParam低位元組,看視窗訊息內的網路事件的具體錯誤
Event: FD_CONNECT
| Error code |
Meaning |
| WSAEAFNOSUPPORT |
Addresses in the specified family cannot be used with this socket. |
| WSAECONNREFUSED |
The attempt to connect was rejected. |
| WSAENETUNREACH |
The network cannot be reached from this host at this time. |
| WSAEFAULT |
The namelen parameter is invalid. |
| WSAEINVAL |
The socket is already bound to an address. |
| WSAEISCONN |
The socket is already connected. |
| WSAEMFILE |
No more file descriptors are available. |
| WSAENOBUFS |
No buffer space is available. The socket cannot be connected. |
| WSAENOTCONN |
The socket is not connected. |
| WSAETIMEDOUT |
Attempt to connect timed out without establishing a connection. |
Event: FD_CLOSE
| Error code |
Meaning |
| WSAENETDOWN |
The network subsystem failed. |
| WSAECONNRESET |
The connection was reset by the remote side. |
| WSAECONNABORTED |
The connection was terminated due to a time-out or other failure. |
-
Event: FD_ACCEPT
-
Event: FD_ADDRESS_LIST_CHANGE
-
Event: FD_GROUP_QOS
-
Event: FD_OOB
-
Event: FD_QOS
-
Event: FD_READ
-
Event: FD_WRITE
| Error code |
Meaning |
| WSAENETDOWN |
The network subsystem failed. |
Event: FD_ROUTING_INTERFACE_CHANGE
| Error code |
Meaning |
| WSAENETUNREACH |
The specified destination is no longer reachable. |
| WSAENETDOWN |
The network subsystem failed. |
Remarks
The WSAAsyncSelect function is used to request that WS2_32.DLL should send a message to the window hWnd when it detects any network event specified by thelEvent parameter. The message that should be sent is specified by the wMsg parameter. The socket for which notification is required is identified by the s parameter.
WSAAsyncSelect 要WS2_32.DLL給視窗發訊息,當檢測到lEvent的時候發,發的是wMsg訊息,s通訊端被監察
The WSAAsyncSelect function automatically sets socket s to nonblocking mode, regardless of the value of lEvent. To set socket s back to blocking mode, it is first necessary to clear the event record associated with socket s via a call to WSAAsyncSelect with lEvent set to zero. You can then call ioctlsocket or WSAIoctl to set the socket back to blocking mode. For more information about how to set the nonblocking socket back to blocking mode, see the ioctlsocket and WSAIoctlfunctions.
WSAAsyncSelect 把通訊端調成非阻塞,而不管你給的lEvent是什麼。
要調回阻塞就要用WSAAsyncSelect 把IEvent置零。然後就可以用 ioctlsocket 或者 WSAIoctl 把通訊端調成阻塞模式。
The lEvent parameter is constructed by using the bitwise OR operator with any value listed in the following table.
| Value |
Meaning |
| FD_READ |
Set to receive notification of readiness for reading. |
| FD_WRITE |
Wants to receive notification of readiness for writing. |
| FD_OOB |
Wants to receive notification of the arrival of OOB data. |
| FD_ACCEPT |
Wants to receive notification of incoming connections. |
| FD_CONNECT |
Wants to receive notification of completed connection or multipoint join operation. |
| FD_CLOSE |
Wants to receive notification of socket closure. |
| FD_QOS |
Wants to receive notification of socket Quality of Service (QoS) changes. |
| FD_GROUP_QOS |
Wants to receive notification of socket group Quality of Service (QoS) changes (reserved for future use with socket groups). Reserved. |
| FD_ROUTING_INTERFACE_CHANGE |
Wants to receive notification of routing interface changes for the specified destination(s). |
| FD_ADDRESS_LIST_CHANGE |
Wants to receive notification of local address list changes for the socket protocol family. |
Issuing a WSAAsyncSelect for a socket cancels any previous WSAAsyncSelect or WSAEventSelect for the same socket. For example, to receive notification for both reading and writing, the application must call WSAAsyncSelect with both FD_READ and FD_WRITE, as follows:
關注的事件要一起設定在一個訊息裡
C++
rc = WSAAsyncSelect(s, hWnd, wMsg, FD_READ|FD_WRITE);
It is not possible to specify different messages for different events. The following code will not work; the second call will cancel the effects of the first, and onlyFD_WRITE events will be reported with message wMsg2:
不要分開去設定在不同訊息但又是相同的通訊端裡。。。
C++
rc = WSAAsyncSelect(s, hWnd, wMsg1, FD_READ);rc = WSAAsyncSelect(s, hWnd, wMsg2, FD_WRITE);
To cancel all notification indicating that Windows Sockets should send no further messages related to network events on the socket, lEvent is set to zero.
C++
rc = WSAAsyncSelect(s, hWnd, 0, 0);
這要就不髮網絡事件了
Although WSAAsyncSelect immediately disables event message posting for the socket in this instance, it is possible that messages could be waiting in the application message queue. Therefore, the application must be prepared to receive network event messages even after cancellation. Closing a socket withclosesocket also cancels WSAAsyncSelect message sending, but the same caveat about messages in the queue still applies.
雖然你這樣取消了發送或者說對網路事件的監聽,但是之前遺留在Window訊息佇列裡的網路訊息還是有效,也就是隨後將根據隊列順序發給視窗,這是就造成了取消之後,視窗仍然會收到網路訊息的情況。