Select () usage of non-blocking socket programming in Linux

Source: Internet
Author: User

Select is still important in Socket programming, but it is not very popular for beginners of Socket programming, they are just used to writing blocking programs such as connect, accept, recv, or recvfrom (the so-called blocking method block, as the name suggests, means that the process or thread must wait for an event to occur when executing these functions, if the event does not occur, the process or thread will be blocked and the function cannot return immediately ). However, using Select can achieve non-blocking (the so-called non-block method means that the process or thread does not have to wait for the event to execute this function. Once executed, it will definitely return, different return values are used to reflect the execution of the function. If the event occurs in the same way as the blocking method, if the event does not occur, a code is returned to inform the event that the event has not occurred, while the process or thread continues to execute, so it is more efficient), it can monitor the changes in the file descriptor we need to monitor read/write or exception. The following is a detailed introduction!

Select Function Format (I am talking about Berkeley socket programming in Unix, which is different from that in windows. I will explain it later ):

Int select (int maxfdp, fd_set * readfds, fd_set * writefds, fd_set * errorfds, struct timeval * timeout );

Two struct types are described first:

First, struct fd_set can be understood as a collection in which the file descriptor (file descriptor) is stored, that is, the file handle. This can be what we call a common object, of course, any device, pipeline, and FIFO in Unix are all in the file format, so there is no doubt that a socket is a file, and a socket handle is a file descriptor. The fd_set set can be operated manually by some macros, such as clearing the set FD_ZERO (fd_set *) and adding a given file descriptor to the set FD_SET (int, fd_set *), deletes a given file descriptor from the collection FD_CLR (int
, Fd_set *), check whether the specified file descriptor in the set can read and write FD_ISSET (int, fd_set *). For example.

Second, struct timeval is a common structure used to represent the time value. It has two members: one is the number of seconds and the other is the number of milliseconds.

Specific interpretation of select parameters:

Int maxfdp is an integer that indicates the range of all file descriptors in the collection. That is, the maximum value of all file descriptors is 1. It cannot be wrong! In Windows, the value of this parameter does not matter. You can set it incorrectly.

Fd_set * readfds is a pointer to the fd_set structure. This collection should contain file descriptors. We want to monitor the read changes of these file descriptors, that is, we are concerned about whether data can be read from these files. If a file in this collection is readable, select returns a value greater than 0, indicating that the file is readable, if there is no readable file, the timeout parameter is used to determine whether to time out. If the time exceeds the timeout time, select returns 0. If an error occurs, a negative value is returned. You can pass in a NULL value, indicating that you do not care about any file read changes.

Fd_set * writefds is a pointer to the fd_set structure. This collection should contain file descriptors. We want to monitor the write changes of these file descriptors, that is, we are concerned about whether data can be written to these files. If there is a file in this collection that can be written, select will return a value greater than 0, indicating that a file is writable, if no writable file exists, the timeout parameter is used to determine whether to time out. If the time exceeds the timeout time, select returns 0. If an error occurs, a negative value is returned. You can pass in a NULL value, indicating that you do not care about any file write changes.

Fd_set * errorfds indicates the intention of the two parameters above, which is used to monitor file error exceptions.

Struct timeval * timeout is the select timeout time. This parameter is crucial. It can make the select statement in three States. First, if NULL is input as an input parameter, that is, the time structure is not input, that is, the select statement is placed in the blocking state. It must wait until a file descriptor in the collection of monitored file descriptors changes. Second, if the time value is set to 0 s and 0 ms, it becomes a pure non-blocking function. no matter whether the file descriptor has changed or not, it immediately returns to continue execution. If the file does not change, 0 is returned, and a positive value is returned. Third, the value of timeout is greater than 0, this is the waiting timeout time, that is, the select statement is blocked within the timeout time, and an event will be returned within the time-out time. Otherwise, no matter how long it will be returned, the return value is the same as the above.

Return Value:

Negative value: select error positive value: some files can be read or written, or errors 0: wait for timeout, there is no readable or wrong file

With select, you can write a decent network program! A simple example is to accept data from the network and write it into a file.

Example:

Main ()

{

Int sock;

FILE * fp;

Struct fd_set fds;

Struct timeval timeout = {3, 0}; // select wait 3 seconds, 3 seconds round-robin, set to 0 if it is not blocked

Char buffer [256] = {0}; // receiving buffer of 256 bytes

/* Assume that a UDP connection has been established, and the specific process is not written or simple. Of course, the same applies to TCP. The host ip address and port are already specified, and the file to be written has already been opened.

Sock = socket (...);

Bind (...);

Fp = fopen (...);*/

While (1)

{

FD_ZERO (& fds); // clear the set for each loop. Otherwise, the descriptor changes cannot be detected.

FD_SET (sock, & fds); // Add Descriptor

FD_SET (fp, & fds); // same as above

Maxfdp = sock> fp? Sock + 1: fp + 1; // Add 1 to the maximum value of the descriptor

Switch (select (maxfdp, & fds, & fds, NULL, & timeout) // select

{

Case-1: exit (-1); break; // select error, exit the program

Case 0: break; // Round Robin again

Default:

If (FD_ISSET (sock, & fds) // test whether sock is readable, that is, whether there is data on the network.

{

Recvfrom (sock, buffer, 256,...); // accept Network Data

If (FD_ISSET (fp, & fds) // test whether the file can be written

Fwrite (fp, buffer...); // write a file

Buffer clearing;

} // End if break;

} // End switch

} // End while

} // End main

----------------------------------------------------------
Linux select ()

Linux select () Details select system calls are used for our program to monitor the State Changes of multiple file handles (file descriptor. The program stops at select and waits until one or more of the monitored file handles change their status.

There are a lot of files in the handle in Linux. If you use a man function, when the return value of the function is said to be successful, a file handle is created. For example, man socket can see "On success, a file descriptor for the new socket is returned. "man 2 open can see" open () and creat () return the new file descriptor ". In fact, the file handle is an integer. You can see the socket function declaration:
Int socket (int domain, int type, int protocol );
Of course, the most familiar handles are 0, 1, and 2. 0 indicates the standard input, 1 indicates the standard output, and 2 indicates the standard error output. 0, 1, and 2 are integers. The corresponding FILE * structure is represented by stdin, stdout, stderr, 0 is stdin, 1 is stdout, and 2 is stderr.
For example, the following two sections of Code read 9 bytes from the standard input:

# Include <stdio. h>
# Include <unistd. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char buf [10] = "";
Read (0, Buf, 9);/* read characters from the standard input 0 */
Fprintf (stdout, "% s \ n", Buf);/* output stdout write characters to the standard */
Return 0;
}
/*** The code above and below can be used to read the 9 characters entered by the user from the standard input ***/
# Include <stdio. h>
# Include <unistd. h>
# Include <string. h>
Int main (INT argc, char ** argv)
{
Char Buf [10] = "";
Fread (BUF, 9, 1, stdin);/* read characters from the standard input stdin */
Write (1, Buf, strlen (BUF ));
Return 0;
}
The preceding SELECT statement is used to monitor the status changes of one or more handles. 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 collection of handles. The first rdfds is used to save such a handle: When the handle status changes to readable, the system will tell the select function to return the handle, similarly, the second wtfds indicates that the system will tell the select function to return a result when the handle state is changed to writable. Similarly, the third parameter exfds is a special case, that is, when a special case occurs on the handle, the system will tell the select function to return. In special cases, for example, the other Party sends emergency data through a socket handle. If we only want to check whether data in a socket is readable, we can:

Fd_set rdfds;/* declare an fd_set set to save the socket handle to be detected */
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 socket of the handle to be detected to the Set */
TV. TV _sec = 1;
TV. TV _usec = 500;/* set the maximum waiting time for select to 1 second plus 500 milliseconds */
Ret = select (socket + 1, & rdfds, null, null, & TV);/* checks whether the handle set in rdfds 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-changing handles. Since we only monitor the socket handle, therefore, ret must be 1. If multiple handles change at the same time, the total number of handles is returned */
/* Here we should read data from the socket handle, because the select function has told us that the handle contains data readable */
If (FD_ISSET (socket, & rdfds) {/* first checks whether the handle monitored outside the socket is actually readable */
/* Read data in the socket handle */
Recv (...);
}
}
Note that the first parameter of the select function is the maximum value of all the handle values that are added to the set. For example, we have created three handles:
************************ ********************
* Filename: One-Step Linux Network Programming-select details
* Purpose: Describes the usage of select in detail.
* Wrote by: zhoulifa (zhoulifa@163.com) Zhou Lifa (http://zhoulifa.bokee.com)
Linux enthusiasts Linux knowledge disseminators sohowhose developers are best at C Language
* Date time:
* Note: Anyone can copy the code and use these documents, including your commercial use.
* But follow the GPL
* Thanks to: Google
* Hope: more and more people are expected to contribute to the development of science and technology.
* Technology stands on the shoulders of giants and makes progress faster! Thank you for your contributions to the open source team!
**************************************** *****************************/

Int sa, sb, SC;
Sa = socket (...);/* create three handles respectively and connect them to the server */
Connect (sa ,...);
Sb = socket (...);
Connect (sb ,...);
SC = socket (...);
Connect (SC ,...);

FD_SET (sa, & rdfds);/* add three handles 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 handles. 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 the keyboard, we should put the handle of the standard input 0 in the SELECT statement to check, 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 );
}----------------------------------------------------------
Linux select Study Notes

The Select system call is used for our program to monitor the State Changes of multiple file descriptors (File descr extends ptor. The program stops at select and waits until one or more monitored file descriptors change their status. The Select () mechanism provides an fd_set data structure, which is actually a long array. Each array element can be associated with an open file descriptor (whether it is a socket descriptor, or other files, named pipelines, or device descriptors. The programmer completes the connection establishment. When select () is called, the content of fd_set is modified by the kernel according to the IO status, in this way, the socket or file of the select () process is notified to be readable,

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 rfds, wfds, and efds.

In addition, there is a set of standard macro definitions for fd_set variables to process such variables:

FD_ZERO (fd_set * fdset): clears the contact between fdset and all file descriptors.

FD_SET (int fd, fd_set * fdset): Establishes the connection between the file descriptor fd and fdset.

FD_CLR (int fd, fd_set * fdset): clears the contact between the file descriptor fd and fdset.

FD_ISSET (int fd, fd_set * fdset): checks whether the file descriptor fd associated with fd_set can be read and written.> 0 indicates that the file descriptor fd can be read and written.

(For the definition of fd_set and related macros, see/usr/include/sys/types. h) The three defined parameters are the set of descriptors. The first rfds is used to save such descriptors: When the descriptor state changes to readable, the system will tell the select function to return, the second wfds indicates that when the descriptor State turns to writable, the system will tell the select function to return. The third parameter efds is special, that is, when a special condition occurs on the descriptor, the system will tell the select function to return. The following uses an input as an example to describe:

Int fd1, fd2;/* define two descriptors */

Fd1 = socket (...);/* Create a socket connection */

Fd2 = open ("/dev/tyS0", O_RDWR);/* open a serial port */

FD_ZERO (& rfds);/* clears the set before using the select function */

FD_SET (fd1, & rfds);/* Add two descriptors to the read monitoring set respectively */

FD_SET (fd2, & rfds );

Int maxfd = 0;

Maxfd = (fd1> fd2 )? (Fd1 + 1): (fd2 + 1);/* Add 1 to the maximum value */

Ret = select (maxfd, & rfds, NULL, NULL, & TV);/* Then call the select function */

In this way, you can use a switch statement to determine which input source is in the input data. The specific judgment is as follows:

Switch (ret ){

Case-1: perror ("select");/* This indicates that the select function has an error */

Case 0: printf ("timeout \ n");/* indicates that the socket status has not changed during the specified time */

Default:

If (FD_ISSET (fd1, & rfds) processing function 1 ();/* socket has data */

If (FD_ISSET (fd2, & rfds) processing function 2 ();/* ttyS0 has data */

}

 

Search by network:

In Linux, the select call process is as follows:

1. the user-layer application calls select (), and the underlying calls poll ())

2. The core layer calls sys_select () ------> do_select ()

Finally, call the poll function of struct file_operations * f_op for the struct file type variable corresponding to the file descriptor fd.

The function pointed to by poll returns the read/write information.

1) if read/write is enabled, read/write information is returned.

2) if read/write is not available, the process will be blocked, wait for the driver to wake up, call the poll function again, or return a response timeout.

3. the driver needs to implement the poll function.

When the driver finds that data can be read and written, it notifies the core layer and the core layer to re-call the function query information pointed to by poll.

Poll_wait (filp, & wait_q, wait) // Add the current process to the waiting queue, but it is not blocked.

Use wake_up_interruptible (& wait_q) to wake up the waiting queue during interruption

----------------------------------------------------------
The use of select () in Socket programming
 
Those who have used WinSock APIs know that WinSock programming is very convenient.
Whether it is the underlying API WSAAsyncSelect () or the asynchronous Socket class of MFC:
CAsyncSocket provides messages such as FD_ACCEPT, FD_READ, and FD_CLOSE.
For programmers to capture and process. FD_ACCEPT notifies the process of a client Socket request connection,
The local Socket of the FD_READ notification process is readable, And the FD_CLOSE notification process is
Disable. So, is BSD Socket really inferior?
 
None! 'Cause cpu love unix so.
 
In bsd unix, a system calls the select () command to provide a similar message-driven mechanism.
The cpu solemnly announces that the WSAAsyncSeclet () of WinSock is only the fork version of this select!
 
Bill is also from fork, xixi.
 
The select () mechanism provides an fd_set data structure, which is actually a long array,
Each array element can be associated with an open file handle (whether it is a Socket handle or other
File, named pipe, or device handle) to establish a contact. The programmer completes the process of establishing a contact,
When select () is called, the content of fd_set is modified by the kernel according to the IO status.
Which Socket or file is readable for the select () process:
 
# Include <sys/types. h>
# Include <sys/times. h>
# Include <sys/select. h>
 
Int select (nfds, readfds, writefds, limit TFDs, timeout)
Int nfds;
Fd_set * readfds, * writefds, * required TFDs;
Struct timeval * timeout;
 
Ndfs: the number of file handles monitored by the select statement, depending on the number of files opened in the process.
The maximum file number plus one.
Readfds: a collection of readable file handles monitored by select.
Writefds: a set of writable file handles monitored by select.
Except TFDs: a collection of abnormal file handles monitored by select.
Timeout: the timeout end time of this select () operation. (See/usr/sys/select. h,
Accurate to one second per million !)
 
When the image file in readfds or writefds is readable, writable, or times out, this select ()
Returns the result. Programmers can use the macros provided by a set of systems to determine when select () ends.
Which file is readable or writable. Readfds is particularly useful for Socket programming.
Several related macros are explained as follows:
 
FD_ZERO (fd_set * fdset): clears the contact between fdset and all file handles.
FD_SET (int fd, fd_set * fdset): Establishes the connection between the file handle fd and fdset.
FD_CLR (int fd, fd_set * fdset): clears the contact between the file handle fd and fdset.
FD_ISSET (int fd, fdset * fdset): Check whether the file handle fd associated with fdset is
It can be read and written.> 0 indicates that it can be read and written.
(For definition of fd_set and related macros, see/usr/include/sys/types. h)
 
In this way, your socket only needs to be read when there is something to read, roughly as follows:
 
...
Int sockfd;
Fd_set fdR;
Struct timeval timeout = ..;
...
For (;;){
FD_ZERO (& fdR );
FD_SET (sockfd, & fdR );
Switch (select (sockfd + 1, & fdR, NULL, & timeout )){
Case-1:
Error handled by u;
Case 0:
Timeout hanled by u;
Default:
If (fd_isset (sockfd )){
Now u read or Recv something;
/* If sockfd is father and
Server socket, u can now
Accept ()*/
}
}
}
 
Therefore, an FD_ISSET (sockfd) is equivalent to a FD readable notification.
For the struct timeval function, use man select. Different timeval settings
Enable select () to display three features: timeout termination, no timeout blocking, and round robin. Because
Timeval can be accurate to one second per million, so Windows SetTimer () is not
What. You can use select () to make a super clock.
 
What is the implementation of FD_ACCEPT? Still above, because the client will send
The Connection Request message. At this time, select () will certainly end. FD_ISSET (sockfd) is of course large.
At zero, because there is a message readable! As for this application, it mainly lies in the parent of the service provider.
Socket, if you do not like the active accept (), you can change it to the above mechanism to accept ().
 
As for the implementation and processing of FD_CLOSE, it takes a lot of cpu processing time and is not finished yet.
 
--
This article discusses how to use select () to detect Socket shutdown by the other Party:
 
The local Socket is still readable, because when the Socket of the other party is closed, a closed connection will be sent.
The notification message will be immediately detected by select. TCP connection (three-way handshake) and off
For more information about the closed (secondary handshake) mechanism, see related books on TCP/IP.
 
For some reason, UNIX does not seem to have provided a notification to the process about Socket or Pipe being disabled by the other party.
Signal, or the cpu is limited. In short, when the other party closes, one executes recv () or read (),
Return-1 immediately. The value of the global variable errno is 115, and the corresponding sys_errlist [errno]
Connect refused (see/usr/include/sys/errno. h ). Therefore
In the for (;)... select () block, when something is readable, check recv () or
When the return value of read () is-1, the local Socket is shut down; otherwise, select () will
I always think that there is something to read, and the result is that the cpu has been heartbroken to cut the pin. If you don't believe it, try: No Check
Query recv () to return the result, and write the received result (not actually received) to the standard output...
Similar problems also occur in programming of famous pipelines. For details, refer to release as: release a useful
The source code of the Socket Client.
 
As for the processing that the other party suddenly closes when writing the Socket, it can simply capture the signal SIGPIPE and perform
The corresponding process of shutting down the local Socket and so on. SIGPIPE is interpreted as writing a non-reader pipeline.
We will not repeat it here. Please refer to man signal.
 
The above is the experience accumulated by the cpu in the TCP/IP data transmission experiment. If there is any error or omission, please kill it.
 
Alas, yesterday, a bunch of grandsons in the hacker area had almost no short circuits. Ren cpu (Pentium Core) z80
 
In addition, the application of select in asynchronous (non-blocking) connect was just started with socket programming.
I have always used blocking-type connect. The problem with non-blocking connect is that proxy scan was used at the time.
However
Through online communication with netizens and related FAQs, I finally learned how to solve this problem.
Using select can solve this problem well. The general process is as follows:
 
1. Set the opened socket to non-blocking. You can use fcntl (socket, F_SETFL, O_NDELAY)
(Some systems can also use FNEDLAY ).
 
2. Send the connect call. At this time,-1 is returned, but errno is set to EINPROGRESS, meaning that connect is still
The process is not completed yet.
 
3. Set the opened socket to the monitored writable (note that it is not readable) file set and use select for monitoring,
If it can be written, use
Getsockopt (socket, SOL_SOCKET, SO_ERROR, & error, sizeof (int ));
To get the error value. If it is zero, connect is successful.
 
In many unix versions of proxyscan programs, you can see similar processes, in addition to the essence of solaris
Area-> there is a general connect module with timeout parameters in programming skills.

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.