1

I am using Windows 8 with JDK 1.7. My IP address is 192.168.1.108, when I am running:

System.out.println(InetAddress.getLocalHost().equals(InetAddress.getByName("localhost"))); 

OR

System.out.println(InetAddress.getLocalHost().equals(InetAddress.getByName("127.0.0.1")));

Output - It's all false.

InetAddress.getLocalHost() - Output: 192.168.1.108      
InetAddress.getByName("localhost") - Output: 127.0.0.1

Further more, my UDP server is binded on InetAddress.getLocalHost() and it can't receive anything from the client if the client send packets to InetAddress.getByName("localhost"). However, it works well if the client send to InetAddress.getLocalHost(). Port is corrent.

Anyone know the difference? Thanks in advance.

SK.
  • 4,174
  • 4
  • 30
  • 48
user3057903
  • 11
  • 1
  • 2
  • Probably `getLocalHost()` method does name resolution and resolves the address to 127.0.0.1 (or any other ip). So the question becomes: What is the difference between localhost and 127.0.0.1. Check here: http://stackoverflow.com/questions/7382602/what-is-the-difference-between-127-0-0-1-and-localhost – Utku Özdemir Dec 02 '13 at 15:21
  • Nope, System.out.println(InetAddress.getByName("localhost").equals(InetAddress.getByName("127.0.0.1"))) gives me true – user3057903 Dec 02 '13 at 15:32
  • Because both `getByNa‌me` and `getLocalhost` methods return you an `InetAddress` object. `equals` method is overridden in `InetAddress` class to return true if IPs are same. Check here: http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#equals(java.lang.Object) – Utku Özdemir Dec 02 '13 at 15:40

2 Answers2

2

From the JDK documentation for getLocalHost():

Returns the address of the local host. This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.

In my GNU/Linux box, my host name is "laptop", which is mapped to an address different than 127.0.0.1 in /etc/hosts. There is an equivalent file in Windows at C:\Windows\System32\drivers\etc\hosts.

By default this hosts file is searched before DNS lookup.

akrog
  • 326
  • 1
  • 10
0

The ad1 gives you your address in your LAN/WAN, it seems (I mean local/private network IP addresses like e.g. 192.168.0.108 or like 10.3.6.55).

See also:

http://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces

http://download.java.net/jdk7/archive/b123/docs/api/java/net/InetAddress.html#getLocalHost%28%29

But note that ad2 and ad3 are equal in my example.

import java.net.InetAddress;

public class Test014 {

    public static void main(String[] args) throws Exception {
        InetAddress ad1 = InetAddress.getLocalHost();
        InetAddress ad2 = InetAddress.getByName("localhost");
        InetAddress ad3 = InetAddress.getByName("127.0.0.1");

        printArr(ad1.getAddress());
        printArr(ad2.getAddress());
        printArr(ad3.getAddress());

        System.out.println(ad1.equals(ad2));
        System.out.println(ad1.equals(ad3));
        System.out.println(ad2.equals(ad3));
    }

    static void printArr(byte[] arr){
        for (int i=0; i<arr.length; i++){
            System.out.print("[" + i + "] = " + arr[i] + "; ");
        }
        System.out.println();
        System.out.println("---------");
    }

}

Also, check the API docs about when the equals method returns true and when false.

http://download.java.net/jdk7/archive/b123/docs/api/java/net/InetAddress.html#equals%28java.lang.Object%29

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • Now I understand why equal return false. But I've only one network adaptor. The packet sent to 127.0.0.1 should also be received if I bind udp socket on 192.168.1.108, am I right? – user3057903 Dec 02 '13 at 15:38
  • I think - not really. See here. http://stackoverflow.com/questions/7556811/why-bind-a-socket-to-an-address I know that servers usually have special flag/switch to tell them to listen on all known addresses/interfaces. And you're practically writing a server. So it does matter on which address or addresses you have your server port open for incoming connections. – peter.petrov 5 mins ago – peter.petrov Dec 02 '13 at 15:53