Windows Socket I/O model-select model

Source: Internet
Author: User

See chapter 5 of Windows Network Programming. To consolidate knowledge and drive problems, check knowledge points.

Select model in Windows

1. What is the role of the select model?

You can determine whether data exists on the specified socket and whether data can be written. Prevents blocking caused by the use of send or Recv.

2. What are the advantages of the select model?

This can reduce the number of threads required by the original blocking model and reduce resource consumption.

3. What is the prototype of the select function?

Int select (

Int NFDs, // ignore unnecessary

Fd_set far * readfds,

Fd_set far * writefds,

Fd_set far * release TFDs,

Const struct timeval far * timeout

);

4. What are the conditions for the readfds set?

Readfds checks readability. when any of the following conditions is met, the corresponding socket will be retained in readfds after the select return:

* Data can be read.

* The connection has been closed, restarted, or terminated.

* If you have already called listen and a connection is waiting to be established, success will be returned at accept.

5. What are the conditions for the writefds set?

Writefds checks writability. when any of the following conditions is true, the corresponding socket will be retained in the writefds after the select return:

* Data can be sent

* If a non-blocking connection request is being processed and the connection is successful

6. What are the conditions for the limit TFDs set?

Except TFDs checks the exceptional socket status. when either of the following conditions is true, the corresponding socket will be retained in the future TFDs after the select return:

* If a non-blocking connection request is being processed and the connection attempt fails

* OOB (out of band) data is available for reading

7. How to test whether a socket is readable?

Add the socket s to the readfds collection, and then call the select function to wait for completion. After the select operation is complete, determine whether the socket S is still in the readfds collection. If yes, the data is readable, you can read data from it immediately. If not in the Set, there is no readable data.

8. Can the three sets be empty at the same time?

No. One or more sets are not empty. Select can wait.

9. will select be blocked? Which of the following parameter conditions is determined?

When the conditions in all three sets are not met, the blocking duration is determined by the timeval parameter. If the timeout pointer is null, it will be blocked until there are conditions in the three sets.

10. What will happen if the timeval is set to 0?

If the timeval value is set to 0, the Select Operation returns immediately after checking the status of the three sets, allowing the program to round-robin The select operation. However, this setting should be avoided for performance considerations.

11. There are four operation macros for the set. What are they?

Fd_zero (* Set) initializes the set S to an empty set

Fd_clr (S, * Set) deletes socket from set

Fd_isset (S, * Set) checks whether s is in the Set set

Fd_set (S, * Set) adds socket s to the set S

12. How does a SELECT statement operate on one or more socket handles?

* Use fd_zero to initialize the fd_set you are interested in.

* Use fd_set to allocate the socket to the fd_set to be used.

* Call the select function and set the timeout period.

* The Return Value of the select function indicates the data of the socket that has been waiting for success. when the data is returned, only the socket set with corresponding conditions is left. Use fd_isset to check whether the set contains sockets.

* Process data for sockets with corresponding events in each set.

* Return to Step 1 and initialize fd_set.

13. What are the restrictions on the fd_set structure? How can we break through this restriction?

The size of the fd_set structure fd_setsize is defined in winsock2.h and can contain only 64 sockets. To increase the value, define fd_setsize as the desired size before including winsock2.h.

14. What are the disadvantages of select when there are many sockets?

The select model uses three sets to check whether certain conditions are true. This means that if there are 1000 sockets, before calling select, use fd_set to add these 1000 sockets to the collection of interest. when an event occurs, check whether all sockets are in each collection, will affect the efficiency of the service program running.

Related Article

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.