Non-Blocking UDP in VB.NET for beginners?

T

Tommy Long

I apologise in advance for this question has been answered at least in this
thread:

http://forums.msdn.microsoft.com/en-US/netfxnetcom/thread/9b7d3a8f-b964-45a7-ba7d-1a567f406757

Unfortunately I'm pretty new at all this and still can't get the jist.

Basically I've implemented a UDPClient.Send and Receive, run the program and
realised what was ment by 'blocking' when i read the MSDN article. My
program hangs, waiting to receive something.

As the title says, I'm looking for a way to achieve this without blocking,
probably as answered in the thread I've linked, but in an example format, or
at least easier than even lamens terms?

I know I'm being a pain, thus the apologies.

Here is an example of the blocking udpclient procedure I'm using at the
moment: (oUDPClient is currently declared globally while I tried to figure
things out...)
--------------------------------------------
Public Function HandleIncoming() As Boolean

Dim SendersIP As String
Dim ReceivedEx As String

Try
Dim receiveBytes As [Byte]() =
oUDPClient.BeginReceive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
SendersIP = RemoteIpEndPoint.Address.ToString()
ReceivedEx = returnData.ToString()
'Do stuff with ReceivedEx...
HandleIncoming = True

Catch ex As Exception
HandleIncoming = False
Exit Sub
End Try

End Function
--------------------------------------------
To add to the above, I'm using Visual Studio 2005 Standard Edition. I've
created a VB project and this networking stuff which I have no experience in,
is just a small, but unfortunately crucial part of it.

What I need the above procedure to do is trigger when an incoming
transmission is detected. The procedure can then 'select case' and react
depending on what it has received. All of which I'd like to do without the
application hanging/being blocked - so the user can go on using other
features whilst the UDPClient listens in the background.

I don't mind doing this with some sort of timer checking if there has been
something received, if there is no event that can be triggered available.
I'd prefer it if it didn't involve having to download someone's .DLL and
referencing that, but if thats the only way to do so then c'est la vie.

I've already had a look at two such methods, one provided by 'UnoLibs' and
another by the name of 'TinyServer'. Both of which do not appear to have
been written in Visual Studion 2005 and refer to lots of old/obsolete
namespaces which I've failed to update to be compliant/usable/error free, in
my VS05 SE.

Thanks in advance for any help you guys can provide.
 
T

Tommy Long

Ok, I believe I have worked out for myself how to get around the blocking
issue. Whether it is the correct way to do it, is yet to be determined.

Basically the Functions/Procedures involved in the UDP
transferring/receiving I've passed over to a worker, and started
multithreading. I had no idea I could multithread in VS05 so easily, so I'm
glad I've had this problem and learnt something of use.

My application at the moment, whilst loading up a login screen, attempts to
test the UDP connection. With my lack of knowledge in UDP/TCP I have no idea
if its even possible. But to test the connection I attempt to get the
computer to listen out on port ####, using my newly learnt worker. The main
thread then attempts to send a bytearray across the same port, addressed to
the local computers IP.

I was kind of hoping that the packet would be sent off to the router, and
the router would then bounce it back as the intended recipient is also the
source. With my lack of knowledge I don't even know if thats
allowed/possible.

The error I am getting is when the main thread attempts to send a packet, I
get the message:

"Cannot send packets to an arbitrary host while connected."

Now it is my understanding that UDP works without establishing a connection.
Its just sends something off and doesn't care where it ends up or whether it
arrives anywhere at all, whereas TCP sets up a connection, and verifies
everything? Is that about right?

However, reading through microsoft articles I see that their instances of
UDPClients use the .Connect method, so thats what I have done with both my
'listening instance, and my 'sending instance' of UDPClient. Due to the
order of things the main thread which handles the 'sending instance' is the
second to use the connect method, immediately after which it attempts to
send:

sUDPClient.Send(sendBytes, sendBytes.Length, RecipientIP, RecipientPort)

At the same time my worker thread is sat (blocked) on the line:

receiveBytes = rUDPClient.Receive(RemoteIpEndPoint)

It is the 'sUDPClient.Send.....' that throws the above error. Is anyone
able to tell me where I've gone wrong (other than "trying to program")?

Thanks in advance guys.

--
-------------------------------
Please respond to my posts via the newsgroup as the e-mail provided is not
monitored.


Tommy Long said:
I apologise in advance for this question has been answered at least in this
thread:

http://forums.msdn.microsoft.com/en-US/netfxnetcom/thread/9b7d3a8f-b964-45a7-ba7d-1a567f406757

Unfortunately I'm pretty new at all this and still can't get the jist.

Basically I've implemented a UDPClient.Send and Receive, run the program and
realised what was ment by 'blocking' when i read the MSDN article. My
program hangs, waiting to receive something.

As the title says, I'm looking for a way to achieve this without blocking,
probably as answered in the thread I've linked, but in an example format, or
at least easier than even lamens terms?

I know I'm being a pain, thus the apologies.

Here is an example of the blocking udpclient procedure I'm using at the
moment: (oUDPClient is currently declared globally while I tried to figure
things out...)
--------------------------------------------
Public Function HandleIncoming() As Boolean

Dim SendersIP As String
Dim ReceivedEx As String

Try
Dim receiveBytes As [Byte]() =
oUDPClient.BeginReceive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
SendersIP = RemoteIpEndPoint.Address.ToString()
ReceivedEx = returnData.ToString()
'Do stuff with ReceivedEx...
HandleIncoming = True

Catch ex As Exception
HandleIncoming = False
Exit Sub
End Try

End Function
--------------------------------------------
To add to the above, I'm using Visual Studio 2005 Standard Edition. I've
created a VB project and this networking stuff which I have no experience in,
is just a small, but unfortunately crucial part of it.

What I need the above procedure to do is trigger when an incoming
transmission is detected. The procedure can then 'select case' and react
depending on what it has received. All of which I'd like to do without the
application hanging/being blocked - so the user can go on using other
features whilst the UDPClient listens in the background.

I don't mind doing this with some sort of timer checking if there has been
something received, if there is no event that can be triggered available.
I'd prefer it if it didn't involve having to download someone's .DLL and
referencing that, but if thats the only way to do so then c'est la vie.

I've already had a look at two such methods, one provided by 'UnoLibs' and
another by the name of 'TinyServer'. Both of which do not appear to have
been written in Visual Studion 2005 and refer to lots of old/obsolete
namespaces which I've failed to update to be compliant/usable/error free, in
my VS05 SE.

Thanks in advance for any help you guys can provide.
 

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