« MBS Xojo / Real Studi… | Home | MBS Xojo / Real Studi… »

Tip of the day: Find network interfaces by name


We had this week the issue that we need to find a NetworkInterface object for a network interface based on the name. As NetworkInterface class does not have a property for that, we use our NetworkInterfaceMBS class to build a mapping from IP address to actual name. It works well here and I think this code maybe useful for you. Beside name, it also allows to use any IP or MAC address to specify the interface.

Function FindNetworkInterface(name as string) As NetworkInterface name = name.trim if name.len = 0 then Return nil // search by IP/MAC dim u as integer = System.NetworkInterfaceCount-1 for i as integer = 0 to u dim n as NetworkInterface = System.GetNetworkInterface(i) if n.IPAddress = name or n.MACAddress = name then Return n end if next // use MBS Plugin to build a mapping dim interfaces() as NetworkInterfaceMBS = NetworkInterfaceMBS.AllInterfaces dim map as new Dictionary for each n as NetworkInterfaceMBS in interfaces dim IPv4s() as string = n.IPv4s dim IPv6s() as string = n.IPv6s for each IPv4 as string in IPv4s map.Value(IPv4) = n.Name next for each IPv6 as string in IPv6s map.Value(IPv6) = n.Name next if n.MAC<>"" then map.Value(n.MAC) = n.Name end if next // now search interfaces by name, IPv4 or IPv6 for i as integer = 0 to u dim n as NetworkInterface = System.GetNetworkInterface(i) if map.Lookup(n.IPAddress, "") = name then Return n end if if map.Lookup(n.MACAddress, "") = name then Return n end if next End Function
11 01 14 - 12:24
No comments

  
Remember personal info?

Emoticons / Textile


Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.