473,385 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

UDPClient.Receive Issue

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 :(

--
-------------------------------
Please respond to my posts via the newsgroup as the e-mail provided is not
monitored.
Jul 30 '08 #1
0 1487

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Morten Overgaard | last post by:
Hi I'm listening on the SysLog port (514) through UDP. The problem is that I am not receiving anything nut I know that i get messages on the port. When I use KIWI to listen on the same port via...
2
by: Richy Rich | last post by:
Hi, I've developed an application in C# which spawns a thread to receive datagrams. If I use the socket receive method, there are no problems when using the application under NT or XP. I...
1
by: Dan Kelley | last post by:
I have 2 projects - 1 Winform project that sends Udp messages using the UdpClient class when a button is clicked, and a Console application that listens for these Udp messages. If I try to use...
2
by: Rick | last post by:
Hi, I'm trying to get a simple UdpClient app working. I've been looking at the MSDN info regarding UdpClient. When I set it up on my own PC and send messages to myself it works OK. If I try to...
0
by: Steve | last post by:
I am looking into UdpClient sample code from MSDN where one UdpClient object used for both send and receive operation. Please take a look at the link below...
2
by: Emilio | last post by:
Question about UdpClient sample ' This constructor arbitrarily assigns the local port number. Dim udpClient As New UdpClient() Try udpClient.Connect("www.contoso.com", 11000) ' Sends a...
3
by: D. André Dhondt | last post by:
In VB.NET 2003, is there a way to create a System.Net.Sockets.UDPClient to listen to any address AND any port? I can get it to listen to any address, but only if I specify a port (for example,...
7
by: | last post by:
I have the following program using System; using System.Collections; using System.Net.Sockets; using System.Net; using System.IO; public class Test { public static void Main(string s) {
0
by: Johan | last post by:
Suppose: There are two devices on my network that both broadcast UDP datagrams (and they both use the same portnumber 1234). I want to create two executables that use UdpClient internally. Each...
0
by: Karl Durrance | last post by:
Hi all, I am having a weird issue with sending and receiving multicast messages either using the UDPClient object or directly with sockets. Normally, my application works perfectly, receiving...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.