Unexpected end in getLocalHost with InetAddress

1

I need to connect to a computer using RMI, but the project server class has the following line:

InetAddress.getLocalHost().toString()

Of which according to the documentation I hope I return a String with the network address of my machine, I am connected to a network and would expect an IP different from localhost , however this method returns 127.0.1.1 .

Here I have the code of the project .

    
asked by Emanuel Martínez Pinzón 01.04.2016 в 14:58
source

1 answer

2

I think the problem is network interfaces. On my machine I have eth0 and lo :

$ ifconfig 
eth0      Link encap:Ethernet  HWaddr 34:17:eb:d8:9e:83  
          inet addr:10.91.11.30  Bcast:10.91.11.255  Mask:255.255.255.0
          inet6 addr: fe80::3617:ebff:fed8:9e83/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12685226 errors:0 dropped:321209 overruns:0 frame:0
          TX packets:6092251 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:6504196254 (6.5 GB)  TX bytes:1019289759 (1.0 GB)
          Interrupt:20 Memory:f7e00000-f7e20000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:587126 errors:0 dropped:0 overruns:0 frame:0
          TX packets:587126 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:259789919 (259.7 MB)  TX bytes:259789919 (259.7 MB)

If I execute the following code:

import java.net.*;

public class Address {
    public static void main(String[] args) {
        try {
            InetAddress IP = InetAddress.getLocalHost();
            System.out.println("Mi IP es: " + IP.getHostAddress());
        } catch( UnknownHostException e ) {
          System.out.println(e);
        }   
    }    
}

The result is:

$ java Address
Mi IP es: 127.0.1.1

And that is far from what you are looking for. However if we iterate the interfaces and print the IP addresses we can get closer:

import java.net.*;
import java.util.Enumeration;

public class Address {
    public static void main(String[] args) {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            System.out.println("------------------");
            while (interfaces.hasMoreElements()) {
                NetworkInterface interfaz = interfaces.nextElement();
                System.out.println("Interfaz: " + interfaz.getDisplayName());
                Enumeration<InetAddress> direccion = interfaz.getInetAddresses();
                while (direccion.hasMoreElements()) {
                    InetAddress ip = direccion.nextElement();
                    System.out.println(ip.getHostAddress());
                }
                System.out.println("------------------");
            }
        } catch(SocketException e) {
            System.out.println(e);
        }
    }
}

The result of the previous code would be:

$ java Address
------------------
Interfaz: eth0
fe80:0:0:0:3617:ebff:fed8:9e83%2
10.91.11.30
------------------
Interfaz: lo
0:0:0:0:0:0:0:1%1
127.0.0.1
------------------

We already have the interfaces, and the IPv4 and IPv6 addresses of each interface.

You could even get rid of the Loopback using the isLoopback of the interface:

import java.net.*;
import java.util.Enumeration;

public class Address {
    public static void main(String[] args) {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            System.out.println("------------------");
            while (interfaces.hasMoreElements()) {
                NetworkInterface interfaz = interfaces.nextElement();
                // No necesitamos el Loopback
                if (interfaz.isLoopback()) {
                    continue;
                }
                System.out.println("Interfaz: " + interfaz.getDisplayName());
                Enumeration<InetAddress> direccion = interfaz.getInetAddresses();
                while (direccion.hasMoreElements()) {
                    InetAddress ip = direccion.nextElement();
                    System.out.println(ip.getHostAddress());
                }
                System.out.println("------------------");
            }
        } catch(SocketException e) {
            System.out.println(e);
        }
    }
}

Now the result would be:

$ java Address 
------------------
Interfaz: eth0
fe80:0:0:0:3617:ebff:fed8:9e83%2
10.91.11.30
------------------

If you do not need IPv6, you can ask if the IP in question is from instance Inet6Address :

import java.net.*;
import java.util.Enumeration;

public class Address {
    public static void main(String[] args) {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            System.out.println("------------------");
            while (interfaces.hasMoreElements()) {
                NetworkInterface interfaz = interfaces.nextElement();
                // No necesitamos el Loopback
                if (interfaz.isLoopback()) {
                    continue;
                }
                System.out.println("Interfaz: " + interfaz.getDisplayName());
                Enumeration<InetAddress> direccion = interfaz.getInetAddresses();
                while (direccion.hasMoreElements()) {
                    InetAddress ip = direccion.nextElement();
                    // Solo IPv4
                    if (ip instanceof Inet6Address) {
                        continue;
                    }
                    System.out.println(ip.getHostAddress());
                }
                System.out.println("------------------");
            }
        } catch(SocketException e) {
            System.out.println(e);
        }
    }
}

Finally, the result is:

$ java Address 
------------------
Interfaz: eth0
10.91.11.30
------------------

Well, I think this is pretty close to what you're looking for.

References

answered by 01.04.2016 / 15:59
source