简体   繁体   English

C#:UDP客户端无法从一台计算机运行到另一台计算机

[英]C#: UDP client not working from one machine to other

I want to exchange information via UDP from one machine to the other. 我想通过UDP从一台计算机与另一台计算机交换信息。 However, for some reason, it does not work. 但是,由于某些原因,它不起作用。

This code runs on machine 1, the receiver (192.168.200.1): 此代码在机器1(接收者(192.168.200.1))上运行:

class Program
{
    static void Main(string[] args)
    {            
        StartListening();
        Console.Write("Waiting.... Press any key to stop");
        Console.ReadLine();
    }

    private async static void StartListening()
    {
        Console.WriteLine("Start listening...");
        string message = await ReadUdpPacket();
        Console.Write(message);
    }

    private async static Task<string> ReadUdpPacket()
    {
        UdpClient udpClient = new UdpClient(44444);            
        while (udpClient.Available == 0)
        {
            Thread.Sleep(10);
        }
        return "Something received...";
    }
}

and this code runs on machine 2 (192.168.200.2): 并且此代码在计算机2(192.168.200.2)上运行:

static void Main(string[] args)
    {
        Console.WriteLine("Small delay so that listeners can prepare...");
        Thread.Sleep(5000);
        Console.WriteLine("Sending packet...");
        IPEndPoint local = new IPEndPoint(IPAddress.Parse("192.168.200.2"), 0);
        IPEndPoint remote = new IPEndPoint(IPAddress.Parse("192.168.200.1"), 44444); // When changed to 192.168.200.2 and run receiver local, it works
        UdpClient udpClient = new UdpClient(local);
        byte[] bytes = Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwxyz");
        udpClient.Send(bytes, bytes.Length, remote);
        Console.Write("Done, press any key...");
        Console.ReadKey();
    }

When I start the programs on both machines, I expected to see 'Something received' appearing in the console window on machine 1. However, that does not happen. 当我在两台计算机上都启动程序时,我希望在计算机1的控制台窗口中看到“收到某些消息”。但是,这不会发生。 I see the UDP packet leaving machine 2 by means of WireShark. 我看到通过WireShark离开机器2的UDP数据包。 The packet is also seen by machine 1; 机器1也可以看到该数据包; I also run WireShark on that one. 我也在那个上运行WireShark。 But the application on machine 1 does not show a response. 但是计算机1上的应用程序未显示响应。

I ran both programs on the same computer (192.168.200.2, thus machine 2) as well. 我也在同一台计算机(192.168.200.2,因此是计算机2)上运行了这两个程序。 Program 1 can be started, but program 2 needs a slight modification: The line: IPEndPoint remote = new IPEindpoint... needs to contain the local machine's IP address of course; 程序1可以启动,但程序2需要稍作修改:行:IPEndPoint remote = new IPEindpoint ...当然需要包含本地计算机的IP地址; I indicated this by a comment in program 2. So the line reads: 我在程序2中通过注释指出了这一点。因此该行显示为:

IPEndPoint remote = new IPEndPoint(IPAddress.Parse("192.168.200.2"), 44444);

When I do this, the string 'Something received' arrives! 当我这样做时,字符串“收到的东西”到了!

So it looks that the programming is correct (please note that I minimized the code; this was the smallest project to reproduce the issue that I have). 因此,看起来编程是正确的(请注意,我将代码最小化了;这是再现我所遇到问题的最小项目)。 Otherwise I would not see it working with both programs on one machine. 否则,我将看不到它在一台机器上同时使用两个程序。 The sender application is probably also correct, because the UDP packet goes from one machine to the other (at least, I see the UDP packet on WireShark output which runs on both machines). 发送者应用程序也可能是正确的,因为UDP数据包从一台计算机传到另一台计算机(至少,我在WireShark输出上看到了UDP数据包,该数据包在两台计算机上都运行)。 But anyway, the receiver does not get the packet when it runs on the other machine. 但是无论如何,当接收方在另一台计算机上运行时,接收方不会获得该数据包。

When you run the receiver for the first time, you will most probably get a firewall warning. 首次运行接收器时,很可能会收到防火墙警告。 I clicked 'Allow'. 我点击了“允许”。

I also tried both programs on the other machine (after updating the addresses). 我还尝试了另一台计算机上的两个程序(更新地址后)。 It also works here. 在这里也可以使用。 From and to same machine works, from one machine to the other: nothing. 从一台机器到另一台机器,从一台机器到另一台机器:什么都没有。

I'm really stuck with this. 我真的很坚持。 Anyone a clue on this? 有任何线索吗? Have I forgot anything? 我忘了什么吗? I'm running the console apps on Win7 64-bit on both machines. 我正在两台计算机上的Win7 64位上运行控制台应用程序。 I use VS 2012. 我使用VS 2012。

I've found out that have to add two rules to the firewall: 我发现必须向防火墙添加两个规则:

  1. One for your application; 一种适合您的应用; so you have to specify the name 所以你必须指定名字
  2. One for the port and protocol 一个用于端口和协议

If you add both, it works. 如果同时添加两者,则可以使用。 If you add only one of them, it does not work. 如果仅添加其中之一,则无法使用。 I assumed that adding the name of your application should be enough. 我认为添加应用程序的名称就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM