I am developing a UDP Client PC application. It is supposed to receive UDP datagrams from more than 4 devices.
The system behaves in the following way:
- Multiple devices are communicating with each other via UDP broadcasts on a fixed port (11000) forming a personal area network with no connectivity to the internet.
- PC application is executed on a computer connected to the same network.
- PC application listens to UDP broadcasts on 11000 port to discover the devices.
- When a specific command is received from the PC application, that device goes into a different execution mode while other devices continue to broadcast their packets.
- This behaves in the desired manner when there is only one device in the personal area network.
I am facing a strange issue when there are two or more devices in the network, such that:
- I set the
endPoint
to the desired IPAddress and Port of the desired device using the discovered device list. - I call
myUDP.Receive(ref endPoint);
to receive UDP Datagram
This returns with the Datagram which was broadcasted by the second device in the network, rather than returning the response from the device with which I am trying to communicate. I have verified using the Wireshark that the response is sent from the device.
I tried looping through for a finite number of times to get the desired datagram.
// Some code which initializes the endPoint with desired IP Address and Port
...
// Some code which sends the data
...
// Some code which sets the IP Address of the device from which the response is expected
selectedIPAddress = IPAddress.Parse(labelIPAddressSettings.Text.Trim());
copyendPoint = endPoint;
// Listen to response
do
{
rexdDatagram = myUDP.Receive(ref endPoint);
if (endPoint.Address != selectedIPAddress)
{
// This datagram is not from the desired device
// Restore to the desired endpoint
endPoint = copyendPoint;
// Not sure if there is way to discard this enqueued datagram
}
i_timeout = i_timeout + 1;
if (i_timeout == 10)
{
// Datagram from the desired device has not been received
break;
}
// Not sure if the thread needs to sleep, debugging..
Thread.Sleep(1000);
} while (1);
Question: Is my code correct to loop within enqueued datagrams? Is there a way to discard previous datagrams and start afresh?
Thread.Sleep
or at least a very short sleeptime? And also, there is no code here that handles the case if it is the expected endpoint... – FildorremoteEP
on the methodUdpClient.Receive
isn't meant for specifying from which remote address to receive from, but rather to specify from which remote address something was received. Meaning you should always passnew IPEndPoint(IPAddress.Any, 0)
to it – MindSwipe