|
-
Sep 8th, 2003, 11:33 PM
#1
Thread Starter
Junior Member
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????
-
Sep 9th, 2003, 07:43 AM
#2
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.
-
Sep 9th, 2003, 11:49 AM
#3
I wonder how many charact
Perhaps this would work...
VB Code:
Public Class MyUDP
Inherits System.Net.Sockets.UdpClient
Public Function TimeOutValue(ByVal value As Integer) As Boolean
Me.SettimeOut(value)
'or this
Dim r as System.Net.Sockets.Socket = Me.Client
r.SetSocketOption(Net.Sockets.SocketOptionLevel.Udp, Net.Sockets.SocketOptionName.ReceiveTimeout, value)
'
End Function
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|