欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java通过InetAddress获得指定主机的IP地址,javainetaddress,下面的例子通过InetA

来源: javaer 分享于  点击 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();    }}
相关栏目:

用户点评