public void AddListeners() { // Mark all adapters as not visited. foreach(var inst in NetworkAdapters.Values) { inst.Visited = false; } List<IPAddress> IPList = GetValidIPAddresses(); foreach(IPAddress localAddress in IPList) { if (NetworkAdapters.ContainsKey(localAddress)) { NetworkAdapters[localAddress].Visited = true; } else { EthernetInstance inst = new EthernetInstance(); IPAddress multicastaddress = IPAddress.Parse("239.0.0.222"); int port = 6; var udpClient = new UdpClient(AddressFamily.InterNetwork); udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); try { udpClient.Client.Bind(new IPEndPoint(localAddress, port)); } catch (SocketException e) { // // Bind() can return WSAEADDRNOTAVAIL if we try to bind // too soon after plugging in a USB NIC. If this // happens, we just continue and catch this connection // next time. The cause of this is uncertain. The // message means "the ip address is invalid", but if // we just wait a little while, the ip address becomes // valid. // // Easy to repro. Just start this app and plug in a // USB adapter after it's running. // if (e.ErrorCode == 10049) // WSAEADDRNOTAVAIL { continue; } } udpClient.JoinMulticastGroup(multicastaddress, localAddress); udpClient.BeginReceive(OnReceiveSink, new object[] { udpClient, new IPEndPoint(localAddress, ((IPEndPoint) udpClient.Client.LocalEndPoint).Port) }); inst.Address = localAddress; inst.Visited = true; NetworkAdapters.Add(localAddress,inst); } } // Remove un-visited adapters from our collection. Operate on a copy of the collection since we're modifying from within an enumerator var newCollection = new Dictionary<IPAddress, EthernetInstance>(NetworkAdapters); foreach(var inst in NetworkAdapters.Values) { if (inst.Visited == false) { newCollection.Remove(inst.Address); } } NetworkAdapters = newCollection; }
public void AddListeners() { // Mark all adapters as not visited. foreach (var inst in NetworkAdapters.Values) { inst.Visited = false; } List <IPAddress> IPList = GetValidIPAddresses(); foreach (IPAddress localAddress in IPList) { if (NetworkAdapters.ContainsKey(localAddress)) { NetworkAdapters[localAddress].Visited = true; } else { EthernetInstance inst = new EthernetInstance(); IPAddress multicastaddress = IPAddress.Parse("239.0.0.222"); int port = 6; var udpClient = new UdpClient(AddressFamily.InterNetwork); udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); try { udpClient.Client.Bind(new IPEndPoint(localAddress, port)); } catch (SocketException e) { // // Bind() can return WSAEADDRNOTAVAIL if we try to bind // too soon after plugging in a USB NIC. If this // happens, we just continue and catch this connection // next time. The cause of this is uncertain. The // message means "the ip address is invalid", but if // we just wait a little while, the ip address becomes // valid. // // Easy to repro. Just start this app and plug in a // USB adapter after it's running. // if (e.ErrorCode == 10049) // WSAEADDRNOTAVAIL { continue; } } udpClient.JoinMulticastGroup(multicastaddress, localAddress); udpClient.BeginReceive(OnReceiveSink, new object[] { udpClient, new IPEndPoint(localAddress, ((IPEndPoint)udpClient.Client.LocalEndPoint).Port) }); inst.Address = localAddress; inst.Visited = true; NetworkAdapters.Add(localAddress, inst); } } // Remove un-visited adapters from our collection. Operate on a copy of the collection since we're modifying from within an enumerator var newCollection = new Dictionary <IPAddress, EthernetInstance>(NetworkAdapters); foreach (var inst in NetworkAdapters.Values) { if (inst.Visited == false) { newCollection.Remove(inst.Address); } } NetworkAdapters = newCollection; }