A seconda di quello che stai facendo, c'è un metodo che potrebbe aiutare Win32. Restituirà la migliore interfaccia per un dato indirizzo IP. Per ottenere quello di default (la 0.0.0.0), che di solito è quello che si vuole per il multicast, è abbastanza facile:
P/Invoke firma:
[DllImport("iphlpapi.dll", CharSet = CharSet.Auto)]
private static extern int GetBestInterface(UInt32 DestAddr, out UInt32 BestIfIndex);
Poi da qualche altra parte:
// There could be multiple adapters, get the default one
uint index = 0;
GetBestInterface(0, out index);
var ifaceIndex = (int)index;
var client = new UdpClient();
client.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, (int)IPAddress.HostToNetworkOrder(ifaceIndex));
var localEndpoint = new IPEndPoint(IPAddress.Any, <port>);
client.Client.Bind(localEndpoint);
var multicastAddress = IPAddress.Parse("<group IP>");
var multOpt = new MulticastOption(multicastAddress, ifaceIndex);
client.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, multOpt);
var broadcastEndpoint = new IPEndPoint(IPAddress.Parse("<group IP>"), <port>);
byte[] buffer = ...
await client.SendAsync(buffer, buffer.Length, broadcastEp).ConfigureAwait(false);
fonte
2013-09-12 16:20:10
hai trovato una soluzione? Ho lo stesso problema. L'articolo su MSDN ha risolto il tuo problema? – Gobliins
per me non è stato – Gobliins
@Gobliins Non ha funzionato anche per me:/Qualche soluzione? – J4N