converting an IP address to host name

前端 未结 4 1789
臣服心动
臣服心动 2020-12-03 22:26

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

相关标签:
4条回答
  • 2020-12-03 22:32

    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());
    }
    }
    
    0 讨论(0)
  • 2020-12-03 22:44

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

    0 讨论(0)
  • 2020-12-03 22:46

    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();
    
    0 讨论(0)
  • 2020-12-03 22:47

    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);
    
    0 讨论(0)
提交回复
热议问题