如何做到在整个本地网络中的UDP多播在C#? [英] How to do a UDP multicast across the local network in c#?
问题描述
我想获得一些简单的UDP通信的工作我的本地网络上。
I am trying to get some simple UDP communication working on my local network.
我想要做的是做一个多播到所有的机器在网络上
All i want to do is do a multicast to all machines on the network
下面是我送code
public void SendMessage(string message)
{
var data = Encoding.Default.GetBytes(message);
using (var udpClient = new UdpClient(AddressFamily.InterNetwork))
{
var address = IPAddress.Parse("224.100.0.1");
var ipEndPoint = new IPEndPoint(address, 8088);
udpClient.JoinMulticastGroup(address);
udpClient.Send(data, data.Length, ipEndPoint);
udpClient.Close();
}
}
和这里是我的接收code
and here is my receiving code
public void Start()
{
udpClient = new UdpClient(8088);
udpClient.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50);
receiveThread = new Thread(Receive);
receiveThread.Start();
}
public void Receive()
{
while (true)
{
var ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
var data = udpClient.Receive(ref ipEndPoint);
Message = Encoding.Default.GetString(data);
// Raise the AfterReceive event
if (AfterReceive != null)
{
AfterReceive(this, new EventArgs());
}
}
}
它完全在我的本地机器,但不通过网络。
It works perfectly on my local machine but not across the network.
似乎 - 不是防火墙。我禁用它在两台机器上,它仍然没有工作。
-Does not seem to be the firewall. I disabled it on both machines and it still did not work.
- 它的工作原理,如果我做的一个直接发送到客户机的硬codeD IP地址(即不是多播)。
-It works if i do a direct send to the hard coded IP address of the client machine (ie not multicast).
任何帮助将是AP preciated。
Any help would be appreciated.
推荐答案
请问你的本地网络的硬件支持的 IGMP ?
Does your local network hardware support IGMP?
这有可能是你的交换机组播知道,但如果IGMP被禁用,如果任何附加硬件订阅某个组播组这样就不会这些数据包转发也不会注意到。
It's possible that your switch is multicast aware, but if IGMP is disabled it won't notice if any attached hardware subscribes to a particular multicast group so it wouldn't forward those packets.
要测试这一点,暂时用交叉线连接两台机器直接在一起。这应该(AFAICR)总是工作。
To test this, temporarily connect two machines directly together with a cross-over cable. That should (AFAICR) always work.
另外,它应该是服务器的一半code,有提供给TTL参数 JoinMulticastGroup()
,而不是客户端的一半。
Also, it should be the server half of the code that has the TTL argument supplied to JoinMulticastGroup()
, not the client half.
这篇关于如何做到在整个本地网络中的UDP多播在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!