2012-08-04 80 views

回答

11

是的,它的可能性。

import java.net.*; 
public class HostName 
{ 
    public static void main(String args[]) 
    { 
    InetAddress inetAddress =InetAddress.getByName("127.64.84.2");//get the host Inet using ip 
    System.out.println ("Host Name: "+ inetAddress.getHostName());//display the host 
    } 
} 
4

像这样的东西应该指向你在正确的方向:

import java.net.InetAddress; 
import java.net.UnknownHostException; 

public class DNSLookup { 
    public static void main(String args[]) { 
    try { 
     InetAddress host; 
     if (args.length == 0) { 
     host = InetAddress.getLocalHost(); 
     } else { 
     host = InetAddress.getByName(args[0]); 
     } 
     System.out.println("Host:'" + host.getHostName() 
      + "' has address: " + host.getHostAddress()); 

    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } 
    } 
} 

Source

0

可以使用InetAddress类的getHostName()方法。

0

尝试了这一点....

System.out.println(InetAddress.getByName("IP_ADDR").getHostName());

0

嘿我米使用上述方法的BT的gethostname()方法没有返回ip给予的主机名。

见代码:

try { 
//  This is ip of tutorialspoint.com  
      InetAddress addr2 = InetAddress.getByName("127.64.84.2");  
      op.setText("Host name is: "+addr2.getHostName()); 
     } 
     catch (UnknownHostException e3) { 
      op.setText("Error: Host not found" + e3); 
     } 
0
import java.net.*; 

public class GetHostNameFromIPAddress { 

     public static void main(String[] args) { 
      try { 
       InetAddress inetAddr = InetAddress.getByName("163.53.76.55"); 
       // Get the host name 
       String hostname = inetAddr.getHostName(); 
       // Get canonical host name 
       String canonicalHostname = inetAddr.getCanonicalHostName(); 
       System.out.println("Hostname: " + hostname); 
       System.out.println("Canonical Hostname: " +  canonicalHostname); 
      } 
      catch (UnknownHostException e) { 
       System.out.println("Host not found: " + e.getMessage()); 
      } 
     } 

} 
相关问题