Visual Studio接收以太网UDP(C#)

Visual Studio接收以太网UDP(C#),c#,visual-studio,udp,ethernet,udpclient,C#,Visual Studio,Udp,Ethernet,Udpclient,我试图通过以太网(从控制器)接收UDP数据,但遇到了一些问题。我知道控制器正在发送数据,因为我可以在wireshark上看到数据,但我尝试过的所有方法都没有奏效。下面的代码是我发现的最接近于接收我想要的数据的代码。 更多信息:控制器IP和端口为192.168.82.27:1743,我端的接收IP和端口为192.168.82.21:1740 public class UDPListener { static UdpClient client = new UdpCli

我试图通过以太网(从控制器)接收UDP数据,但遇到了一些问题。我知道控制器正在发送数据,因为我可以在wireshark上看到数据,但我尝试过的所有方法都没有奏效。下面的代码是我发现的最接近于接收我想要的数据的代码。 更多信息:控制器IP和端口为192.168.82.27:1743,我端的接收IP和端口为192.168.82.21:1740

    public class UDPListener
    {
        static UdpClient client = new UdpClient(1740);
        public static void Main()
        {
            try
            {
                client.BeginReceive(new AsyncCallback(recv), null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            while (true)
            {

            }
        }
        //CallBack
        private static void recv(IAsyncResult res)
        {
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 1743);
            byte[] received = client.EndReceive(res, ref RemoteIpEndPoint);
            //Process code
            Console.WriteLine(RemoteIpEndPoint + "  :  " + Encoding.ASCII.GetString(received));
            client.BeginReceive(new AsyncCallback(recv), null);
        } 
    }
此代码应适用于:

public class Receiver
{
    private readonly UdpClient udp;
    private IPEndPoint ip = new IPEndPoint(IPAddress.Any, 1740);
    public Receiver()
    {
        udp = new UdpClient
        {
            ExclusiveAddressUse = false
        };
        udp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        udp.Client.Bind(ip);
    }
    public void StartListening()
    {
        udp.BeginReceive(Receive, new object());
    }
    private void Receive(IAsyncResult ar)
    {
        var bytes = udp.EndReceive(ar, ref ip);
        StartListening();
    }
}

谢谢你的回复,但它似乎给我的代码比我贴的少?也许我做错了,但是控制台没有给我任何信息(我在一个部分中添加了输出接收到的任何内容)。当我运行最初发布的代码时,控制台输出如下内容:192.168.82.247:1740:?@???这显然不是我要找的数据,但它是有意义的。