try

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xairoy
    New Member
    • Jan 2010
    • 1

    try

    Hallo,
    I am developing an application where there is a source which sends UDP datagrams at a constant Interval (2 datagrams per second). I am receiving these datagrams in the following code:

    Code:
    EP = New IPEndPoint(IPAddress.Any, clsUDP.nListenOnPort)  
    
    (' clsUDP.nListenOnPort=6466)
    
    UDPInput = New UdpClient(EP)
    While clsMisc.ListenforUDP
      Try
    
    byt = UDPInput.Receive(EP)
    ..................
     Catch ex As Exception
            MessageBox.Show(ex.ToString)
      End Try
    End While
    
     UDPInput.Close()
    
    UDPInput = Nothing
    EP = Nothing
    the flag clsMisc.Listenf orUDP is true as long as the application runs. This is set to false when an user Logs off.
    This is working properly when a user logs in and logs off three times consecutively. But when an user logs in fourth time. I found out by debugging that the program hangs
    after
    byt = UDPInput.Receiv e(EP)
    this line. I have also tried with asynchronous connection (Beginreceive and end receive) and also with
    UDPInput.Client .SetSocketOptio n(SocketOptionL evel.Socket, SocketOptionNam e.ReceiveBuffer , 0) to flush the buffer before I read the byt but in vain. It is always the fourh time that the problem arises.

    I am trying this to solve for the last 3 days without success. I shall be grateful if somebody helps me.
    Thanks.
    Roy
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Let me be clear: I do C#, not VB so some code translation on your part may be required.

      Your code logic seems to be quite close to my own for UDP datagrams
      Code:
              public void StartListener()
              {
                  IPAddress IPlisten = new IPAddress(new byte[] { 127, 0, 0, 1 });
                  if(ListenIP != null) IPAddress.TryParse(ListenIP, out IPlisten);
                  IPEndPoint groupEP = new IPEndPoint(IPlisten, ListenPort);
                  done = false;
      
                  try
                  {
                      while (!done)
                      {
                          Console.WriteLine("Waiting for broadcast");
                          byte[] bytes = listener.Receive(ref groupEP);
                          ReceivedMsg(bytes); // Raise custom event with received bytes
                      }
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine(e.ToString());
                  }
                  finally
                  {
                      if (listener != null) listener.Close();
                  }
              }
      My line 13 would correspond to your line 9 where the received packet is assigned to a byte array.

      I'm not sure what you mean by "hangs up after this line". It is the nature of the .Receive method to stop and wait for an incoming packet.

      I run my UDP Listener on its own thread because it halts execution. When a packet is received I then raise a custom event ("ReceivedMs g") with the byte array that came in. Then the loop (lines 10-15) iterates and the .Receive again halts all execution until another packet is received.

      I hope this helps.

      Comment

      Working...