How to Set connection timeout in socket

Source: Internet
Author: User

How to Set connection timeout in socket
(Published by antghazi at 9:28:38)

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.ProgramTo 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 ; // Set sending timeout to 6 seconds
If (: Setsockopt (cclient, sol_socket, so_sndtimeo ,( Char   * ) & Timeout, Sizeof (Timeout )) = Socket_error) {
Return 0;
}
Timeout = 6000 ; // Set receiving timeout for 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 ; // Connection Timeout: 15 seconds
Timeout. TV _usec = 0 ;
RET = Select ( 0 , 0 , & R, 0 , & Timeout );
If (Ret <=   0 )
{
: Closesocket (cclient );
Return 0;
}
// Generally, the non-lock mode is difficult to control. 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.