Example of how to implement timeout connect () in Unix (Linux) C ++

Source: Internet
Author: User

Java connect has the timeout function, but C ++ connect does not? The following two versions are available:

 

Supports timeout connect ()/connect with timeout

Code:

 

 Void connect_w_to (void) {<br/> int res; <br/> struct sockaddr_in ADDR; <br/> long ARG; <br/> fd_set myset; <br/> struct timeval TV; <br/> int valopt; <br/> socklen_t Lon; </P> <p> // create socket <br/> SOC = socket (af_inet, sock_stream, 0); <br/> If (SOC <0) {<br/> fprintf (stderr, "error creating socket (% d % s)/n", errno, strerror (errno); <br/> exit (0 ); <br/>}</P> <p> ADDR. sin_family = af_inet; <Br/> ADDR. sin_port = htons (2000); <br/> ADDR. sin_addr.s_addr = inet_addr ("192.168.0.1"); </P> <p> // set non-blocking <br/> If (ARG = fcntl (SOC, f_getfl, null )) <0) {<br/> fprintf (stderr, "error fcntl (..., f_getfl) (% s)/n ", strerror (errno); <br/> exit (0); <br/>}< br/> Arg | = o_nonblock; <br/> If (fcntl (SOC, f_setfl, ARG) <0) {<br/> fprintf (stderr, "error fcntl (..., f_setfl) (% s)/n ", strer Ror (errno); <br/> exit (0 ); <br/>}< br/> // trying to connect with timeout <br/> res = connect (SOC, (struct sockaddr *) & ADDR, sizeof (ADDR )); <br/> If (RES <0) {<br/> If (errno = einprogress) {<br/> fprintf (stderr, "einprogress in connect () -selecting/N "); <br/> do {<br/> TV. TV _sec = 5 // overtime; <br/> TV. TV _usec = 0; <br/> fd_zero (& myset); <br/> fd_set (SOC, & myset); <br/> res = select (SOC + 1, null, & myset, null, & TV); <br/> If (RES <0 & errno! = Eintr) {<br/> fprintf (stderr, "error connecting % d-% s/n", errno, strerror (errno )); <br/> exit (0); <br/>}< br/> else if (RES> 0) {<br/> // socket selected for write <br/> Lon = sizeof (INT); <br/> If (getsockopt (SOC, sol_socket, so_error, (void *) (& valopt), & Lon) <0) {<br/> fprintf (stderr, "error in getsockopt () % d-% s/n", errno, strerror (errno); <br/> exit (0); <br/>}< br/> // check the V Alue returned... <br/> If (valopt) {<br/> fprintf (stderr, "error in delayed connection () % d-% s/n", valopt, strerror (valopt); <br/> exit (0); <br/>}< br/> break; <br/>}< br/> else {<br/> fprintf (stderr, "timeout in select ()-cancelling! /N "); <br/> exit (0); <br/>}< br/>}while (1 ); <br/>}< br/> else {<br/> fprintf (stderr, "error connecting % d-% s/n", errno, strerror (errno )); <br/> exit (0); <br/>}< br/> // set to blocking mode again... <br/> If (ARG = fcntl (SOC, f_getfl, null) <0) {<br/> fprintf (stderr, "error fcntl (..., f_getfl) (% s)/n ", strerror (errno); <br/> exit (0); <br/>}< br/> Arg & = (~ O_nonblock); <br/> If (fcntl (SOC, f_setfl, ARG) <0) {<br/> fprintf (stderr, "error fcntl (..., f_setfl) (% s)/n ", strerror (errno); <br/> exit (0 ); <br/>}< br/> // I hope that is all <br/>}

 

Simplified Version code:

Void connect_w_to (void) {<br/> int res, valopt; <br/> struct sockaddr_in ADDR; <br/> long ARG; <br/> fd_set myset; <br/> struct timeval TV; <br/> socklen_t Lon; </P> <p> // create socket <br/> SOC = socket (af_inet, sock_stream, 0); </P> <p> // set non-blocking <br/> Arg = fcntl (SOC, f_getfl, null); <br/> Arg | = o_nonblock; <br/> fcntl (SOC, f_setfl, ARG); </P> <p> // trying to connect with timeout <br /> ADDR. sin_family = af_inet; <br/> ADDR. sin_port = htons (2000); <br/> ADDR. sin_addr.s_addr = inet_addr ("192.168.0.1"); <br/> res = connect (SOC, (struct sockaddr *) & ADDR, sizeof (ADDR )); </P> <p> If (RES <0) {<br/> If (errno = einprogress) {<br/> TV. TV _sec = 15; <br/> TV. TV _usec = 0; <br/> fd_zero (& myset); <br/> fd_set (SOC, & myset); <br/> If (select (SOC + 1, null, & myset, null, & TV)> 0) {<br/> L On = sizeof (INT); <br/> getsockopt (SOC, sol_socket, so_error, (void *) (& valopt), & Lon); <br/> If (valopt) {<br/> fprintf (stderr, "error in connection () % d-% s/n", valopt, strerror (valopt )); <br/> exit (0); <br/>}< br/> else {<br/> fprintf (stderr, "timeout or error () % d-% s/n ", valopt, strerror (valopt); <br/> exit (0 ); <br/>}< br/> else {<br/> fprintf (stderr, "error connecting % D-% s/n ", errno, strerror (errno); <br/> exit (0 ); <br/>}< br/> // set to blocking mode again... <br/> Arg = fcntl (SOC, f_getfl, null); <br/> Arg & = (~ O_nonblock); <br/> fcntl (SOC, f_setfl, ARG); <br/> // I hope that is all <br/>}< br/>

From:

Http://www.developerweb.net/forum/showthread.php? P = 13486

 

 

This is another version found on the network. For details, refer:

 

 /* <Br/> * non-blocking connect () with timeout, Which is W. richard Steven s implementation <br/> */<br/> static int tcp_connect (unsigned int IP, unsigned short int port, <br/> unsigned int timeout) <br/>{< br/> struct sockaddr_in sin; <br/> int S, flags, error_value, N; <br/> fd_set rset, Wset; <br/> struct timeval TV; <br/> size_t error_value_len; <br/>/* <br/> * fill in sockaddr_in structure <br/> */<br/> memset (& sin, 0, sizeof (Sin); <br/> sin. sin_family = af_inet; <br/>/* <br/> * the two are all in the network byte sequence <br/> */<br/> sin. sin_addr.s_addr = IP; <br/> sin. sin_port = port; <br/> error_value = 0; <br/> error_value_len = sizeof (error_value); <br/> If (S = socket (pf_inet, sock_stream, 0) <0) <br/>{< br/> perror ("socket error"); <br/> return (-1 ); <br/>}< br/> If (flags = fcntl (S, f_getfl, 0) <0) <br/>{< br/> Perror ("fcntl (f_getfl) error"); <br/> goto tcp_connect_1; <br/>}< br/>/* <br/> * set the non-blocking mode <br/> */<br/> If (fcntl (S, f_setfl, flags | o_nonblock) <0) <br/>{< br/> perror ("fcntl (f_setfl) error"); <br/> goto tcp_connect_1; <br/>}< br/> If (n = connect (S, (struct sockaddr *) & sin, sizeof (SIN) <0) <br/> {<br/>/* <br/> * because of non-blocking sockets, the error <br/> */<br/> If (errno! = Einprogress) <br/>{< br/>/* <br/> * connect error: connection refused <br/> * perror ("Connect error"); <br/> */<br/> goto tcp_connect_1; <br/>}< br/>/* <br/> * you can insert other code and wait for connect () in parallel () complete <br/> */<br/> If (n = 0) <br/> {<br/>/* <br/> * even if the socket is not blocked, connect () may also return <br/> */<br/> goto tcp_connect_0; <br/>}< br/> fd_zero (& rset ); <br/> fd_set (S, & rset); <br/> Wset = Rset; <br/> TV. TV _sec = timeout; <br/> TV. TV _usec = 0; <br/> N = select (S + 1, & rset, & Wset, null, timeout? & TV: NULL); <br/> If (n <0) <br/> {<br/> perror ("select error"); <br/> goto tcp_connect_1; <br/>}< br/> else if (n = 0) <br/>{< br/>/* <br/> * fprintf (stderr, "select returned with timeout. <br/> "); <br/> */<br/> goto tcp_connect_1; <br/>}< br/> else if (fd_isset (S, & rset) | fd_isset (S, & Wset) <br/>{< br/> If (getsockopt (S, sol_socket, so_error, & error_value, <br/> & err Or_value_len) <0) <br/>{< br/> goto tcp_connect_1; <br/>}< br/> else <br/> {<br/> fprintf (stderr, "some error occur in tcp_connect () <br/> "); <br/> goto tcp_connect_1; <br/>}< br/> tcp_connect_0: <br/> If (fcntl (S, f_setfl, flags) <0) <br/>{< br/> perror ("fcntl (f_setfl) error"); <br/> goto tcp_connect_1; <br/>}< br/> If (error_value! = 0) <br/>{< br/>/* <br/> * error: Connection refused <br/> * fprintf (stderr, "error: % S <br/> ", strerror (error_value); <br/> */<br/> goto tcp_connect_1; <br/>}< br/> n = 1; <br/> setsockopt (S, sol_socket, so_keepalive, & N, sizeof (n); <br/> return (s); <br/> tcp_connect_1: <br/> close (s); <br/> return (-1); <br/>}/ * end of tcp_connect */

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.