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.
========================================================== ===
Blocking and non-blocking
After the server enters the listen status, we will discuss two implementation methods:
1. fcntl Method
There are two ways to work on a file or device specified by a file descriptor: blocking and non-blocking. Blocking means that when you try to read and write the file descriptor, if nothing is readable at the time, or cannot be written at the moment, the program enters the waiting state until something is readable or writable. for non-blocking states, if nothing is readable or not writable, the read/write function returns immediately without waiting. by default, the file descriptor is blocked. when implementing the chat room, the server takes turns to query and establish a socket with each client. Once readable, the characters in the socket are read and sent to all other clients. in addition, the server also needs to check whether a new client tries to establish a connection at any time, so that if the server is blocked in any place, the content sent by other clients will be affected, the attempt by the new client to establish a connection is also affected. therefore, we use fcntl
The file descriptor becomes non-blocking:
Fcntl (sockfd, f_setfl, o_nonblock );
// Sockfd is the file descriptor to change the state.
// F_setfl indicates the state of the file descriptor to be changed
// O_nonblock indicates that the file descriptor is not blocked.
Use natural language to describe the chat room server:
While (1)
{
If a new connection then is established and the new connection is recorded;
For (all valid connections)
{
If the connection contains characters that can be read by then
{
For (all other valid connections)
{
Send the string to the connection;
}
}
}
}
Since it determines whether a new connection exists and whether the connection is readable is non-blocking, every time it is determined, no matter whether there is a new connection or not, it will return immediately. in this way, any client that sends characters to the server or tries to establish a new connection will not affect the activity of other clients. for the client, after establishing a connection, you only need to process two file descriptors, one is the socket descriptor that establishes the connection, and the other is the standard input. like server, if blocking is used, it is easy to affect the reading of another one because one of them has no input. therefore, convert them into non-blocking ones, and then the client performs the following actions:
While (do not want to exit)
{
If (the connection to the server is readable)
{
Read from the connection and output it to the standard output.
}
If (standard input readable)
{
Read from the standard input and output it to the connection with the server.
}
}
By analyzing the above programs, we can know that both the server and client continuously take turns to query each file descriptor, and read and process each file descriptor once it is readable. such programs are constantly being executed. As long as there are CPU resources, they will not be spared. therefore, the consumption of system resources is very large. when the server or client is executed separately, about 98% of the CPU resources are occupied. therefore, we can use another blocking method to solve this problem. This is select.
2. Select Method
In the select method, all file descriptors are blocked. select is used to determine whether a set of file descriptors contains a readable (write). If not, it is blocked until one is awakened. let's first look at the implementation of a relatively simple client:
Because the client only needs to process two file descriptors, it is necessary to determine whether there are two file descriptors that can be read and written:
Fd_zero (sockset); // clear sockset
Fd_set (sockfd, sockset); // Add sockfd to sockset
Fd_set (0, sockset); // Add 0 (standard input) to the sockset
Then the client handles the following:
While (do not want to exit)
{
Select (sockfd + 1, & sockset, null );
// At this time, the function will be blocked until there is a readable value in the standard input or sockfd.
// The first parameter is 0 and the maximum value in sockfd plus one
// The second parameter is the read set, that is, sockset.
// Third, the four parameters are the write set and the exception set, which are empty in this program.
// The Fifth parameter is the time-out period, that is, if the specified time is not readable, an error occurs.
// And return. When this parameter is null, the timeout value is set to an infinite length.
// When the SELECT statement is returned because it is readable, all that is included in the sockset statement is readable.
// File descriptors.
If (fd_isset (sockfd, & sockset ))
{
// Fd_isset the macro to determine whether sockfd is a readable file descriptor
Read from sockfd and output to standard output.
}
If (fd_isset (0, & sockset ))
{
// Fd_isset the macro to determine whether sockfd is a readable file descriptor
Read from standard input and output to sockfd.
}
Reset sockset. (To empty sockset and add sockfd and 0)
}
The following describes the server situation:
Set sockset as follows:
Fd_zero (sockset );
Fd_set (sockfd, sockset );
For (all valid connections)
{
Fd_set (userfd [I], sockset );
}
Maxfd = maximum file description symbol + 1;
Server:
While (1)
{
Select (maxfd, & sockset, null );
If (fd_isset (sockfd, & sockset ))
{
// There is a new connection
Create a new connection and add the connection descriptor to sockset.
}
For (all valid connections)
{
If (fd_isset (userfd [I], & sockset ))
{
// The connection contains characters that are readable.
Read characters from the connection and send them to other valid connections.
}
}
Reset sockset;
}
Because the select mechanism is used, when no characters are readable, the program is blocked, and the CPU resources are used to the minimum extent. When one server and several clients are executed on the same machine, the system load is only about 0.1. The original fcntl method is used to run only one server, and the system load can reach about 1.5. therefore, select is recommended.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/cation/archive/2007/05/03/1595753.aspx