Get hostname, when IP address is known

V

vi

I have the following program

import java.net.*;
public class Inet1 {
public static void main(String[] a) throws Exception {
InetAddress i1 = InetAddress.getByName("www.sun.com");
System.out.println(i1.getHostAddress()); //209.249.116.195
InetAddress i2 = InetAddress.getByName("209.249.116.195");
System.out.println(i2.getHostName());
}
}

On the last println statement, I want the hostname to be printed i.e.
www.sun.com.
Given a hostname, I am able to get IP address. But, given an IP
address, I am not able to get the hostname.
Please suggest a solution.
Thanks.
 
O

Oliver Wong

vi said:
I have the following program

import java.net.*;
public class Inet1 {
public static void main(String[] a) throws Exception {
InetAddress i1 = InetAddress.getByName("www.sun.com");
System.out.println(i1.getHostAddress()); //209.249.116.195
InetAddress i2 = InetAddress.getByName("209.249.116.195");
System.out.println(i2.getHostName());
}
}

On the last println statement, I want the hostname to be printed i.e.
www.sun.com.
Given a hostname, I am able to get IP address. But, given an IP
address, I am not able to get the hostname.
Please suggest a solution.
Thanks.

See RFC953: http://www.faqs.org/rfcs/rfc953.html

- Oliver
 
E

Eric K Idema

vi said:
I have the following program
import java.net.*;
public class Inet1 {
public static void main(String[] a) throws Exception {
InetAddress i1 = InetAddress.getByName("www.sun.com");
System.out.println(i1.getHostAddress()); //209.249.116.195
InetAddress i2 = InetAddress.getByName("209.249.116.195");
System.out.println(i2.getHostName());
}
}
On the last println statement, I want the hostname to be printed i.e.
www.sun.com.
Given a hostname, I am able to get IP address. But, given an IP
address, I am not able to get the hostname.

There's nothing wrong with your code. It looks like Sun's nameserver
isn't configured properly for reverse lookups. Try a few more examples
and you'll find that some work and some don't. My experience is that
many companies don't get reverse dns right.

Eric
 
R

Roedy Green

Given a hostname, I am able to get IP address. But, given an IP
address, I am not able to get the hostname.
see http://mindprod.com/jgloss/ip.html for the whois source code.

It does not work unless you have a reverse DNS lookup. Some sites will
not support that. You can try out the code

java WhoIs mindprod.com
java WhoIs 24.85.32.136

Note that sometimes several domains share and IP and vice versa.
 
R

Roedy Green

It looks like Sun's nameserver
isn't configured properly for reverse lookups.

that is correct. You can go DNS->IP but not the reverse with Sun. Part
of the catch is reverse lookup can be used for spam harvesting, and
other annoying purposes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top