Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. socket timeout

socket timeout

Scheduled Pinned Locked Moved C#
helptutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rupel
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • R Rupel

      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

      L Offline
      L Offline
      Luis Alonso Ramos
      wrote on last edited by
      #2

      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 1 Reply Last reply
      0
      • L Luis Alonso Ramos

        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 Offline
        R Offline
        Rupel
        wrote on last edited by
        #3

        ...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

        R D 2 Replies Last reply
        0
        • R Rupel

          ...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

          R Offline
          R Offline
          Rupel
          wrote on last edited by
          #4

          just 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

          1 Reply Last reply
          0
          • R Rupel

            ...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

            D Offline
            D Offline
            Daniel Turini
            wrote on last edited by
            #5

            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

            R 1 Reply Last reply
            0
            • D Daniel Turini

              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

              R Offline
              R Offline
              Rupel
              wrote on last edited by
              #6

              hey, this looks cool. will try it tomorrow :) although i will try SocketOptionLevel.Udp instead of Tcp first ;) thx anyway. :) :wq

              R 1 Reply Last reply
              0
              • R Rupel

                hey, this looks cool. will try it tomorrow :) although i will try SocketOptionLevel.Udp instead of Tcp first ;) thx anyway. :) :wq

                R Offline
                R Offline
                Rupel
                wrote on last edited by
                #7

                at first, both SocketOptionLevel.Udp and SocketOptionLevel.Tcp raise an exception when being used with SocketOptionName.ReceiveTimeout. it seems you have to use SocketOptionLevel.Socket instead. but that even doesn't help. the Receive()-function still blocks. i don't know what that ReceiveTimeout is for :( :wq

                R 1 Reply Last reply
                0
                • R Rupel

                  at first, both SocketOptionLevel.Udp and SocketOptionLevel.Tcp raise an exception when being used with SocketOptionName.ReceiveTimeout. it seems you have to use SocketOptionLevel.Socket instead. but that even doesn't help. the Receive()-function still blocks. i don't know what that ReceiveTimeout is for :( :wq

                  R Offline
                  R Offline
                  Rupel
                  wrote on last edited by
                  #8

                  now 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 stop Receive() from blocking and this way it works. :) thx to everyone that helped. (also the ones that read, thought and had no idea) :wq

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups