It is not original. It is copied from Baidu space.
The Select system call enables our program to monitor the State Changes of multiple file descriptors. The program stops at select and waits until one or more monitored file descriptors change their status.
The prototype of the select function is as follows:
Int select (INT NFDs, fd_set * readfds, fd_set * writefds, fd_set * limit TFDs, struct timeval * timeout );
The last timeout parameter of the function is obviously a timeout value. Its type is struct timeval *, which is a pointer to a variable in the struct timeval structure, so we need to declare a struct timeval TV in the program; then pass the TV variable address & TV to the select function. The structure of struct timeval is as follows:
Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds */
};
The three parameters 2nd, 3, and 4 are of the same type: fd_set *, that is, we need to declare several fd_set variables in the program, such as rdfds, wtfds, exfds, then, pass the address & rdfds, & wtfds, & exfds of the variable to the select function. These three parameters are a set of descriptors. The first rdfds is used to save such descriptors: When the descriptor state changes to readable, the system will tell the select function to return, similarly, the second wtfds indicates that when the descriptor state changes to writable, the system will tell the select function to return. Similarly, the third parameter exfds is a special case, that is, the system will tell the select function to return the statement when a special condition occurs on the description. In special cases, for example, the other Party sends emergency data through a socket descriptor. If we only want to detect
Whether the socket has data readable, we can do this:
Fd_set rdfds;/* declare a fd_set set to save the socket descriptor we want to detect */
Struct timeval TV;/* declare a time variable to save the time */
Int ret;/* Save the returned value */
Fd_zero (& rdfds);/* clears the set before using the select function */
Fd_set (socket, & rdfds);/* Add the descriptor socket to the Set */
TV. TV _sec = 1;
TV. TV _usec = 500000;/* set the maximum waiting time for select to 1 second plus 500 milliseconds */
Ret = select (socket + 1, & rdfds, null, null, & TV);/* checks whether the descriptor we set in the rdfds set above has readable information */
If (Ret <0) perror ("select");/* This indicates that the select function has an error */
Else if (ret = 0) printf ("timeout \ n");/* indicates that within the time range we set, the socket status has not changed */
Else {/* indicates that the wait time is less than 1 second plus 500 milliseconds, and the socket status has changed */
Printf ("ret = % d \ n", RET);/* The returned value of RET records the number of state change descriptors. Since we only monitor the socket descriptor, therefore, RET must be 1. If multiple descriptors change at the same time, the total number of descriptors is returned */
/* Here we should read data from the socket descriptor, because the select function has told us that the descriptor contains readable data */
If (fd_isset (socket, & rdfds) {/* first checks whether the descriptor monitored outside the socket is actually readable */
/* Read the data in the socket descriptor */
Recv (...);
}
}
Note that the first parameter of the select function is the maximum value of all descriptor values added to the set.
For example, we have created three descriptors:
Int SA, Sb, SC;
Sa = socket (...);/* create three descriptors respectively and connect them to the server */
Connect (SA ,...);
SB = socket (...);
Connect (SB ,...);
SC = socket (...);
Connect (SC ,...);
Fd_set (SA, & rdfds);/* add three descriptors to the read monitoring set respectively */
Fd_set (SB, & rdfds );
Fd_set (SC, & rdfds );
Before using the select function, you must find the maximum value among the three descriptors. We generally define a variable to save the maximum value and obtain the maximum socket value as follows:
Int maxfd = 0;
If (Sa> maxfd) maxfd = sa;
If (Sb> maxfd) maxfd = Sb;
If (SC> maxfd) maxfd = SC;
Then, call the select function:
Ret = select (maxfd + 1, & rdfds, null, null, & TV);/* Add 1 to the maximum value */
In the same way, if we want to check whether the user enters the input by keyboard, we should put the standard input 0 descriptor in the SELECT statement for detection, as shown below:
Fd_zero (& rdfds );
Fd_set (0, & rdfds );
TV. TV _sec = 1;
TV. TV _usec = 0;
Ret = select (1, & rdfds, null, null, & TV);/* Note that the maximum value must be 1 */
If (Ret <0) perror ("select");/* Error */
Else if (ret = 0) printf ("timeout \ n");/* the user did not press the keyboard during the specified time TV */
Else {/* the user has a keyboard to read the user's input */
Scanf ("% s", Buf );
}
Select requires several fd_set parameters to specify the socket you are concerned about:
Fd_clr (S, * Set)
Delete socket specified by S from fd_set structure: Set
Fd_isset (S, * Set)
Determine whether the socket S is in set. If the return value is not 0, it indicates that S is in set. Otherwise.
This is to determine whether the socket is readable/writable, or whether the status of the corresponding file descriptor is changed.
Fd_set (S, * Set)
Add socket s to set
Fd_zero (* Set)
Initialize set, set all 0