C ++ Socket timeout settings

Source: Internet
Author: User

When using winsocket, sending () and recv () may fail due to network conditions or other reasons. You can set the sending and receiving time limit:
Int nNetTimeout = 1000; // 1 second
// Sending time limit
Setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & nNetTimeout, sizeof (int ));
// Receiving time limit
Setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & nNetTimeout, sizeof (int ));

But when our client connects to the server through the connect function, the timeout time is very long. How should we set the timeout time for connect? After checking the data, we found that the select method can query the status of the socket.
Fd_set rfd; // sets the descriptor to test whether the connection is available in struct timeval timeout; // The time struct FD_ZERO ($ rfd); // first clears the timeout of a descriptor set. TV _sec = 60; // seconds timeout. TV _usec = 0; // 1 million s, microsecond u_long ul = 1; // indicates non-blocking ioctlsocket (socket, FIONBIO, $ ul); // set to non-blocking connection

 

Next we will start connectconnect (socket, (SOCKADDR *) & addrClient, sizeof (SOCKADDR) FD_SET (sock, & rfd); Connect and add the socket to the descriptor rfb, in this way, you can test the socket. One descriptor can be added to multiple sockets and then run the select function to query ret = select (0, 0, & rfd, 0, & timeout); if (ret <= 0) {This indicates that the connection has timed out, and no connection is successful. Then you can perform corresponding processing in the program.} if the connection is successful within the timeout period, reset the socket to the blocking status as follows ul = 0; ioctlsocket (socket, FIONBIO, & ul); The recv and send operations can be performed normally below. By the way, the operation of sending data from multiple NICS is recorded. When the client actively connects to the server (TCP), it is not required by default. If the local ip address and port are used, the operating system will automatically allocate the port to you, then, the egress is automatically selected based on the route. But what should we do when you want to choose different NICs to send data based on different data. The answer is simple,
SOCKADDR_IN addrSelf; // local address addrSelf. sin_addr.s_addr = inet_addr ("192.168.1.110"); // specify the network adapter address addrSelf. sin_family = AF_INET;
AddrSelf. sin_port = htons (20000); // if (-1 = bind (sockClient [I], (SOCKADDR *) & addrSelf, sizeof (SOCKADDR ))) // forcibly bind the NIC address to Soket {bound successfully}

 

Reprinted please indicate the original connection www.cnblogs.com/gaoteng/p/3767967.html Snow Wolf's personal blog

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.