标签:res system array 面向对象 imp unit dem host 根据
根据java面向对象的原理,ip地址也被封装成了对象,InetAdressimport java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import org.junit.Test;
/**
* 获取ip地址
* @author WangShuang
*
*/
public class Demo {
@Test
public void run1() {
try {
//获取本机的ip地址
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
//已知别人的域名 获取别人的ip地址对象
InetAddress byName = InetAddress.getByName("www.baidu.com");
System.out.println(byName);
String hostAddress = byName.getHostAddress();//返回ip地址的字符串表现形式
System.out.println(hostAddress);
InetAddress[] allByName = InetAddress.getAllByName("www.baidu.com");
System.out.println(Arrays.toString(allByName));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
标签:res system array 面向对象 imp unit dem host 根据
原文地址:http://blog.51cto.com/13579086/2069332