The Select model of socket network programming

Source: Internet
Author: User
Tags function prototype set set
Select function Prototype: int select (int maxfdp1,fd_set *readset,fd_set *writeset, fd_set *exceptset,const struct timeval * timeout); Parameter explanation: Parameter 1: Maximum file description alphanumeric 1. Parameter 2: For readability, parameter 3: Check for writable, Parameter 4: To detect Out-of-band data, parameter five: A pointer to the TIMEVAL structure that determines the maximum time that a select waits for I/O. If you are empty, you will always wait.   If the value is set to {0,0}, the select returns immediately. Definition of Timeval structure: struct Timeval {long tv_sec;//seconds long tv_usec;//microseconds} return value explanation: >0: Positive number of the ready descriptor-1: Error 0: Timeout Select related macros: FD_CLR (S, *set): Deletes socket s from set.
Fd_isset (S, *set): Checks if S is a member of a set set; if the answer is yes, return true.
Fd_set (S, *set): Adds socket s to set set.
Fd_zero (* Set): Initializes the set to an empty collection. Use step: Suppose we want to know if we can safely read data from a socket without being bogged down in endless
Lock state, you can use the Fdset macro to assign your own socket to the Fdread collection, and then call Select. To
To detect whether your sockets are still part of the Fdread collection, use the Fd_isset macro. The following steps will be taken
You can complete the entire process of using Select to manipulate one or more socket handles:
1 use Fdzero macros to initialize a Fdset object;
2) using Fdset macros, the socket handle is added to the fdset set;
3 Call the Select function and wait for it to return ... when the select is complete, the total number of socket handles that are set in all Fdset collections is returned.
and update each collection accordingly.
4 Check the Fdset collection according to the return value of select and Fdisset macro.
5 The I/O process is known after "Pending" I/O operations in each collection.
Then go back to step 1 and continue with the select process. When the Select function returns, the FDSET structure is modified to remove the socket handles that do not have pending I/O operations.
That's why we use the Fdisset macro to determine whether a particular socket is still in the collection in the previous step (4).
Fd_set structure Explanation: struct Fd_set can be understood as a set, which holds a file descriptor (FileDescriptor), which is a file handle, which can be the common meaning of what we call a file, of course, under UNIX any device, pipeline, FIFO, etc. are file forms, all included, so there is no doubt that a socket is a file, the socket handle is a file descriptor.

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.