By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,178 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,178 IT Pros & Developers. It's quick & easy.

Connect a socket with timeout

P: n/a
How does one put a timeout on an attempt to connect a socket? There's no
timeout parameter to Socket.Connect(), and while there are socket options
for send and receive timeouts, there is none for connect timeout.
Apr 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Mike,

In order to do this, you will have to time the asynchronous event.
Basically, make a call to BeginConnect, and start a timer at the same time,
with an interval of your timeout.

In the timer function, set a flag indicating if the timer has fired.

Then, in the callback for the BeginConnect method, you check the flag.
If it is set, then you timed out, if not, then you haven't timed out.

You can then elaborate, by creating an Event that is set when the
timeout occurs, or the connection is created. Then, you have your thread
wait on both of those events. Depending on the event fired, you would then
know if there is a timeout, or if the connection succeeded.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike Schilling" <ap@newsgroup.nospam> wrote in message
news:Oy**************@TK2MSFTNGP15.phx.gbl...
How does one put a timeout on an attempt to connect a socket? There's no
timeout parameter to Socket.Connect(), and while there are socket options
for send and receive timeouts, there is none for connect timeout.

Apr 4 '06 #2

P: n/a

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u5**************@TK2MSFTNGP14.phx.gbl...
Mike,

In order to do this, you will have to time the asynchronous event.
Basically, make a call to BeginConnect, and start a timer at the same
time, with an interval of your timeout.

In the timer function, set a flag indicating if the timer has fired.

Then, in the callback for the BeginConnect method, you check the flag.
If it is set, then you timed out, if not, then you haven't timed out.
I thought about something like that, but couldn't find a way to abort the
connection attempt if the timer fires and it's still pending. EndConnect is
documented as synchronously completing the connection, and it's not clear
from the docs what Close or Shutdown would do.

You can then elaborate, by creating an Event that is set when the
timeout occurs, or the connection is created. Then, you have your thread
wait on both of those events. Depending on the event fired, you would
then know if there is a timeout, or if the connection succeeded.

Apr 4 '06 #3

P: n/a
Hi Mike,

The Close method will release all the resource on the Socket. Shutdown
method will disable the socket from sending and receiving. So, I suggest
you call Close method on the Socket to cancel connecting.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Apr 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.