本文介绍了在同一台计算机上的两个程序之间发送和接收 UDP 数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过 localhost/127...通过 UDP 在同一台计算机上通过 UDP 进行通信(仅单向)...共享相同的端口号?

Is it possible to get two separate programs to communicate on the same computer (one-way only) over UDP through localhost/127... by sharing the same port #?

我们正在进行一个学生项目,在该项目中,我们需要在两台计算机之间发送包含一些遥测数据的 UDP 数据包.生成这些数据包的程序是专有的,但我自己正在使用 C# 使用 System.Net.Sockets.UdpClientSystem.Net.IPEndPoint 编写接收器程序.

We're working on a student project in which we need to send UDP packets containing some telemetry between two computers. The program that generates these packets is proprietary, but I'm working on the receiver program myself with C# using System.Net.Sockets.UdpClient and System.Net.IPEndPoint.

当我们连接了多台计算机时,这在我们的小组会议期间运行良好,我们可以在这些计算机上分别运行这两个程序.但是当我在家并尝试扩展遥测处理程序时,它并不是很有用,因为我只有一台计算机(我需要一个供稿来测试处理程序).我也无法在学校的任何计算机上安装该程序.

This works fine during our group's meetings when we have multiple computers connected on which we can run the two programs separately. But it's not very useful when I'm home and trying to expand on the telemetry processing program as I only have one computer (I need a feed for testing the processing program). I can not install the program on any of the school's computers either.

当我尝试同时在我的计算机上运行这两个程序时(最后启动我的程序),我收到一个 SocketException,表示每个端口通常只允许使用一次.这使我相信必须有某种方法来共享端口(尽管在任何时候只有一个程序可以使用计算机上的端口是有道理的,但我同时运行多个互联网浏览器没有问题(而且我假设他们使用端口 80 进行 http)).

When I try to run both programs on my computer at the same time (starting my program last) I get a SocketException saying that only a single use of each port is normally allowed. Which leads me to believe there must be some way to share the port (although it makes sense that only a single program can use port on a computer at any one time, I have no trouble running multiple internet browsers at the same time (and I suppose they use port 80 for http)).

重新编辑

sipwiz 是对的,感谢 Kalmi 提供指向 UdpClient.Client.Bind() 的指针.不过,当时,我们正在考虑使用另一个程序来生成类似的数据包,并且我们可以使用我的第一个(虽然幼稚)方法与 ctor 中的 UDP 客户端绑定在同一台计算机上共享端口.很抱歉不得不取消标记您的答案,sysrqb.

sipwiz was right, and thanks to Kalmi for the pointer to UdpClient.Client.Bind().At the time, though, we are considering using another program that generates similar packets, and with which we are able to share port with on the same computer using my first (although naive) approach with the UDP client binding in the ctor.Sorry for having to unmark your answer, sysrqb.

推荐答案

我没想到这是可能的,但是..好吧.. sipwiz是对的.

I did not expect this to be possible, but.. well.. sipwiz was right.

这可以很容易地完成.(请投票给 sipwiz 的答案!)

IPEndPoint localpt = new IPEndPoint(IPAddress.Any, 6000);

//Failed try
    try
    {
        var u = new UdpClient(5000);
        u.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

        UdpClient u2 = new UdpClient(5000);//KABOOM
        u2.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
    }
    catch (Exception)
    {
        Console.WriteLine("ERROR! You must call Bind only after setting SocketOptionName.ReuseAddress.
 And you must not pass any parameter to UdpClient's constructor or it will call Bind.");
    }

//This is how you do it (kudos to sipwiz)
    UdpClient udpServer = new UdpClient(localpt); //This is what the proprietary(see question) sender would do (nothing special)

    //!!! The following 3 lines is what the poster needs...(and the definition of localpt (of course))
    UdpClient udpServer2 = new UdpClient();
    udpServer2.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
    udpServer2.Client.Bind(localpt);

这篇关于在同一台计算机上的两个程序之间发送和接收 UDP 数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 02:25