3、InetAddress示例代码
publicclassDemo1
{
publicstaticvoidmain(String[]args)
{
InetAddressina;
try
{
ina=InetAddress.getLocalHost();
System.out.println(ina);
System.out.println(ina.getAddress());//返回此InetAddress对象的原始IP地址
System.out.println(ina.getHostAddress());//返回IP地址字符串(以文本表现形式)。
System.out.println(ina.getHostName());//获取此IP地址的主机名
System.out.println(ina.getLocalHost());//返回本地主机
}
catch(UnknownHostExceptione)
{
//TODO自动生成的catch块
e.printStackTrace();
}
InetAddressina1;
try
{
ina1=InetAddress.getByName("192.168.1.119");
System.out.println(ina1);
}
catch(UnknownHostExceptione)
{
//TODO自动生成的catch块
e.printStackTrace();
}
InetAddress[]ina2;
try
{
ina2=InetAddress.getAllByName("www.microsoft.com");
for(inti=0;i<ina2.length;i++)
System.out.println(ina2[i]);
}catch(UnknownHostExceptione)
{
//TODO自动生成的catch块
e.printStackTrace();
}
}
}