从文本框中提取的多个端口上的 C# UDP 侦听器

本文关键字:侦听器 UDP 文本 提取 | 更新日期: 2023-09-27 18:36:08

我试图到处搜索这个问题,但无济于事。我试图完成的是我在文本框中输入端口列表。然后我创建一个带有这些端口的udpClient数组开始侦听它们。

static class communicator
    {
        // Setting Variables
        static UdpClient[] UDPreceiver;
        static TcpListener[] TCPreceiver;
        static IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
        static bool listening = false;
        // Listener function
        public static void UDPstartlistening(int port)
        {
            // Startlistening
            listening = true;
            while (listening)
            {
                try
                {
                    UDPreceiver[port] = new UdpClient(port); // udp server
                    if (UDPreceiver[port].Available > 0) // Only read if we have some data queued in buffer
                    {
                        //IPEndPoint object will allow us to read datagrams sent from any tracker.
                        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                        // Blocks untill data is received
                        Byte[] receiveBytes = UDPreceiver[port].Receive(ref RemoteIpEndPoint);
                        string returnData = ByteArrayToString(receiveBytes);
                        // Uses the IPEndPoint object to determine who sent us anything
                        Program.form1.addlog("Received: " + returnData.ToString() + " - from " + RemoteIpEndPoint.Address.ToString() + " on port: " + RemoteIpEndPoint.Port.ToString());
                        // Forward this message to the website
                        Task.Run(() => forwardToWebsite(returnData.ToString(), RemoteIpEndPoint.Address.ToString(), RemoteIpEndPoint.Port, "udp", port));
                    }
                    Thread.Sleep(10);
                }
                catch (Exception e)
                {
                   MessageBox.Show("Source : " + e.Source + "'r'n" + "Message : " + e.Message, "Error");
                }
            }
        }

它给了我"对象引用未设置为对象的实例",在"UDPreceiver[port]"的行上。可用"。

我这样做的方式是否正确?

从文本框中提取的多个端口上的 C# UDP 侦听器

试试这个,它包含一些错误修复:

  1. 将接收器放在列表中,而不是未分配的数组中
  2. 客户端应在读取循环之外声明
  3. 您知道这会阻止调用线程吗? => 使用一个新的线程(() => { ... });在另一个线程中运行接收部分

代码如下:

static class communicator
{
    // Setting Variables
    static List<UdpClient> UDPreceivers = new List<UdpClient>();
    //static List<TcpListener> TCPreceivers = new List<TcpListener>();
    static IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
    static bool listening = false;
    // Listener function
    public static void UDPstartlistening(int port)
    {
        UdpClient UDPreceiver = new UdpClient(port); // udp server
        UDPreceivers.Add(UDPreceiver);
        // Startlistening
        listening = true;
        while (listening)
        {
            try
            {
                if (UDPreceiver.Available > 0) // Only read if we have some data queued in buffer
                {
                    //IPEndPoint object will allow us to read datagrams sent from any tracker.
                    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    // Blocks untill data is received
                    Byte[] receiveBytes = UDPreceiver.Receive(ref RemoteIpEndPoint);
                    string returnData = ByteArrayToString(receiveBytes);
                    // Uses the IPEndPoint object to determine who sent us anything
                    Program.form1.addlog("Received: " + returnData.ToString() + " - from " + RemoteIpEndPoint.Address.ToString() + " on port: " + RemoteIpEndPoint.Port.ToString());
                    // Forward this message to the website
                    Task.Run(() => forwardToWebsite(returnData.ToString(), RemoteIpEndPoint.Address.ToString(), RemoteIpEndPoint.Port, "udp", port));
                }
                Thread.Sleep(10);
            }
            catch (Exception e)
            {
                MessageBox.Show("Source : " + e.Source + "'r'n" + "Message : " + e.Message, "Error");
            }
        }
    }
}

我认为您需要仔细查看互联网上的示例,您刚刚创建的UDPreceiver[port]对象不处于接收数据的状态。根据这里,对象应该调用BeginReceive 。没有 C#,但这可能会有所帮助。