仅当一个接口处于活动状态时,具有多个 NIC 的 UDP 多播才有效
我已经查看了所有内容,但找不到解决这个问题的方法。我尝试了所有我能看到的组合,但没有成功。
基本上,我想选择一个接口,在两台机器上启动 UDP 客户端并发送/接收消息。当只有一个网卡处于活动状态时,一切正常,但当两个网卡处于活动状态时,它就会停止工作。我用 Wireshark 进行查看,通过一个网卡可以看到数据包传入和传出。
现在,当我使用两个网卡时,我只能从第一个枚举的网卡进行发送,而无法在任何一个上接收。当两个 NIC 均处于活动状态时,WireShark 不会在端口上显示任何接收到的数据包。
代码如下。我曾经只有一个插座,但正在尝试一些不同的东西。
public UDPInstance(IPAddress ip, int port, int RXFrequency)
{
rxFreq = RXFrequency;
// Listener Init
TXclient = new UdpClient();
RXclient = new UdpClient();
TXclient.ExclusiveAddressUse = false;
RXclient.ExclusiveAddressUse = false;
//localEp = new IPEndPoint(ip, port);
TXlocalEp = new IPEndPoint(ip, port);
RXlocalEp = new IPEndPoint(IPAddress.Any, port);
TXclient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
RXclient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
TXclient.Client.Bind(TXlocalEp);
RXclient.Client.Bind(RXlocalEp);
InterfaceIP = ip.ToString();
multicastaddress = IPAddress.Parse("239.0.0.222");
TXclient.JoinMulticastGroup(multicastaddress);
RXclient.JoinMulticastGroup(multicastaddress);
// Sender Init
remoteep = new IPEndPoint(multicastaddress, port);
Listener = null;
RXData = new List<string>();
StartListenerThread();
}
public void StartListenerThread()
{
Listener = new Thread(new ThreadStart(ListenerThread));
Listener.IsBackground = true;
Listener.Start();
}
public void StopListenerThread()
{
Listener.Abort();
}
private void ListenerThread()
{
while (true)
{
Byte[] data = RXclient.Receive(ref remoteep);
string datastr = Encoding.Unicode.GetString(data);
if (datastr != "")
{
string[] PacketStrings = datastr.Split(new char[] { '~' });
foreach (string pkt in PacketStrings)
RXData.Add(pkt);
}
Thread.Sleep(rxFreq);
}
}
public void Transmit(string data)
{
byte[] buffer;
buffer = Encoding.Unicode.GetBytes(data);
TXclient.Send(buffer, buffer.Length, remoteep);
}
I have looked all over and cannot find a solution to this problem. I have tried every combination I could see with no luck.
Basically, I would like to choose an interface, start a UDP client on two machines and Send/Receive messages. Everything works fine when only one NIC is active, but when two are active, it stops working. I have looked with Wireshark and with one NIC can see packets coming in and going out.
Now when I use two NICs, I can only TX from the first enumerated one and cannot receive on either. WireShark does not show any received packets on the port for either of the two NICs when they are both active.
The code is the following. I used to just have one socket but was trying some different things.
public UDPInstance(IPAddress ip, int port, int RXFrequency)
{
rxFreq = RXFrequency;
// Listener Init
TXclient = new UdpClient();
RXclient = new UdpClient();
TXclient.ExclusiveAddressUse = false;
RXclient.ExclusiveAddressUse = false;
//localEp = new IPEndPoint(ip, port);
TXlocalEp = new IPEndPoint(ip, port);
RXlocalEp = new IPEndPoint(IPAddress.Any, port);
TXclient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
RXclient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
TXclient.Client.Bind(TXlocalEp);
RXclient.Client.Bind(RXlocalEp);
InterfaceIP = ip.ToString();
multicastaddress = IPAddress.Parse("239.0.0.222");
TXclient.JoinMulticastGroup(multicastaddress);
RXclient.JoinMulticastGroup(multicastaddress);
// Sender Init
remoteep = new IPEndPoint(multicastaddress, port);
Listener = null;
RXData = new List<string>();
StartListenerThread();
}
public void StartListenerThread()
{
Listener = new Thread(new ThreadStart(ListenerThread));
Listener.IsBackground = true;
Listener.Start();
}
public void StopListenerThread()
{
Listener.Abort();
}
private void ListenerThread()
{
while (true)
{
Byte[] data = RXclient.Receive(ref remoteep);
string datastr = Encoding.Unicode.GetString(data);
if (datastr != "")
{
string[] PacketStrings = datastr.Split(new char[] { '~' });
foreach (string pkt in PacketStrings)
RXData.Add(pkt);
}
Thread.Sleep(rxFreq);
}
}
public void Transmit(string data)
{
byte[] buffer;
buffer = Encoding.Unicode.GetBytes(data);
TXclient.Send(buffer, buffer.Length, remoteep);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
迈克·G 是对的。 UDPClient 类的构造函数之一采用 IPEndPoint 作为参数。如果 IPEndPoint 设置为本地接口的 IP 地址,那么这就是 UDPClient 和底层套接字将使用的接口,所以是的,您可以将两个 UDP 客户端绑定到一台计算机上的同一端口,只要它们处于打开状态即可单独的本地 IP 接口(即多宿主或多 NIC)。
Mike G is correct. One of the constructors for the UDPClient class takes an IPEndPoint as an argument. If the IPEndPoint is set to the IP address of a local interface, then that is the interface that the UDPClient and underlying socket will use so yes, you can have two UDP clients bound to the same port on a a machine as long as they are on seperate local IP interfaces (i.e. multi-homed or multi-NIC).
我知道这个线程很旧,但遇到同样的问题,我想我无论如何都会做出贡献。
在我的“发送者”机器上,我有 6 个网卡。但只有 1 需要能够发送多播消息,所以我使用了 http://sinclairmediatech.com/using-multicast-on-windows-with-multiple-nics/:
I know this thread is old, but having the same problem, I thought I would contribute anyway.
On my 'sender' machine, I have 6 NICs. But only 1 needs to be able to send multicast messages, so I used this trick from http://sinclairmediatech.com/using-multicast-on-windows-with-multiple-nics/ :
我在 Windows 故障转移集群上遇到了同样的问题......
多个网卡...
我最终向 Microsoft 立案,因为我认为这是操作系统问题。
事实并非如此。
您需要指定要用于创建 IPEndpoint 的接口的 IP。
然后在创建套接字时使用该端点而不是 IPAddress.any
这为我解决了问题。
希望即使晚了也能有所帮助。
I had the same issue on a windows failover cluster...
Multiple nics....
I ended up opening a case with Micorsoft as I thought it was an OS issue.
It wasn't.
You need to specify the IP of the interface you whant to use to create a IPEndpoint.
THen use that endpoint when creating the socket instead of IPAddress.any
That solved the problem for me.
Hope it helps even if it is late.