简体   繁体   English

将IP地址转换为主机名

[英]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. 在我的java应用程序中,如果用户输入IP,我们需要显示主机名,如果给出主机名,那么我们需要显示主机的IP。

For example if user enters an IP address like 173.194.36.37 application should display google.com and vice verse. 例如,如果用户输入的IP地址如173.194.36.37应用程序应显示google.com173.194.36.37

Are there any utilities available to perform this operation? 是否有可用于执行此操作的实用程序?

If you are coding in Java, try using InetAddress 如果您使用Java编码,请尝试使用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 . 您正在寻找的东西叫做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. SomeKittens项目提到你看起来像一个用Java编写的完整DNS服务器,可能比你需要的更多。 Have a look at java.net.InetAddress : 看看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. 可以通过在InetAddress上使用getCanonicalHostName() 获取主机的名称(但不是域名) - 这应该可行。 The best answer here linked to the DNS Java project, which will get you the domain name. 这里的最佳答案链接到DNS Java项目,它将为您提供域名。

Example code to connect to, and get the host name from, one of Google's servers is given below: 下面给出了连接到Google服务器之一并从中获取主机名的示例代码:

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());        

    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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