请问如何实现域名方向解析?就是给一个IP地址,解析出该地址对应的域名。用inetaddress只能实现域名到IP的解析,却无法实现反向解析(IP到域名),我试了gethostname(),好像不能实现。

解决方案 »

  1.   

    gethostname()记得是得到本机的名字
    除非你自已有DNS服务,好像不能从IP得到域名
      

  2.   

    InetAddress address = InetAddress.getByAddress(byte[] addr);addrees会以hostname/address的形式toString()
      

  3.   

    回帖的时候自己都觉得不对,重新查了下JAVA网络编程:6.1.1.3 public static InetAddress getByAddress(byte[ ] address) throws UnknownHostException // Java 1.4public static InetAddress getByAddress(String hostName, byte[] address) throws UnknownHostException // Java 1.4
    In Java 1.4 and later, you can pass a byte array and optionally a hostname to getByAddress() to create an InetAddress object with exactly those bytes. Domain name lookup is not performed. However, if byte array is some length other than 4 or 16 bytes—that is, if it can't be an IPv4 or IPv6 address—an UnknownHostException is thrown.This is useful if a domain name server is not available or might have inaccurate information. For example, none of the computers, printers, or routers in my basement area network are registered with any DNS server. Since I can never remember which addresses I've assigned to which systems, I wrote a simple program that attempts to connect to all 254 possible local addresses in turn to see which ones are active. (This only took me about 10 times as long as writing down all the addresses on a piece of paper.)getByAddress(byte[] address) really doesn't do anything getByAddress(String address) doesn't do. In a few cases, it might be marginally faster because it doesn't have to convert a string to a byte array, but that's a trivial improvement. getByAddress(String hostName, byte[] address) does let you create InetAddress objects that don't match or even actively conflict with the information in the local DNS. There might occasionally be a call for this, but the use case is pretty obscure.
    以前都是用getByName("202.119.24.35"),但在JAVA1.4中不能显示主机名,而只能显示/202.119.24.35.
      

  4.   

    我怀疑这是不是DNS不支持反向解析?我用的是JDK1.5
      

  5.   

    以前也测试过,如果想通过一个IP地址反向解析出域名几乎是不可能的。我以前在局域网测试,有的时候通过一个网内的另一台电脑的IP地址都返回不了那台电脑的名字。
      

  6.   

    有本书叫《精通JAVA瓦网络编程》,是05年出的,它上面演示了反向解析的功能,但我尝试总是不行,是不是我的方法不对?或者反向解析需要特殊配置?
      

  7.   

    反向解析首先要求被解析的机器的DNS支持;很遗憾,国内大多数ISP并不提供这种服务,所以无论用什么开发工具,也不能反向解析出域名
      

  8.   

    byte[] b = {(byte)192, (byte)168, (byte)25, (byte)222}; 
    final InetAddress addr = InetAddress.getByAddress(b);
      

  9.   

    byte[] b = {(byte)202, (byte)119, (byte)24, (byte)35}; 
     InetAddress addr = InetAddress.getByAddress(b);
    System.out.println(arr.getHostName());