Async Timeout events for TCP connections.
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Mark Smith
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 to this message...
 
    
Jared Parsons [MSFT] (VIP)
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]
Click here to reveal e-mail address

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" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.AsyncCallback
System.IAsyncResult
System.Net.IPAddress
System.Net.IPEndPoint
System.Net.Sockets.AddressFamily
System.Net.Sockets.ProtocolType
System.Net.Sockets.Socket
System.Net.Sockets.SocketType




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com