首页 > 代码库 > WSAAsyncSelect function (Windows)
WSAAsyncSelect function (Windows)
WSAAsyncSelect function
The WSAAsyncSelect function requests Windows message-based notification of network events for a socket.
Syntax
C++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消息队列里的网络消息还是有效的,也就是随后将根据队列顺序发给窗口,这是就造成了取消之后,窗口仍然会收到网络消息的情况。
WSAAsyncSelect function (Windows)