# Socket Connect Timeout

**URL:** <https://forums.digi.com/t/socket-connect-timeout/3912>\
**Category:** NET+OS\
**Tags:** socket\
**Created:** [February 26, 2004, 10:41pm UTC](https://forums.digi.com/t/socket-connect-timeout/3912 "2004-02-26T22:41:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![egawtry](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@egawtry](https://forums.digi.com/u/egawtry)\
**Post date:** [February 26, 2004, 10:41pm UTC](https://forums.digi.com/t/socket-connect-timeout/3912/1 "2004-02-26T22:41:29Z")

</div>

How do I set the socket connect timeout? Normally there is a setsockopt() called SO\_CONNECT\_TIME, but that seems to be missing. A three minute wait (default) is a bit silly.

Thanks,  
-Erik

---

<div class="post-metadata">

**Author:** ![jwormsley](https://avatars.discourse-cdn.com/v4/letter/j/3da27b/32.png) [@jwormsley](https://forums.digi.com/u/jwormsley)\
**Post date:** [August 11, 2006, 8:14pm UTC](https://forums.digi.com/t/socket-connect-timeout/3912/2 "2006-08-11T20:14:37Z")

</div>

Two years and no one has answered this?

Anyway, anyone got a good solution? Two minutes is a long time to wait to find out the external host for a connect isn’t out there.

---

<div class="post-metadata">

**Author:** ![bellonep](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@bellonep](https://forums.digi.com/u/bellonep)\
**Post date:** [August 16, 2006, 11:53am UTC](https://forums.digi.com/t/socket-connect-timeout/3912/3 "2006-08-16T11:53:13Z")

</div>

You can use the setsockopt to set the various options. Additionally when opening the port you can pass it the O\_NONBLOCK option. I.E. open(/com/0/, O\_RDWR | O\_NONBLOCK). This will allow the connect to return immediately and then you can check back with a select to see if the connection completed.

---

<div class="post-metadata">

**Author:** ![jwormsley](https://avatars.discourse-cdn.com/v4/letter/j/3da27b/32.png) [@jwormsley](https://forums.digi.com/u/jwormsley)\
**Post date:** [August 16, 2006, 1:31pm UTC](https://forums.digi.com/t/socket-connect-timeout/3912/4 "2006-08-16T13:31:31Z")

</div>

My current code is something like this:

sock = socket (AF\_INET, SOCK\_STREAM, 0);  
setsockopt(sock, SOL\_SOCKET, SO\_BIO, (char \*)&not\_used, sizeof(not\_used));  
server.sin\_family = AF\_INET;  
server.sin\_port = myParams.Host\_Port;  
server.sin\_addr.s\_addr = htonl(DNSgethostbyname((char \*)&myParams.Host\_Addr));  
if (connect (sock, &server, sizeof(server)) \>= 0)  
{  
do something…  
}

Are you suggesting something more like this?

timeval timeout = {5,0}; // 5 second timeout  
sock = socket (AF\_INET, SOCK\_STREAM, 0);  
setsockopt(sock, SOL\_SOCKET, SO\_NBIO, (char \*)&not\_used, sizeof(not\_used));  
server.sin\_family = AF\_INET;  
server.sin\_port = myParams.Host\_Port;  
server.sin\_addr.s\_addr = htonl(DNSgethostbyname((char \*)&myParams.Host\_Addr));

err = connect (sock, &server, sizeof(server));  
while (err == some\_value\_that\_says\_still\_working)  
err = select(set\_size, &read\_set, &write\_set, &exception\_set, &timeout);  
if (err == some\_value\_that\_says\_connected)  
{  
setsockopt(sock, SOL\_SOCKET, SO\_BIO, (char \*)&not\_used, sizeof(not\_used));  
do something…  
}

So, what would I put for:

some\_value\_that\_says\_still\_working  
set\_size  
read\_set  
write\_set  
exception\_set  
some\_value\_that\_says\_connected

Sorry, I’m not at all familiar with socket level programming, and none of the example apps do anything like this.

Thanks,  
Jeff.
