Anders Lassen
Anders Lassen

Reputation: 649

Virtualbox conflicting with getting ip

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

Answers (1)

MiltoxBeyond
MiltoxBeyond

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:

  1. A server listening for broadcast packets either on a specific port, or in general.
  2. A client (in this case your android app) that listens on a specific port for a connection(TCP/UDP depends on your applications needs).
  3. The client will also need to be able to send out a broadcast packet on the network identifying itself.

Basically the steps go as follows:

  1. Server binds ports needed for UDP broadcast receipt.
  2. Client binds port to receive response from server (lets say port 16000 for example)
  3. The Client then sends the broadcast packet on the network which is sent to all clients.
  4. The server receives the packet and with the packets info gets the IP address of the client.
  5. The server then responds to the client via the listening port (in this case 16000) either via UDP or TCP.
  6. Once the connection to the client is established the client knows via the connection information the IP of the server, has an active connection to it, and can potentially save the address for later.

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

Related Questions