socket timeout
-
hi, i'm using udp on sockets and wonder how to set a timeout on receiving? haven't found anything on that issue... sometimes it happens, that there is no connection or packets get lost and i really need a configurable timeout here. possible workaround would be to have the calling thread install a timer, and if the child-thread isn't done with his communication-things, he's aborted (-9 so to say ;)). but it would be nicer if there is a timer somewhere within the socket-internals... any idea? :wq
-
hi, i'm using udp on sockets and wonder how to set a timeout on receiving? haven't found anything on that issue... sometimes it happens, that there is no connection or packets get lost and i really need a configurable timeout here. possible workaround would be to have the calling thread install a timer, and if the child-thread isn't done with his communication-things, he's aborted (-9 so to say ;)). but it would be nicer if there is a timer somewhere within the socket-internals... any idea? :wq
Rüpel wrote: possible workaround would be to have the calling thread install a timer, and if the child-thread isn't done with his communication-things, he's aborted If the thread is blocked in UdpClient.Receive() or UDP Socket.ReceiveFrom(), then calling Abort on the thread will not stop it until the blocked method returns. To stop it, after calling Thread.Abort, you have to send a datagram to yourself for the thread to unblock. Then it will be aborted. -- LuisR ────────────── Luis Alonso Ramos Chihuahua, Mexico www.luisalonsoramos.com "Do not worry about your difficulties in mathematics, I assure you that mine are greater." -- Albert Einstein
-
Rüpel wrote: possible workaround would be to have the calling thread install a timer, and if the child-thread isn't done with his communication-things, he's aborted If the thread is blocked in UdpClient.Receive() or UDP Socket.ReceiveFrom(), then calling Abort on the thread will not stop it until the blocked method returns. To stop it, after calling Thread.Abort, you have to send a datagram to yourself for the thread to unblock. Then it will be aborted. -- LuisR ────────────── Luis Alonso Ramos Chihuahua, Mexico www.luisalonsoramos.com "Do not worry about your difficulties in mathematics, I assure you that mine are greater." -- Albert Einstein
...you have to send a datagram to yourself for the thread to unblock... i've tried, but it doesn't seem to work. :( what's wrong with this little console-application?
[STAThread] static void Main(string[] args) { Class1 foo = new Class1(); foo.MainLoop(); } public void MainLoop() { Thread listener = new Thread(new ThreadStart(Listen)); listener.Start(); UdpClient sender = new UdpClient(); IPEndPoint self = new IPEndPoint(IPAddress.Loopback,7789); sender.Send(new byte [] {0x01},1,self); while (listener.IsAlive) ; } void Listen() { UdpClient ear = new UdpClient(); IPEndPoint remote = new IPEndPoint(IPAddress.Any,0); ear.Receive(ref remote); Console.WriteLine("Got some Input from {0}",remote); }
:wq -
...you have to send a datagram to yourself for the thread to unblock... i've tried, but it doesn't seem to work. :( what's wrong with this little console-application?
[STAThread] static void Main(string[] args) { Class1 foo = new Class1(); foo.MainLoop(); } public void MainLoop() { Thread listener = new Thread(new ThreadStart(Listen)); listener.Start(); UdpClient sender = new UdpClient(); IPEndPoint self = new IPEndPoint(IPAddress.Loopback,7789); sender.Send(new byte [] {0x01},1,self); while (listener.IsAlive) ; } void Listen() { UdpClient ear = new UdpClient(); IPEndPoint remote = new IPEndPoint(IPAddress.Any,0); ear.Receive(ref remote); Console.WriteLine("Got some Input from {0}",remote); }
:wqjust a little bumpage ;), in case someone thinks: "hey this is just a 'question-answer-thx'-thread and i don't have to look at it" :rolleyes: if that gets fixed, i'm ready to post my first article here on codeproject. so could anyone please give me an idea? :rose: :wq
-
...you have to send a datagram to yourself for the thread to unblock... i've tried, but it doesn't seem to work. :( what's wrong with this little console-application?
[STAThread] static void Main(string[] args) { Class1 foo = new Class1(); foo.MainLoop(); } public void MainLoop() { Thread listener = new Thread(new ThreadStart(Listen)); listener.Start(); UdpClient sender = new UdpClient(); IPEndPoint self = new IPEndPoint(IPAddress.Loopback,7789); sender.Send(new byte [] {0x01},1,self); while (listener.IsAlive) ; } void Listen() { UdpClient ear = new UdpClient(); IPEndPoint remote = new IPEndPoint(IPAddress.Any,0); ear.Receive(ref remote); Console.WriteLine("Got some Input from {0}",remote); }
:wqTry this: (for a 10 seconds timeout) ear.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.ReceiveTimeout, 10000); "In an organization, each person rises to the level of his own incompetence." Peter's Principle
-
Try this: (for a 10 seconds timeout) ear.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.ReceiveTimeout, 10000); "In an organization, each person rises to the level of his own incompetence." Peter's Principle
-
hey, this looks cool. will try it tomorrow :) although i will try
SocketOptionLevel.Udp
instead of Tcp first ;) thx anyway. :) :wqat first, both
SocketOptionLevel.Udp
andSocketOptionLevel.Tcp
raise an exception when being used withSocketOptionName.ReceiveTimeout
. it seems you have to useSocketOptionLevel.Socket
instead. but that even doesn't help. theReceive()
-function still blocks. i don't know what thatReceiveTimeout
is for :( :wq -
at first, both
SocketOptionLevel.Udp
andSocketOptionLevel.Tcp
raise an exception when being used withSocketOptionName.ReceiveTimeout
. it seems you have to useSocketOptionLevel.Socket
instead. but that even doesn't help. theReceive()
-function still blocks. i don't know what thatReceiveTimeout
is for :( :wqnow i did a 'hack' that works for me. :)
public class TimedUdpClient : UdpClient
{
private int timeout = 1000; // 1 sec default
private Thread timeoutWatchdog;/// /// Get/Set the receiving timeout in milliseconds (1000 = 1sec) /// public int Timeout { get { return timeout; } set { timeout = value; } } public new byte\[\] Receive(ref IPEndPoint remote) { timeoutWatchdog = new Thread(new ThreadStart(StartWatchdog)); timeoutWatchdog.Start(); try { byte\[\] ret = base.Receive(ref remote); return ret; } catch (SocketException) { return null; } } private void StartWatchdog() { Thread.Sleep(timeout); this.Send(new byte\[\] {0x00},1,"",8000); }
}
my little test-program from two or three postings above didn't receive the packets since they have to be sent on the same socket (at least i believe that). when this byte from my watchdog is sent, there's an exception ("connection closed by remote" or something like that) raised somewhere inside
UdpClient.Receive()
. it would be nicer, if it would just receive the byte and one could distinguish by the sender, if that packet was sent by the watchdog (thus, on the same socket) or comes from outside. UDP is a connectionless protocol, so i'm not sure, how such a connection-closed-excpetion may occur, but actually it does :wtf: anyways. all i wanted, was stopReceive()
from blocking and this way it works. :) thx to everyone that helped. (also the ones that read, thought and had no idea) :wq