返回顶部

收藏

java通过InetAddress获得指定主机的IP地址

更多

下面的例子通过InetAddress类获得指定域名的ip地址。

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

/*
 * Main.java
 *
 * @author outofmemory.cn
 */
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();
    }
}

标签:java,IP,InetAddress

收藏

0人收藏

支持

0

反对

0

发表评论