Re: Time out a connect() call
Re: Time out a connect() call
- Subject: Re: Time out a connect() call
- From: Peter Sichel <email@hidden>
- Date: Tue, 01 Mar 2011 09:23:55 -0500
On Feb 28, 2011, at 4:10 PM, Nathan Sims wrote:
> Is there some way to 'time out' a connect(2) call? I'm using a TCP/IP socket over an intranet and if I specify an IP address and port that I can't connect to, it takes 75 seconds to time out! If that could be reduced to 5 seconds or so it would be great.
You can time out or cancel most TCP calls indirectly by setting the socket to non-blocking mode and then creating a pipe to an "abort" socket that you include in your call to select(). select() supports a timeout parameter. To abort the select(), you simply write to the other end of the pipe so it wakes up. I wrap the whole thing in a TCP Socket class that supports connect with timeout. When the select() wakes up from timeout or abort, you then close the corresponding sockets.
The method is described in more detail in Stevens "UNIX Network Programming".
In my case, I'm actually scanning to determine whether a service is available, so the timeout is more important than waiting to open a connection which I'm not going to use anyway.
Enjoy!
- Peter
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden