简体   繁体   中英

converting an IP address to host name

In my java application if user enters the IP we need to display the host name, if host name is given then we need to display the IP of the host.

For example if user enters an IP address like 173.194.36.37 application should display google.com and vice verse.

Are there any utilities available to perform this operation?

If you are coding in Java, try using InetAddress

InetAddress addr = InetAddress.getByName("173.194.36.37");
String host = addr.getHostName();
System.out.println(host);

What you're looking for is something called DNS . This project seems to be what you're looking for.

The project SomeKittens referred to you looks like a complete DNS server written in Java, which might be more than you need. Have a look at java.net.InetAddress :

java.net.InetAddress.getByName("example.com").getHostAddress();

In terms of domain name, there are no built in utilities, no. You can get the name of a host (but not the domain name) by using getCanonicalHostName() on InetAddress - that should work. The best answer here linked to the DNS Java project, which will get you the domain name.

Example code to connect to, and get the host name from, one of Google's servers is given below:

public class GetHostName {
public static void main(String[] args) throws Exception {
    InetAddress address = InetAddress.getByAddress(new byte[]{74, 125,(byte) 227, 7});
    System.out.println(address.getCanonicalHostName());
}
}
import java.net.*;
import java.util.*;
import java.io.*;

public class DNS

 {
    public static void main(String[] args) throws Exception

    {
        Scanner s = new Scanner(System.in);

        System.out.println("Enter the Domain`enter code here` Name");
        String domainName = s.nextLine();
        System.out.println("Domain into IP");
        System.out.println(InetAddress.getByName(domainName).getHostAddress());

        System.out.println("Enter the IP Name");
        String ipName = s.nextLine();
        System.out.println("IP into Domain");
        System.out.println(InetAddress.getByName(ipName).getHostName());        

    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM