0

I just posted a question here: How to get the IP address for remote host using Java

The following code worked with me in the couple of domain names that I tested.

   InetAddress inetAddress = InetAddress.getByName("http://www.google.com");
    String ipAddress = inetAddress.getHostAddress().toString()'
    System.out.println(ipAddress );//prints 66.152.109.61

But I am in doubt. How to get the IP without creating a socket?.

So, before I go to further stages in my code, I just need to make sure from one point: Do I need to create socket in order to get the IP address for a remote host ?

4

2 に答える 2

3

Short answer: No.

Longer answer: IP's and domain names are connected via DNS (which you should read up on). What your code is doing is really talking to your operating systems DNS resolver (DNS works (mostly) via UDP sockets, so in principle you could say a socket is involved :-)

于 2012-11-19T13:35:20.963 に答える
1

No there is no need to create socket from your code, getHostAddress will open a socket in the back ground and call stub resolver using the DNS configuration on your machine to resolve the name and if you wireshark the traffic you will find that it went and query your dns server for the hostname

于 2012-11-19T13:34:48.653 に答える