Results 1 to 3 of 3

Thread: UDP timeout help needed!!!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    16

    Question UDP timeout help needed!!!

    I am developing an application in VB.NET which uses the Sockets namespace - UDPClient(). I am having trouble as when I wait to receive some data from an ipaddress it just waits forever.

    I believe my problem is related to timeout and would like to know how best to set the value?

    My Example Code:

    Code:
    Dim addr As IPAddress = System.Net.IPAddress.Parse("169.1.1.1")
    Dim udpClient As New System.Net.Sockets.UdpClient()
    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Hello")
    Dim remoteIpEndPoint As New System.Net.IPEndPoint(addr.Address, 100)
    
    Try
       udpClient.Send(sendBytes, sendBytes.Length, remoteIpEndPoint)
    
        ' Blocks until a message returns on this socket from a remote host
        Dim receiveBytes As [Byte]() = udpClient.Receive(remoteIpEndPoint)
        Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
    
        Console.WriteLine(returnData.ToString())
    Catch e As Exception
        Console.WriteLine(e.ToString())
    End Try
    The above code works fine until the receiveBytes line where it just hangs until it receives some data, which could be forever. I need to say "Wait for 5 seconds and if nothing then continue".

    Can somebody please help????

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    According to MSDN Receive() blocks until it gets some data. Unless you can find a non-blocking method I think the only way is to start a new thread containing the Receive() command and kill it after five secods if it hasn't already terminated.
    This world is not my home. I'm just passing through.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Perhaps this would work...

    VB Code:
    1. Public Class MyUDP
    2.     Inherits System.Net.Sockets.UdpClient
    3.     Public Function TimeOutValue(ByVal value As Integer) As Boolean
    4.         Me.SettimeOut(value)      
    5.  
    6.        'or this
    7.        Dim r as System.Net.Sockets.Socket = Me.Client
    8.        r.SetSocketOption(Net.Sockets.SocketOptionLevel.Udp, Net.Sockets.SocketOptionName.ReceiveTimeout, value)
    9.        '
    10.     End Function
    11. End Class

    You would have to instantiate the class, connect, then set the time out, then call the receive method.
    Last edited by nemaroller; Sep 9th, 2003 at 12:08 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width