WSAAsyncSelect function (Windows)

Source: Internet
Author: User
Tags set socket

WSAAsyncSelect function

The wsaasyncselect function requests Windows message-based notification of the network events for a socket.

int WSAAsyncSelect (  _in_ SOCKET S,  _in_ hwnd hwnd,  _in_ unsigned int wmsg,  _in_ long lEvent);
s [in]

A descriptor that identifies the socket for which event notification is required.

hWnd [In]

A handle that identifies the window that would receive a message when a network event occurs.

wmsg [In]

A message to is received when a network event occurs.

The message that the window message loops to accept.

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 the application ' s declaration of Interest in the network event set is successful. Otherwise, the value Socket_error is returned, and a specific ERROR number can being retrieved by calling Wsagetlasterro R.

Successful return to 0 for network events that were previously declared to be of interest

Failed return Socket_error

Error Code meaning

A successful WSAStartup call must occur before using the This function.


The network subsystem failed.


One of the specified parameters is invalid, such as the window handle not referring to an existing window, or the Specifi Ed Socket is a invalid state.


A blocking Windows Sockets 1.1 call are in progress, or the service provider is still processing a callback function.


The descriptor is not a socket.

Additional error codes can set when a application window receives a message. This error code was extracted from the lParam in the reply message using theWSAGETSELECTERROR macro. Possible error codes for each network event is listed in the following table.

WSAGETSELECTERROR macro take lparam low byte, look at the specific error of the network event inside the window message


Error Code meaning
Wsaeafnosupport Addresses in the specified family cannot is used with this socket.
WSAECONNREFUSED The attempt to connect is rejected.
Wsaenetunreach The network cannot is 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 is 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.


Error Code meaning
Wsaenetdown The network subsystem failed.
Wsaeconnreset The connection is reset by the remote side.
wsaeconnaborted The connection is terminated due to a time-out or other failure.

Error Code meaning
Wsaenetdown The network subsystem failed.


Error Code meaning
Wsaenetunreach The specified destination is no longer reachable.
Wsaenetdown The network subsystem failed.

The wsaasyncselect function is used to request this 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 was sent is specified by the wmsg parameter. The socket for which notification is required are identified by the s parameter.

WSAAsyncSelect to Ws2_32.dll to send a message to the window, when detected Levent, hair is wmsg message, s socket is monitored

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's 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 to blocking mode. For more information about what to set the nonblocking socket back to blocking mode, see the ioctlsocket and WSAIoctlfunctions.

WSAAsyncSelect The socket into non-blocking, regardless of the levent you give .

To recall the blockage, use wsaasyncselect to set the ievent to zero. Then you can use ioctlsocket or wsaioctl to turn the socket into blocking mode.

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:

The events of interest are set together in a message.

rc = WSAAsyncSelect (S, hWnd, wmsg, fd_read| Fd_write);

It is not a possible to specify different messages for different events. The following code won't work; The second call would cancel the effects of the first, and onlyfd_write events would be reported with message WMSG2 :

Do not separate to set in different messages but also the same socket ...

rc = WSAAsyncSelect (S, hwnd, WMSG1, fd_read), rc = WSAAsyncSelect (S, hwnd, WMSG2, Fd_write);

To cancel all notification indicating the Windows Sockets should send no further messages related to network events on th E socket, lEvent is set to zero.

rc = WSAAsyncSelect (s, hWnd, 0, 0);

It's not going to be a network event.

Although WSAAsyncSelect immediately disables event message posting for the socket in this instance, it's Possibl E that messages could is waiting in the application message queue. Therefore, the application must is 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.

Although you have canceled the sending or listening to network events, the network messages that were left in the window message queue are still valid, which will then be sent to the window according to the queue order, which is the case that the window will still receive network messages after the cancellation.

WSAAsyncSelect function (Windows)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.