Asked by:
UdpClient fails to receive messages

Question
-
My code below for listening to incoming UDP-messages in UWP just stopped...
using System; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.Net; using System.Net.Sockets; using System.Collections.Generic; using System.Threading; public void StartListen() { Task.Run(() => MyUdpClient()); } async void MyUdpClient() { try { Debug.WriteLine("Start listening.."); var udpClient = new UdpClient() { ExclusiveAddressUse = false, EnableBroadcast = true }; IPEndPoint myEndPoint = new IPEndPoint(IPAddress.Any, 50000); udpClient.Client.Bind(myEndPoint); while (true) { var receivedResult = await udpClient.ReceiveAsync(); string packet = Encoding.ASCII.GetString(receivedResult.Buffer).Trim(); Debug.WriteLine(packet); } } catch (Exception ex) { Debug.WriteLine("\n MyUdpClient() " + ex.Message); } }
The setup is to listen to a navigation Wi-Fi box on a boat that sends out sensor data (typically 20-30 bytes long) via the UDP-protocol using a fixed port 50000. Everything worked as charm until recently when the reading just stopped and the code "hangs" on ReceivedAsync...
I have been able to conclude the following:
- The code works fine in an Xamarin.Forms app running on Android
- I have been able to reproducing the same error in my office between two PCs connected to the same router (one for send and one for listening)
- I have double checked UWP capability settings and and made sure "Client & server" for both Internet and Private networks are checked
Please help
Anything wrong the port and security..?
receiing I have a pice of simple code receiving UDP packets from a Wi-Fi box
- Moved by Xingyu ZhaoMicrosoft contingent staff Tuesday, September 22, 2020 8:22 AM
Thursday, September 17, 2020 2:59 PM
All replies
-
Hi Southbranch,
Thank you for posting here.
It may be due to the device's fault or wrong configuration.
Make sure it is not blocked by the firewall and that the IP address is correct.
Another reference you can need: UDP client not receiving data
Hope it could be helpful.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Friday, September 18, 2020 9:12 AM -
Thanks for your reply.
The device works fine with a another commerical app on my Android device, so it must be either my PC's network settings or my own C# code. Please note, this code just worked fine until recently..
I have also tried various other code implementations with both sync/async setups using classes such as Socket or DatagramSocket, but the results are same - no messages are received..
Friday, September 18, 2020 9:57 AM -
Hi Southbranch,
Thanks for your feedback.
>>this code just worked fine until recently
The problem may related to your network.
You can consider posting your question in Developer Community forum for more efficient responses.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Xingyu ZhaoMicrosoft contingent staff Monday, September 21, 2020 3:17 AM
Monday, September 21, 2020 3:16 AM -
Thanks,
It turned out to be an security issue in Windows 10.
First I made a complete reset on my PC but it did not work. Then I ran through most of the recommendations given here., it did not work. Then I turned off the Windows firewalls for both Private and Public networks (tried before). Then it suddenly started working again (!) but after a while stopped again… Now UDP-message receiving seem to come back and forth periodically so it still remains a mystery..
I am not sure if it relates or, but the wi-fi device is listed/categorized as an “Unidentified network” on my PC (cannot change) while most other networks are classified as either "Private" or "Public". I know the Windows 10 treats these networks differently from a security perspective.
Monday, September 28, 2020 9:10 AM