I. The selection (SELECT) model of the Windows I/O model

Source: Internet
Author: User
Tags set set

1. Select Model:
Select model: Through a Fd_set collection management socket, after the socket requirements are met, the socket is notified. Let the socket work. Avoid sockets entering blocking mode and making unnecessary waits. Select the Fd_set collection and the Select function at the core of the model. With this function, we can determine whether the data is present on the socket or whether it can be written to a socket.

2.select function:
int Select (
int nfds,//is ignored and exists only for compatibility.
Fd_set far* readfds,//readability check (with data readable, connection closed, reset, terminate)
Fd_set far* writefds,//writable check (with data available)
Fd+set far* exceptfds,//out-of-band data check (out-of-band data)
const struct Timeval far* timeout//timeout
);

Working steps for the 3.select model:
(1) First add the socket to the Fd_set collection
(2) Check the accessibility of sockets
(3) Check whether the socket is still on the Fd_set collection
(4) Processing data

4. In three parameters (Readfds, Writefds, and Exceptfds), either of the two can be null, but at least one cannot be a null value! The last parameter, timeout, corresponds to a pointer to a TIMEVAL structure that is used to select the maximum amount of time to wait for the I/O operation to complete. If timeout is a null pointer, then the SELECT function waits indefinitely until at least one of the sockets meets the specified condition and returns. When select completes successfully, the total number of socket handles for incomplete I/O operations is returned in the Fd_set collection. If it times out, it returns 0. For whatever reason, if the select call fails, a SOCKET_ERROR error will be returned.

5.timeval Structure Definition:
struct Timeval
{
Long tv_sec;//number of seconds
Long tv_usec;//number of milliseconds
};

6.fd_set Collection: Before you use the Select function to monitor a socket, you must assign the socket to a Fd_set collection, set the FD_SET structure for read, write, and out-of-band data. After you assign a socket to any collection, and then call select for monitoring, you know whether the above I/O activity is occurring on a socket. Winsock provides the following macro
Processing and inspection of Fd_set:
Fd_zero (*set): Initialize set
Fd_set (S, *set): Add socket s to set set
FD_CLR (S, *set): Removes the socket s from set.
Fd_isset (S,*set): Checks if S is still on the collection set, which must be judged before invoking the Select function.

Working steps for the 7.select model:
(1) Use Fd_zero macros to initialize each of the fd_set you are interested in.
(2) Use the Fd_set macro to assign the socket handle to each fd_set that you are interested in.
(3) Call the Select function and wait for the I/O operation to complete.
(4) Based on the return value of SELECT, we can determine which sockets have not yet completed (pending) I/O operations. The specific method is to check each Fd_set collection using the Fd_isset macro.
(5) After you know the "pending" I/O operation in each collection, process the I/O and return to step 1) to continue with select processing.
(6) When Select returns, it modifies each fd_set structure to remove socket handles that do not exist for pending I/O operations. This is exactly why we use the Fd_isset macro to determine whether a particular socket is still in the collection in the above step (4).

Example code:

1 SOCKET S;2 fd_set Fdread;3 intret;4 5 //Initialize6 ...7 8 //Manage I/O on the socket9  while(1)Ten { OneFd_zero (&fdread);//Initializing a collection A  -Fd_set (S,&fdread);//add a socket to the collection -  theret=Select(0, &fdread,null,null,null);//call the Select function to monitor the completion of network events -     if(ret==socket_error) -     { -         ... +     } -  +     if(res>0) A     { at         //working with Data -         ... -          -         if(Fd_isset (S,&fdread))//checks if the socket is still on the collection -         { -             ... in         } -     } to      +}
View Code

I. The selection (SELECT) model of the Windows I/O model

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