Here is some really clunky code that illustrates, I couldn't find the code I
eventually used, I think I used a WaitCallback or something similar. But, it
illustrates the technique:
// When the timeout for waiting for the answer is
// reached it sends a byte, causing an exception therefore terminating the
// connection.
public class TimedUdpClient : UdpClient
{
private int timeout = 1000; // 1 sec default
private Thread timeoutWatchdog;
public int Timeout
{
get { return timeout; }
set { timeout = value; }
}
public new byte[] Receive(ref IPEndPoint remote)
{
byte [] ret;
timeoutWatchdog = new Thread(new ThreadStart(StartWatchdog));
timeoutWatchdog.Start();
try
{
ret = base.Receive(ref remote);
}
catch (SocketException)
{
ret=null;
}
finally
{
timeoutWatchdog.Abort();
}
return ret;
}
private void StartWatchdog()
{
Thread.Sleep(timeout);
this.Send(new byte[] {0x00},1,"",8000);
}
}
--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
"Kevin" wrote:
Thanks for your reply.
But i don't under know how can i do it using a delegate, do you have an
example? =)
Thanks
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:46**********************************@microsof t.com... This code:
s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout,
1000);
would set the timeout on a send (there is similar for receive)
However it would not control the connect timeout. For that, you might need
to use a delegate with a timeout that closes the socket separately.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
"Kevin" wrote:
Hi
How can i decrease the timeout on Socket.Connect??
Thanks =)