域名轉IP (Java實現)

root @localhost :/home/James/mypro/Network/Host-IP# java network.other.HostnameToIP www.baidu.com     
www.baidu.com/61.135.169.105
www.baidu.com/61.135.169.125
root @localhost :/home/James/mypro/Network/Host-IP# java network.other.HostnameToIP baidu.com
baidu.com/220.181.111.86
baidu.com/123.125.114.144
baidu.com/220.181.111.85

package network.other;

import java.net.*;

public class HostnameToIP {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java network.other.HostnametoIP [hostname]");
        }
        String hostname = args[0];
        try {
            InetAddress[] address = InetAddress.getAllByName(hostname);
            for (int i=0; i<address.length; i++) {
                System.out.println(address[i]);
            }
        } catch (UnknownHostException e) {
            System.err.println("Unknown Host " + hostname);
        }
    }
}
對比C語言實現,Java的封裝確實方便。
相關文章
相關標籤/搜索