How to Set connection timeout in socket (reprinting may be used)

Source: Internet
Author: User

Setting the connect timeout is simple. Some people have mentioned the use of select in csdn, but there is no satisfactory and complete answer. I am talking about the select function. This function is integrated in winsock1.1. To put it simply, "This function enables applications that want to avoid being locked during SOCKET call. Program To manage multiple sockets at the same time in an orderly manner (originally in Windows Network Programming Technology ). For usage and explanation, see Windows network programming technology.
Before using this function, you need to set the socket to non-lock mode so that the socket will be skipped immediately during connect. At the same time, a wsaewouldblock error is usually generated, this error does not matter. If you execute the SELECT statement again, the real timeout occurs.

 

Wsadata WSD;
Socket cclient;
Int ret;
Struct sockaddr_in server;
Hostent * Host = NULL;

If (wsastartup (makeword (2, 0), & WSD) {return 0 ;}
Cclient = socket (af_inet, sock_stream, ipproto_tcp );
If (cclient = invalid_socket) {return 0 ;}
// Set Recv and send time out
Int timeout = 6000; // you can specify 6 seconds for sending timeout.
If (: setsockopt (cclient, sol_socket, so_sndtimeo, (char *) & timeout, sizeof (timeout) = socket_error ){
Return 0;
}
Timeout = 6000; // set the receiving timeout value to 6 seconds.
If (: setsockopt (cclient, sol_socket, so_rcvtimeo, (char *) & timeout, sizeof (timeout) = socket_error ){
Return 0;
}
// Set non-blocking connection
Unsigned long ul = 1;
Ret = ioctlsocket (cclient, fionbio, (unsigned long *) & UL );
If (ret = socket_error) return 0;

// Connection
Server. sin_family = af_inet;
Server. sin_port = htons (25 );
Server. sin_addr. s_addr = inet_addr (lpcstr) psmtp );
If (server. sin_addr.s_addr = inaddr_none) {return 0 ;}

Connect (cclient, (const struct sockaddr *) & server, sizeof (server ));

// Select model, that is, set timeout
Struct timeval timeout;
Fd_set R;

Fd_zero (& R );
Fd_set (cclient, & R );
Timeout. TV _sec = 15; // The connection times out for 15 seconds.
Timeout. TV _usec = 0;
Ret = select (0, 0, & R, 0, & timeout );
If (Ret <= 0)
{
: Closesocket (cclient );
Return 0;
}
// It is difficult to control the non-lock mode. You can set the Back-to-blocking mode based on the actual situation.
Unsigned long ul1 = 0;
Ret = ioctlsocket (cclient, fionbio, (unsigned long *) & ul1 );
If (ret = socket_error ){
: Closesocket (cclient );
Return 0;
}

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.