I think everyone is very clear, Socket is the socket, in the interface programming, the timeout concept, we can think of 3: send timeout, receive timeout, and SELECT timeout (Note: The SELECT function is not only used for the socket, but it is more useful in the interface programming. When connect to the target host, this timeout is not set up. However, this timeout is very long, and connect is a blocking method. A host cannot connect, waiting for Connect to return to endure, your program is to try to connect multiple hosts, I am afraid I can't connect When the host, you can't stand it. I also talk less nonsense, let me talk about my method, if you think you have mastered this method, you don't have to look back, if you still don't know, I am willing to share with you. This article is an example of programs under Linux, but it is also the same, nothing more than a few function names.
There are two ways to set up the Connect settings in Linux. One is some of the parameters of the system, I don't talk about this method, because I can't tell: P, it is not programmed. Another method is to change the implementation of Connect timeout, I have to talk about this method, principle is like this: 1. Establish Socket 2. Set the socket to Non-blocking mode 3. Call Connect () 4. Use select () to check if the Socket descriptor can be written (note, it is writable) 5. Determine the connect () result 6 according to the result returned according to SELECT (). Set the socket to block mode (if your program does not need to use blocking mode, this step is saved, but in general, it is useful in blocking mode, which is easy to manage) If you are familiar with the network programming, I said this process, you know how to write your program, give you a program I wrote, for reference only.
/ **************************************** TIME OUT for connection () * Write by kerl w ******* ********************** /
#include
#define Time_Out_time 20 // Connect timeout time 20 seconds
INT main (int Argc, char ** argv) {................ int suckfd = socket (AF_INET, SOCK_STREAM, 0); if (SockFd <0) exit (1);
Struct sockaddr_in serv_addr; ......... // With server address filling structure serv_addr int error = -1, le; len = sizeof (int); TimeValTM; fd_set set;
Unsigned long ul = 1; ioctl (sockfd, fionbio, & ul); // set to non-blocking mode BOOL RET = false; if (connect (Sockfd, (Struct SockAddr *) & serv_addr, sizeof (serv_addr)) == -1) {TM.TV_SET = TIME_OUT_TIME; TM.TV_USET = 0; FD_SERO (& SET); FD_SET (SOCKFD, & SET); IF (SOCKFD 1, NULL, & SET, NULL, & TM)> 0) {GetSockOpt (SockFD, SOL_SOCKET , SO_ERROR, & ERROR, (Socklen_t *) & len; if (error == 0) RET = true; else ret = false;} else ret = false;} else ret = true;} else ret = true; ul = 0; ioctl (sockfd, fionbio, & ul); // Set to block mode
If (! RET) {Close (SOCKFD); FPRINTF (stderr, "cannot connect the server! / n"); return;} fprintf (stderr, "connection! / n"); // You can also make a package package Operation .............}
The above code snippet is for reference only, but also provides some prompts for beginners, and several functions, select, ioctl, getsockt can find relevant information. If I use it, I will not go out here, you only need to be in Linux Gently knock a Man
In addition, what I need to explain is that although we are set to non-blocking mode with IOCTL, but select itself is blocking, blocking time is its timeout time by calling SELECT, the last parameter TimeVal type variable The TimeVal structural variable pointed to the pointer is determined, the TimeVal structure consists of a member representing the number of seconds and a member representing the number of microseconds, usually we set the number of seconds, set the subtle number to 0 (Note: 1 second is equal to 1 million microseconds). Another parameter worth mentioning of the SELECT function is a variable pointer we use the fd_set type above. Before calling, this variable exists to check the descriptor to check with SELECT. After calling, it is a writable descriptor for the above program, we can use the macro fd_isset to check if a descriptor is there. Since I only have a socket descriptor here, I didn't use the fd_isset macro to check whether this sockfd is in Set after calling SELECT, in fact, it is necessary to add this judgment. However, I used getSockOpt to check, so it can be judged whether this set of interface is really connected, because we just change the use of select to check if it is connected, in fact, SELECT checks if it can be written, For written, it is a writable: 1) The number of available control bytes in the socket transmission buffer is equal to or equal to the current value of the low-tide limit of the socket transmission buffer is equal to the low tide limit of the socket transmission buffer. Or i) socket is connected, or ii) The socket does not require connection (UDP mode) 2) The connection is closed. 3) There is a set of interface errors to be processed. In this way, we need to use the getSockopt function to get some information about the current information to determine whether it is connected. It can give something wrong when it is not connected. Of course, I haven't marked so much in my program. State, just a simple representation of can be connected / unconnected. Let me talk about the results of this program test. I have tested three cases: 1. The normal situation of the target machine network can be connected to the target host, and can successfully apply a package job in a blocking method.
2. When the target machine network is disconnected, the display target host cannot be connected after the timeout time (20 seconds above the program) waits.
3. After the program is run, the target machine network is disconnected. After the timeout, the network is restored before the target host network is restored, after the program is restored, the program is restored, and the program shows the connection target host, and can be successfully carried out Send a package.
The test results of the above circumstances show that this method of setting the Connect timeout is completely feasible. I am in a single-time Connect to pack this type of library, in a monitoring system, so far, running is normal. This programmatic Connect timeout is a little bit of the way to modify the system parameters is that it is only for your program without affecting the system.