我有一个带有wiznet芯片的Atmega通过连接到我PC的linksys路由器发送UDP消息 . PC正在运行代码,打开并监听UDP端口1989上的通信 . 我在PC上也有Wireshark .

Wireshark将继续显示正在从微控制器接收消息,但是我的应用程序中的udpclient在某段时间后无法接收这些消息 . 大约40秒或80条消息将由应用程序接收消息,然后应用程序停止接收

如果我从应用程序发送数据,将再次收到相同持续时间的消息 .

我没有看到任何异常,UDPclient也没有关闭 . 它似乎返回到m_UDPClient.Receive函数并等待 .

代码如下

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);
        }
    }
}

表格代码

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();
        }
    }
}