PC Review


Reply
Thread Tools Rate Thread

Async Timeout events for TCP connections.

 
 
Mark Smith
Guest
Posts: n/a
 
      16th Jun 2004
Hi ,

Is it possible in .Net to define a async timer callback. What I am
wanting to do is declare a time-out condition that gets executed
should the something timeout.

The reason I want to do this is to handle host being down that I am
trying to connect to using TCPIP. Then after a timeout automatically
try another host and just continue trying hosts until someone accepts
a connection, varying my timeout as I loop.

Function1 (string hostname)
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse(hostname, 9999);
IAsyncResult ar = sock.BeginConnect(iep, new
AsyncCallback(ConnectedMethod), sock);

Call another ASYNC routine (TryAnotherHost) that defines timer event
callback which if not cancelled executes within 15 seconds.
}

void ConnectedMethod(IAsyncResult iar)
{
Socket socket = (Socket)iar.AsyncState;
socket.EndConnect(iar);

cancel the timer???
<do all my normal stuff>
}

void TryAnotherHost (IAsyncResult iar)
{
Socket socket = (Socket)iar.AsyncState;
socket.EndConnect(iar);

socket.Close();
function1 ("another");
}

Is this possible and what routines should I review to achieve this.

Thanks

Mark
 
Reply With Quote
 
 
 
 
Jared Parsons [MSFT]
Guest
Posts: n/a
 
      16th Jun 2004
I'm not aware of a good way of setting a connect timeout. I'm not sure if
its possible or not.

Another option would be to asynchronously connect to multiple hosts at the
same time. Since you seem to have a list of available hosts and you just
need one that is working, this method should work for you. Do a
BeginConnect() on say 5 or so and the first one to come back successfully
wins so to speak. If any of the rest succeed then just close them. For
every one that fails while you still have not connected, create a new Socket
and call BeginConnect() for the next host

--
Jared Parson [MSFT]
(E-Mail Removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Mark Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi ,
>
> Is it possible in .Net to define a async timer callback. What I am
> wanting to do is declare a time-out condition that gets executed
> should the something timeout.
>
> The reason I want to do this is to handle host being down that I am
> trying to connect to using TCPIP. Then after a timeout automatically
> try another host and just continue trying hosts until someone accepts
> a connection, varying my timeout as I loop.
>
> Function1 (string hostname)
> {
> Socket sock = new Socket(AddressFamily.InterNetwork,
> SocketType.Stream,
> ProtocolType.Tcp);
> IPEndPoint iep = new IPEndPoint(IPAddress.Parse(hostname, 9999);
> IAsyncResult ar = sock.BeginConnect(iep, new
> AsyncCallback(ConnectedMethod), sock);
>
> Call another ASYNC routine (TryAnotherHost) that defines timer event
> callback which if not cancelled executes within 15 seconds.
> }
>
> void ConnectedMethod(IAsyncResult iar)
> {
> Socket socket = (Socket)iar.AsyncState;
> socket.EndConnect(iar);
>
> cancel the timer???
> <do all my normal stuff>
> }
>
> void TryAnotherHost (IAsyncResult iar)
> {
> Socket socket = (Socket)iar.AsyncState;
> socket.EndConnect(iar);
>
> socket.Close();
> function1 ("another");
> }
>
> Is this possible and what routines should I review to achieve this.
>
> Thanks
>
> Mark



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connect without async but get timeout error when async=true Joseph Microsoft ADO .NET 5 16th Jul 2008 10:13 PM
Way to specify timeout with async. thread operations? (BeginReceiv =?Utf-8?B?RGF2aWQ=?= Microsoft Dot NET Framework 0 10th Nov 2005 05:23 PM
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached. Guoqi Zheng Microsoft ASP .NET 4 3rd Jun 2004 07:39 PM
Timeout during Async SQLServer batch Alan Z. Scharf Microsoft ASP .NET 0 19th May 2004 09:30 PM
Timeout on an async file read Steve Terepin Microsoft C# .NET 0 14th Jan 2004 10:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:17 PM.