2010-07-30 25 views
20

Benim ana dosyası (C: \ WINDOWS \ system32 drivers \ etc \ hosts \): Ben bir IPAddress dönüştürmek için bir yol bulmaya çalışıyorumJava'da Ana Makine Adı İçin IP Adresi?

# Switches 
192.168.200.254  sw-con-ctrl 
192.168.201.253 sw-con-ctrl-2 
192.168.201.254  sw-con-ctrl-1 
# 192.168.188.1  sw-con-ctrl-blk-1 
# 192.168.189.1  sw-con-ctrl-red 
192.168.190.62  access-console 

# Routers 
192.168.21.1   rtr1 
192.168.22.1   rtr2 

adı eşlemelerine IP Adresi bir grup var HostName Java API'leri aracılığıyla programatik olarak.

yalancı kod:

IPAddress ip = new IPAddress("192.168.190.62"); 
String host = ip.getHost(); 
System.out.println(host); //prints "access-console" 

cevap

43

Kodu here denedim ve çalışıyor. Yani:

InetAddress addr = InetAddress.getByName("192.168.190.62"); 
    String host = addr.getHostName(); 
    System.out.println(host); 
1
Bu, hiçbir geriye doğru arama gerektiğinde Javadocs sadece yerel söylemek gibi çalışır

: bir sabit IP adresi verilirse, adres formatının sadece geçerliliği kontrol edilir.

birisi uzaktan arama yapmak için üçüncü taraf kavanozları kullanmadan bir bilginiz varsa

...

2
bu code.I ile deneyebilirsiniz

eski faydalı

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

public class IpTest { 
    public static void main(String args[]) throws UnknownHostException { 
     InetAddress addr = InetAddress.getLocalHost(); 
     String ipAddress = addr.getHostAddress();  
     System.out.println("IP address of localhost : " + ipAddress); 
     String hostname = addr.getHostName(); 
     System.out.println("Name of hostname : " + hostname); 
    } 
} 
düşünüyorum
İlgili konular