Select and WinSock asynchronous IO Models

Source: Internet
Author: User
Tags time in milliseconds

Select and WinSock asynchronous IO Models

If you want to build a server application on Windows, you must consider the I/O model.
Windows provides five I/O models:

■ Select );
■ Asynchronous selection (WSAAsyncSelect );
■ Event Selection (WSAEventSelect );
■ Overlapping I/O (Overlapped I/O );
■ Complete Port ).

Each model is applicable to a specific application scenario. Programmers should have very clear requirements for their own applications,
Make your choice based on factors such as program scalability and portability.
========================================================== ======
The select model is the most common I/O model in Winsock. The core is to use the select function to manage I/O!
The select function is used to determine whether data on a Socket is readable or whether data can be written to a Socket to prevent the program from being in blocking mode,
During an I/O call (such as send or recv, accept, etc.), the socket is forced to enter the "locked" state, while preventing the socket from being in a non-blocking mode.
WSAEWOULDBLOCK error.

The function prototype of explain select is as follows:
Int select (
_ In int nfds,
_ In_out fd_set * readfds,
_ In_out fd_set * writefds,
_ In_out fd_set * contains TFDs,
_ In const struct timeval * timeout
);

The first parameter nfds is ignored. This parameter is still provided to maintain compatibility with Berkeley sockets.
We can see three fd_set parameters later:
Readfds is used to check readability ),
One is used to check writefds ),
One is used for the exceptional TFDs ).

The fd_set structure is defined as follows:
Typedef struct fd_set {
U_int fd_count;
SOCKET fd_array [FD_SETSIZE];
} Fd_set;

# Define FD_SETSIZE 64
Therefore, the fd_set structure can only monitor up to 64 sockets.

Fdset represents a collection of specific sockets. The readfds set includes sockets that meet any of the following conditions:
● Data can be read.
● The connection has been closed, reset, or aborted.
● If you have already called listen and a connection is being established, the accept function is successfully called.

The writefds set includes sockets that meet any of the following conditions:
● Data can be sent.
● If you have finished processing a non-locked connection call, the connection will succeed.

The exceptfds set includes sockets that meet any of the following conditions:
● If you have finished processing a non-locked connection call, the connection attempt will fail.
● Out-of-band (OOB) data is available for reading.

For example, to test whether a socket is "readable", you must add your socket to the readfds set,
Then call the select function and wait for it to complete. After the select statement is complete, determine whether your socket is still part of the readfds set.
If the answer is yes, it indicates that the socket is "readable" and you can read data from it immediately.

Among the three parameters (readfds, writefds, and limit TFDs), either of them can be NULL );
However, at least one cannot be null! In any non-empty set, at least one socket handle must be included;
Otherwise, the select function will have nothing to wait. The last timeout parameter corresponds to a pointer pointing to a timeval structure,
Determines how long the select statement can wait for the I/O operation to complete. If timeout is a null pointer, the select call will be unlimited.
Wait until at least one descriptor meets the specified conditions.

The timeval structure is defined as follows:
The TV _sec field specifies the wait time in seconds;
The TV _usec field specifies the wait time in milliseconds.
1 second = 1000 milliseconds

If the time-out value is set to (0, 0), select returns immediately. This setting should be avoided for performance considerations.

Return Value of the explain select function:
After the select operation is complete, the total number of socket handles that have just completed the I/O operation is returned in the fdset structure.
If the time exceeds the time specified by timeval, 0 is returned. If the select call fails, SOCKET_ERROR is returned,
WSAGetLastError should be called to get the error code!

Before using select to monitor sockets, the socket handle must be assigned to a fdset structure set,
Then, call select to see whether the above I/O activity is happening on a socket.
Winsock provides the following macro operations to process and check fdset for I/O activities:
● FD_CLR (s, * set): deletes socket s from set.
● FD_ISSET (s, * set): checks whether s is a member of the set. If the answer is yes, TRUE is returned.
● FD_SET (s, * set): adds socket s to the set.
● FD_ZERO (* set): initializes the set to an empty set.

For example, assume that we want to know whether data can be securely read from a socket without endless
In the "locked" status, you can use the FDSET macro to allocate your socket to the fdread set and then call select. Yes
To check whether your socket is still part of the fdread set, use the FD_ISSET macro. Follow these steps:
The entire process of using select to operate one or more socket handles can be completed:
1) Use the FDZERO macro to initialize an fdset object;
2) use the FDSET macro to add the socket handle to the fdset collection;
3) Call the select function and wait for it to return ...... After the select statement is complete, the total number of socket handles set in all fdset sets is returned,
Update each set accordingly.
4) Checks fdset Sets Based on select return values and FDISSET macros.
5) after knowing the "pending" I/O operations in each set, process the I/O,
Then return to step 1) to continue the select process.

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.