java.net.InetAddress简单使用

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class Test1 {

	public static void main(String[] args) throws IOException {
		
		InetAddress ip = InetAddress.getLocalHost();
		System.out.println(ip);//hqq-PC/10.10.11.128
		System.out.println(ip.getHostName());//hqq-PC
		System.out.println(ip.getHostAddress());//10.10.11.128
		
		ip = InetAddress.getByName("www.inspur.com");
		System.out.println(ip);//www.inspur.com/218.57.146.152
		System.out.println(ip.getHostName());//www.inspur.com
		System.out.println(ip.getHostAddress());//218.57.146.152
		
		int timeout = 5000;//单位毫秒
		System.out.println(ip.isReachable(timeout));//是否可以ping通,返回true或false
		
		
	}
}

猜你喜欢

转载自huangqiqing123.iteye.com/blog/1408227
今日推荐