Select model for I/O model

Source: Internet
Author: User

I have just taken a look at the relevant knowledge of the I/O model and made a summary to share with you.

The selct model refers to the operation in the socket set fd_set (multiple sockets can be connected together to form a socket set)

The socket set is also divided into three forms. The corresponding relationships are as follows:


Socket set Event occurredWhat exactly?

The readfds select function waits for pending connection requests or data reading or connection shutdown/restart/interruption.

The writefds select function waits for a successful connection (call Connect) or data to be writable.

Wait TFDs select function waiting for connection failure or OOB data readable


The following method is used to determine whether to read and write data. For example:

Test whether a socket S is readable and add it to readfds. Wait until the select function returns. After the call is complete, check whether s is still in readfds.

The general process is as follows:

(1) initialize a socket set fdsocket and add the listening socket handle slisten to this set:

Fd_set fdsocket;

Fd_set (slisten, & fdsocket );

(2) When a copy of The fdsockt set fdread is passed to the select function, when an event occurs, the Select function will remove the outstanding socket handle not found in fdread and return:

Fd_set fdread = fdsocket;

Int nret =: Select (0, & fdread, null );

(3) Compare the original fdsocket set with fdread processed by select to determine which sockets have pending I/O and further process them:

For (INT I = 0; I <(INT) fdsocket. fd_count; I ++)
{
If (fd_isset (fdsocket. fd_array [I], & fdread) // you can check whether the object is readable.
{
If (fdsockt. fd_array [I] = slisten) // indicates that the listener socket wants to connect.
{
Sockaddr_in addrremote;
Int naddrlen = sizeof (addrremote );
Socket snew =: accept (slisten, (sockaddr *) & addrremote, & naddrlen );
Fd_set (snew, & fdsockt );
}
Else // indicates that the data is readable.
{
Char sztext [256];
Int nrecv =: Recv (fdsocket. fd_array [I], sztext, strlen (sztext), 0 );
}
}
}


You can know whether it can be written, which is similar to this one.

The advantage of the model selection is that multiple socket requests can be processed in a single thread, but the number of sockets added to the fd_set structure is limited by default. The maximum number of sockets can be up to 1024.

If there are too many sockets, you have to set these 1000 sockets before calling the SELECT statement. After the SELECT statement is returned, check the 1000 sockets on the right.

Finally, I like the following sentence:

Some persistence makes a series of accidents.

Select model for I/O model

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.