UDPClient.Receive Issue

T

Tommy Long

Ok, I'm not all that great with VB.Net, I admit that early on. Used to get
on well with VB6 but we can't all live in the past I'm told. :)

Basically, the following is a sample procedure, which is run by a background
worker (multi-threading is new to me...).

The procedure is supposed to get my worker thread to start listening on the
specified port. Slightly after the worker thread begins listening, the main
thread attempts to send a byte array on the same port, addressed to the local
machine (I have no idea whether its possible to send something to myself, I
kind of envisioned the packets being sent off to the router and bouncing back
to me).

I'm only sending stuff to myself to 'test' the connection, the application
will hopefully send off to other computers on the network once I get this
working.

The code is:


Public Function UDPHandleCommands(ByVal worker As
System.ComponentModel.BackgroundWorker, _
ByVal e As System.ComponentModel.DoWorkEventArgs) As Boolean

Dim receiveBytes As [Byte]()
Dim rUDPClient As New UdpClient
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim returnData As String = String.Empty
Dim SendersIP As String = String.Empty
Dim ReceivedEx As String = String.Empty

Try
Do
'this is the line I'll refer to as the .Receive line
receiveBytes = rUDPClient.Receive(RemoteIpEndPoint)
returnData = Encoding.ASCII.GetString(receiveBytes)
System.Threading.Thread.Sleep(250)
Loop Until worker.CancellationPending = True Or Not returnData =
""

SendersIP = RemoteIpEndPoint.Address.ToString()
ReceivedEx = returnData.ToString()
Catch ex As Exception
Return False
Exit Function
End Try

'other stuff would go on below if this worker thread actually
received anything

The problem I get is after sitting on the the .Receive line (look at the
code, there's a comment above the line I mean) for a short period, I get an
exception in the immediate window, though the worker sits on that line and
doesn't budge.

The exception in the immediate window is:
"A first chance exception of type 'System.InvalidOperationException'
occurred in System.dll"

The .Receive line is inside a 'Try' as you can see, but at no point do I
catch anything.

It was my understanding that if I send something to that port, when it has
finished receiving it will unblock itself and move on to the next line, which
in my case will convert the received packets into something more readable
hopefully.

However, I have sat F8'ing every line of code up until the worker gets to
receive, and then watched the main thread send something off on that port,
and not have any problems (despite me using obsolete code to resolve hostname
from IP address).

Can someone please tell me where I'm going wrong? I spent ages getting this
application to work as far as it does, really annoying that I can't work out
whats wrong with the above line :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top