我正在编写一些函数,允许我使用IPv4和IPv6发送UDP广播/多播。以下代码执行此操作。我遇到的问题是它只对单个适配器执行此操作。如果我的PC上安装了两个网络适配器,则不会同时发送广播。是否可以将单个套接字配置为同时处理IPv4和IPv6以及在所有NIC上发送和接收?或者我是否必须为每个IP地址创建单独的套接字?
public void CreateBroadcaster(CancellationToken cancellationToken, int discoveryPort, int advancePort)
{
_cancellationToken = cancellationToken;
_broadcastEndpoint = new IPEndPoint(IPAddress.Broadcast, advancePort);
var epLocal = new IPEndPoint(IPAddress.IPv6Any, discoveryPort);
_broadcaster = new UdpClient(epLocal);
var soc = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
soc.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
_broadcaster.Client = soc;
_broadcaster.Client.DualMode = true;
_broadcaster.Client.Bind(epLocal);
_broadcaster.EnableBroadcast = true;
_broadcaster.MulticastLoopback = true;
_broadcaster.Client.SetSocketOption(
SocketOptionLevel.IPv6,
SocketOptionName.AddMembership,
new IPv6MulticastOption(IPAddress.Parse("ff02::1")));
}
public void SendPingsOnAdapterLocalSubnets()
{
_broadcaster.Send(_sendData, _sendData.Length, _broadcastEndpoint);
}
答案 0 :(得分:0)
您需要2个独立的插座。在低级别,sockaddr结构将具有一个或另一个。 IPv4(AF_INET)或IPv6(AF_INET6)。
http://www.gnu.org/software/libc/manual/html_node/Address-Formats.html#Address-Formats