在同一台计算机上发送​​和接收两个节目之间的UDP数据包 [英] Sending and receiving UDP packets between two programs on the same computer

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

问题描述

时有可能得到两个单独的程序在同一台计算机上通讯(仅单程)通过UDP通过本地主机/ 127 ...通过共享相同的端口#?

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.UdpClient 的和的 System.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说,只有一个使用的每个端口是的正常的允许。这使我相信一定有某种方式来分享端口(虽然它是有道理的,只有一个单一的程序可以在任何时候使用端口的计算机上,我没有麻烦运行在同一时间多个Internet浏览器(和我假设他们使用端口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()。
当时,虽然,我们使用的是类似的生成包另一个程序考虑,并与我们所能够使用的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的回答了!)

It can be done very easily. (Please vote sipwiz's answer up!)

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. \n 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数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆