Wsaasyncselect ()

Source: Internet
Author: User
WSAAsyncSelect () Description:
The notification set interface has a request event.

# Include <winsock. h>
Int pascal far WSAAsyncSelect (SOCKET s, HWND hWnd,
Unsigned int wMsg, long lEvent );

S identifies the descriptor of a set of interfaces that require event notification.
HWnd identifies a window handle for receiving messages when a network event occurs.
The message that wMsg receives when a network event occurs.
LEvent bit shielding code, used to indicate a collection of network events that an application is interested in.

Note:
This function is used to request Windows Sockets DLL to send a message for the window handle-no matter when it detects a network event specified by the lEvent parameter. the message to be sent is indicated by the wMsg parameter. the notification set interface is identified by s.
This function automatically sets the set interface to non-blocking mode.
The lEvent parameter is composed of values listed in the following table.
Value Meaning
FD_READ to receive notifications of read preparation.
FD_WRITE: to receive written notifications.
FD_OOB wants to receive notifications of arrival of Edge Data.
FD_ACCEPT wants to receive notifications to be connected.
FD_CONNECT wants to receive notifications that have been connected.
FD_CLOSE: You want to receive notifications about interface closures.

Starting a WSAAsyncSelect () will invalidate all previous WSAAsyncSelect () started for the same set of interfaces. for example, to receive read/write notifications, the application must simultaneously call WSAAsyncSelect () with FD_READ and FD_WRITE, as follows:
Rc = WSAAsyncSelect (s, hWnd, wMsg, FD_READ | FD_WRITE );
It is impossible to distinguish different messages from different events. The following code will not work; the second call will invalidate the role of the first call, and only fd_write will be notified through the wmsg2 message.
Rc = wsaasyncselect (S, hwnd, wmsg1, fd_read );
Rc = wsaasyncselect (S, hwnd, wmsg2, fd_write );
If you want to cancel all notifications, that is, if the implementation of Windows Sockets no longer sends any messages related to network events on the set interface, Levent should be set to 0.
Rc = wsaasyncselect (S, hwnd, 0, 0 );

In this example, even though wsaasyncselect () immediately invalidates the event message sent to this set of interfaces, messages may still exist in the application message queue. therefore, the application must still be prepared to receive network messages-even if the message is voided. closing a set of interfaces with closesocket () also invalidates messages sent by wsaasyncselect (), but messages in the queue still work before closesocke.
Since a set of interfaces that have called accept () has the same attributes as the set of listening interfaces used to receive it, any WSAAsyncSelect () set for the set of listening Interfaces () the event also takes effect on the received set of interfaces. for example, if a listening set of interfaces has the WSAAsyncSelect () event FD_ACCEPT, FD_READ, FD_WRITE, then any set of interfaces received on that listening set of interfaces will also have FD_ACCEPT, FD_READ, FD_WRITE event, and the same wMsg value. if different wMsg and events are required, the application should call WSAAsyncSelect () to pass the received interface and the new message to be sent as parameters.
When a named network event occurs on a set of interfaces, the application window hWnd receives the message wMsg. the wParam parameter identifies a set of interfaces for network events. lParam indicates a network event. lParam contains an error code. the error code can be winsock. any errors defined in h.
You can use the WSAGETSELECTERRORH and WSAGETSELECTEVENT macros to retrieve error codes and events from lParam. The definitions are as follows:
# Define WSAGETSELECTERROR (lParam) HIWORD (lParam)
# Define WSAGETSELECTEVENT (lParam) LOWORD (lParam)
Note: There is a timing window in the accept () call and for the wsaasyncselect () call that changes the event or wmsg. if the application needs to send a different wmsg to the intercepted and the set of interfaces that have called accept (), it should request the fd_accept event on the intercepted set of interfaces, and then in accept () set the corresponding event after the call. because fd_accept is never sent to connected sets of interfaces, while fd_read, fd_write, fd_oob, and fd_close are never sent to listening sets of interfaces, it will not cause difficulties.
Using the above macros will maximize the portability of applications.
Possible network events returned are as follows:
Value Meaning
Fd_read set interface s for read
Fd_write set interface s for writing
Fd_oob out-of-band data is ready to be read on the set interface S.
Fd_accept sets interface s to receive new connections.
The connection on interface s of fd_connect is complete.
Fd_close the connection identified by the set interface S has been closed.
Return Value:
0. If the network event is declared successfully for the application.
Socket_error. Otherwise, you can call wsagetlasterror () to return the specific error code.
Rating:
Although wsaasyncselect () can be called by combining multiple events, the application window still receives a message for each network event.
Like the select () function, wsaasyncselect () is frequently called to determine when a data transfer operation (send () or Recv () can be started and can be successful immediately. however, a robust application must be prepared to receive messages and start a Windows Sockets API call that immediately returns wsaewouldblock. for example, the following sequence of events is possible:
(I) Data arrival set interface s; Windows Sockets transmits wsaasyncselect messages.
(Ii) the application processes other messages.
(Iii) during processing, the application started ioctlsocket (S, fionread...) and noticed that data is ready for reading.
(Iv) The application starts Recv (S,...) to read data.
(V) the application processes the next message cyclically and finally reaches the wsaasyncselect message, indicating that the data is ready for reading.
(Vi) The application starts Recv (S,...), but fails with the error wsaewouldblock.
Other event sequences are also possible.
Windows Sockets DLL does not constantly send messages to an application for a specific network event. if you have successfully sent a notification for a specific event to the application window, the application window will no longer send messages for this network event, until the application calls the function to implicitly notify the network event again.
Event re-notification function
FD_READ recv () or recvfrom ()
FD_WRITE send () or sendto ()
FD_OOB recv ()
FD_ACCEPT accept ()
FD_CONNECT none
FD_CLOSE none
Any call to the re-notification function will receive a re-notification message for the related event even if it fails.
For FD_READ, FD_OOB, and FD_ACCEPT events, message transmission is "level-triggered. this means that if the re-notification function is called and the related events are still valid for the call, the WSAAsyncSelect () message will be sent to the application. this provides an event-driven application and does not have to consider the amount of data arriving at any time. consider the following sequence:
(I) Windows Sockets DLL receives 100 bytes of data on the set interface s and transmits an FD_READ message.
(Ii) The application starts recv (s, buffptr, 50, 0) to receive 50 bytes.
(Iii) because data is still unread, Windows Sockets DLL sends another FD_READ message.

Based on the preceding semantics, the application does not need to read all readable data when receiving the FD_READ message-it is appropriate to make a recv () call for each FD_READ message. if the application initiates multiple recv () calls for an FD_READ message, it will receive multiple FD_READ messages. such an application may want to disable the FD_READ message before calling recv () (by calling the WSAAsyncSelect () function that is not set to the FD_READ event.
If an event is true when the application calls WSAAsyncSelect () for the first time or when a new notification function is called, a message is sent. for example, if the application calls listen (), it tries to connect, and then the application calls WSAAsyncSelect () to declare that it needs to receive FD_ACCEPT messages for the set interface, the implementation of Windows Sockets will immediately pass an FD_ACCEPT message.
The FD_WRITE event is slightly different in processing. the FD_WRITE message is sent when the set interface is connected with connect () for the first time or accepted by accept (), and when the buffer zone is idle after sending () or sendto () fails with the WSAWOULDBLOCK error. therefore, the application can assume that the message may start when the first FD_WRITE message is sent and continues until one request is sent to WSAEWOULDBLOCK. after such a failure, the application will be notified, and the FD_WRITE message will be sent again.
FD_OOB events are only used when the interface is configured to receive out-of-band data independently. if an interface is configured to receive out-of-band data of interest, out-of-band data will be treated as normal data, and the application should register areas of interest to it, the FD_READ event will be received instead of the FD_OOB event. applications can set or monitor out-of-band data processing methods (by using the setsockopt () or getsockopt () functions, and the SO_OOBINLINE option ).
The error code in the FD_CLOSE message indicates whether the interface is closed normally or abnormally. if the error code is 0, it is normal to disable it. If the error code is WSAECONNRESET, the interface's virtual interface will be reset. these functions only apply to interfaces of the SOCK_STREAM type.
The FD_CLOSE message is sent when the virtual circuit of the corresponding interface closes the command. in TCP terminology, this means that FD_CLOSE is sent when the connection enters the fin wait or close wait Status. this is the result of a remote shutdown () call or closesocket () call to the sender.
Please note that your application will only receive the FD_CLOSE message to indicate that the circuit is closed. It will not receive the FD_READ message to indicate this situation.
Error code:
WSANOTINITIALISED must make a successful WSAStartup () call before using this API.
Wsaenetdown windows sockets has detected network subsystem faults.
WSAEINVAL indicates that one of the specified parameters is invalid.
WSAEINPROGRESS a blocked Windows Sockets operation is in progress.
The additional error codes may be set when messages are received in the application window. These codes can be retrieved from lParam using the WSAGETSELECTERROR macro. The possible error codes corresponding to each network event are:
Event: FD_CONNECT
The address specified by WSAEADDRINUSE has been used.
The address specified by WSAEADDRNOTAVAIL cannot be used on the local machine.
The address of the specified WSAEAFNOSUPPORT family cannot be used with the same interface.
The attempt to connect to WSAECONNREFUSED is rejected.
WSAEDESTADDRREQ requires a destination address.
The WSAEFAULT namelen parameter is incorrect.
The WSAEINVAL interface has been bound to an address.
The WSAEISCONN interface has been connected.
WSAEMFILE does not have an available file descriptor.
WSAENETUNREACH cannot be accessed from this host.
WSAENOBUFS has no available buffer space. The interface cannot be connected.
The interfaces of WSAENOTCONN are not connected.
WSAENOTSOCK this descriptor is a file, not a set of interfaces.
WSAETIMEDOUT tries to connect timeout and no connection is established.
Event: FD_CLOSE
Wsaenetdown windows sockets has detected network subsystem faults.
The WSAECONNRESET connection is reconstructed from the remote end.
WSAECONNABORTED disconnects due to timeout or other failures.
Event: FD_READ
Event: FD_WRITE
Event: FD_OOB
Event: FD_ACCEPT
Wsaenetdown windows sockets has detected network subsystem faults.
Description of the Windows Sockets provider:
The provider of Windows Sockets should ensure that the message can be successfully transmitted to the application. If the postmescript () operation fails, the implementation of Windows Sockets must resend the message as long as the window exists.
The Windows Sockets provider should use the WSAMAKESELECTREPLY macro to construct the lParam parameter in the message.
When an interface is disabled, the Windows Sockets provider should clear all messages that are retained and sent to the application window. however, the application must be ready to receive and discard any messages that may have been sent before closesocket.
See:
Select ()

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.