java通过InetAddress获得指定主机的IP地址,javainetaddress,下面的例子通过InetA
分享于 点击 49957 次 点评:182
java通过InetAddress获得指定主机的IP地址,javainetaddress,下面的例子通过InetA
下面的例子通过InetAddress类获得指定域名的ip地址。
import java.net.InetAddress;import java.net.UnknownHostException;/* * Main.java * * @author byrx.net */public class Main { /* * This method performs a NS Lookup */ public void performNSLookup() { try { InetAddress inetHost = InetAddress.getByName("cnn.com"); String hostName = inetHost.getHostName(); System.out.println("The host name was: " + hostName); System.out.println("The hosts IP address is: " + inetHost.getHostAddress()); } catch(UnknownHostException ex) { System.out.println("Unrecognized host"); } } /** * @param args the command line arguments */ public static void main(String[] args) { new Main().performNSLookup(); }}
用户点评