Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I change the connect() timeout?

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I'm testing some code that connects & sends data to a server on the network, but when I test connecting to a non-existing server I have to wait a long time before it returns an error about not being able to connect to the server.

To speed up my testing, is there a way to lower the timeout before connect() returns an error?
 
Hi

This is not something I'm familiar with but I had a scout around on the web for you and I found this forum thread that may help you. They seem to be able to specify the timeout in seconds to sockets. It seems that they setup an array of sockets to read or write to and from and then use connect(). I don't how much help, if any, this will be but perhaps it’s worth taking a look at.


Andrew
 
Thanks, but I don't think the select() function helps until after you run connect(). I'm just trying to reduce the timeout on the actual call to connect() before it gives up.
 
I found another forum thread that also suggests using connect() on a socket and then use the timeout with select() to cut down the timeout period. The original question is almost exactly what you posted. Here is the link to the thread:


I know it's a java forum but I think they are talking about C/C++.

Hope this helps
Andrew
 
Yeah, I was hoping there was a way to do it without changing it to an unblocking call... I don't want to make too many changes to the code that I'm testing.
If I get some time, I guess I could give it a try.
Thanks.
 
Some distressing addition:
Some of the blocking Winsock functions (e.g. connect())
have a timeout embedded into them. The theory behind this is that only
the stack has all the information necessary to set a proper timeout.
Yet, some people find that the value the stack uses is too long for
their application; it can be a minute or longer.

Under Winsock 2, you can set the SO_SNDTIMEO and
SO_RCVTIMEO options with setsockopt() to change the
timeouts for send() and recv().

Unfortunately, the Winsock spec does not document a way to change
many other timeout values, and the above advice doesn't apply to
Winsock 1.1.

The solution is to avoid blocking sockets altogether. All of the
non-blocking socket methods lend themselves to timeouts:
...
(from
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top