Reputation: 649
I'm having trouble returning the correct IP, after i installed oracle's virtualbox. It prints me following:
VirtualBox Host-Only Ethernet Adapter 192.168.56.1
VirtualBox Host-Only Ethernet Adapter fe30:0:0:0:1323:fahd:bt75:8422%eth1
Microsoft Teredo Tunneling Adapter 2041:0:91q8:6at8:30he:3r2c:3a53:ff4c
Microsoft Teredo Tunneling Adapter fj80:0:0:0:32bn:1e2z:3f37:ff5c%net4
Realtek PCIe GBE Family Controller 192.168.0.163
Realtek PCIe GBE Family Controller fe30:0:0:0:3a4c:bf90:232a:a324%eth6
I only want to return 192.168.0.163
I used this code to get the IP:
String ip;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
System.out.println(iface.getDisplayName() + " " + ip);
}
}
} catch (SocketException es) {
throw new RuntimeException(es);
}
How do i retrieve only the wanted IP?
Upvotes: 1
Views: 97
Reputation: 2731
To communicate without having an IP, the best way is either through a discovery service or with broadcast UDP packets.
You will need a few things:
Basically the steps go as follows:
The technical details can be found all over stackoverflow, but it varies depending on the programming languages of the server/client and the operating systems involved.
Here is a tutorial via Oracle Documentation for Java: https://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html
One for C#: https://msdn.microsoft.com/en-us/library/tst0kwb1(v=vs.110).aspx
Upvotes: 1