UDP Receive Asynchronously
I am trying to asynchronously receive UDP commands from my other program. I am using the code below which works great, once. But if i resend the command, it doesn't receive it.
How do I continuously receive UDP packets?
vb.net Code:
Private Sub startListening()
ReceivingUdpClient = New System.Net.Sockets.UdpClient(listenPort)
ThreadReceive = New System.Threading.Thread(AddressOf receiveMessages)
ThreadReceive.Start()
End Sub
Private Sub receiveMessages()
Dim receiveBytes As [Byte]() = ReceivingUdpClient.Receive(RemoteIpEndPoint)
MsgBox(Encoding.ASCII.GetString(receiveBytes))
'the end goal is a Case but message box for debugging
End Sub
Re: UDP Receive Asynchronously
Since the Receive method blocks until something arrives and the fact that it's in a worker thread, I believe all you need to do is wrap the receive in a do loop. I'm still kind of learning this stuff, so if nobody posts saying I'm not right, it means I'm learning real good.
VB Code:
Private Sub receiveMessages()
Do
Dim receiveBytes As [Byte]() = ReceivingUdpClient.Receive(RemoteIpEndPoint)
MsgBox(Encoding.ASCII.GetString(receiveBytes))
'the end goal is a Case but message box for debugging
Loop
End Sub
Re: UDP Receive Asynchronously