UDPClient消息未收到

时间:2019-03-11 12:43:32

标签: c# visual-studio sockets udp udpclient

我是C#,VS和.NET的新手。我掌握了一些基础知识,但是我很难用自己拥有的伺服控制器与UDP通信。

我希望有人可以指导我代码的问题,代码的工作方式以及接收消息的最佳方法。

我的第一步是广播“谁”消息,当我的伺服器看到此消息时,我将获得有关该伺服器信息的响应。

public void Discover()
        {
            byte[] message = Encoding.ASCII.GetBytes("who");
            IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Broadcast, LINKPORT);
            IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

            System.Console.WriteLine("New Transimission Sequence:");
            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            IPEndPoint localEndpoint = new IPEndPoint(IPAddress.Parse(ip.Address.ToString()), LINKPORT);

                            UdpClient udpClient = new UdpClient(localEndpoint);
                            udpClient.Send(message, message.Length, targetEndPoint);

                            UdpClient udpClientB = new UdpClient(remoteEndPoint);
                            udpClientB.Client.ReceiveTimeout = 1000;
                            string recvMessage;
                            try
                            {
                                recvMessage = System.Text.Encoding.UTF8.GetString(udpClientB.Receive(ref remoteEndPoint));
                            }
                            catch (Exception e)
                            {
                                recvMessage = e.Message;
                            }

                            System.Console.WriteLine(recvMessage);

                            udpClient.Close();
                            udpClientB.Close();
                        }
                    }
                }
            }

            System.Console.WriteLine("==========================================================================================");

            return;
        }

这是我目前拥有的。我知道目前它非常粗糙,但是我什至无法使它正常工作...我试图在上面的代码中在所有网卡上广播“谁”消息。然后我要使用udpClientB来接收消息。我尝试使用udpClient,但没有用,这就是为什么我创建了udpClientB。我还尝试过创建一个单独的线程,该线程仅在该端口上侦听,什么也没有...

现在这就是我在控制台窗口中看到的内容。

New Transimission Sequence:
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
==========================================================================================

当我尝试使用udpClient作为发送者和接收者时,我在控制台窗口中看到了“谁”……我在做什么错?发生了什么事?

我将检查增加了三倍,并且正在发送消息,并且当消息到达伺服控制器时,我正在响应消息。

完成此操作后,还有一个问题,哪种方法最好?这段代码被Windows窗体按钮击中,最终我想在Windows窗体列表上显示所有响应的人。我应该创建异步函数调用吗?使用“计时器”不断监听并清空以太网缓冲区?创建一个新线程来收听?还是我当前正在做的事情,只是在线?

感谢您的阅读时间。

0 个答案:

没有答案