none
UDP Client Stops Receiving Data while Wireshark still sees it RRS feed

  • Question

  • I have an Atmega with a wiznet chip sending UDP Messages through a linksys router connected to my PC. The PC is running code that opens and listens for communication on UDP port 1989. I also have Wireshark on the PC.

    Wireshark will continue to show that messages are being received from the Microcontroller, but the udpclient in my application fails to receive these messages after sometime. Around 40 seconds or 80 messages the message will be received by the application and then the application stops receiving

    If I send data out of the application, the messages will be received once again for the same duration.

    I am not seeing any exceptions nor does the UDPclient get closed. it seems to return to the m_UDPClient.Receive function and wait.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.Net.Sockets;
    using System.Net;
    
    namespace SecurityServiceLibrary
    {
        public class SecurityService
        {
            private UdpClient m_UDPClient;
    
            public Action<String> DiagnosticMessageFunction;
    
            private IPEndPoint m_Nodes = new IPEndPoint(IPAddress.Broadcast, 1989);
            private IPAddress m_LocalIPAddress;
    
            private Task m_Listener;
    
            public SecurityService(IPAddress LocalIPAddress)
            {
                m_LocalIPAddress = LocalIPAddress;
    
                m_UDPClient = new UdpClient(new IPEndPoint(m_LocalIPAddress, 1989));
                m_UDPClient.EnableBroadcast = true;
    
                m_Listener = new Task(Listen);
                m_Listener.Start();
            }
            private async void Listen()
            {
                DiagnosticMessage("Start Listening");
                while (true)
                {
                    IPEndPoint Sender = new IPEndPoint(0,0);
                    List<byte> byMessage = m_UDPClient.Receive(ref Sender).ToList();
                    if (!Sender.Address.Equals(m_LocalIPAddress))
                    {
                        ParseMessage(byMessage);
                        DiagnosticMessage(Sender.Address.ToString() + " Message: " + BitConverter.ToString(byMessage.ToArray()));
                    }
                }
            }
            private void ParseMessage(List<Byte> byMessage)
            {
                if (byMessage.Count >= 7)
                {
                    List<Byte> MAC_Address = byMessage.GetRange(0, 6);
                    Byte MessageID = byMessage[6];
    
                    switch (MessageID)
                    {
                        case 0x01:
    
                            break;
    
                        case 0x02:
    
                            break;
                    }
                }
            }
    
            public void RequestAllStatus()
            {
                byte[] Message = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x81 };
                m_UDPClient.Send(Message, Message.Length,m_Nodes);
            }
    
    
            private void DiagnosticMessage(String Message)
            {
                DiagnosticMessageFunction?.Invoke(Message);
            }
        }
    }

    Form Code

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    using System.Net;
    
    using SecurityServiceLibrary;
    
    
    namespace SecurityServiceGUI
    {
        public partial class Form1 : Form
        {
            SecurityService m_securityService = new SecurityService(IPAddress.Parse("192.168.1.10"));
    
            public Form1()
            {
                InitializeComponent();
                m_securityService.DiagnosticMessageFunction = AddToDiagnosticMessage;
            }
    
            private void AddToDiagnosticMessage(String Message)
            {
                AddToListBox(listBox_Diagnostic, DateTime.Now.ToString() +": "+ Message);
            }
    
            private void AddToListBox(ListBox listBox, String Message)
            {
                if (listBox.InvokeRequired)
                {
                    this.Invoke(new Action<ListBox,String>(AddToListBox), new object[] { listBox, Message });
                }
                else
                {
                    if(listBox.Items.Count > 20)
                    {
                        listBox.Items.RemoveAt(0);
                    }
                    listBox.Items.Add(Message);
                }
            }
    
            private void button_RequestAllStatus_Click(object sender, EventArgs e)
            {
                m_securityService.RequestAllStatus();
            }
        }
    }

    Monday, December 11, 2017 3:01 PM

All replies

  • Hello Schmidty1913,

    >>Around 40 seconds or 80 messages the message will be received by the application and then the application stops receiving

    According to your description, the server side has indeed send the data to client side. But it based on UDP protocol, which is referred to as unreliable datagram protocol. In your case I suggest you use TCP client rather than UDP client. It's based on the reliability transmission and it need client send the response to ensure the package has finished. And you could get more detailed information about TCP.

    https://en.wikipedia.org/wiki/Transmission_Control_Protocol

    Best regards,

    Neil Hu


    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.

    Tuesday, December 12, 2017 10:17 AM
    Moderator