package com.test;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test
{
public static void main(String[] args)
{
try
{
// 此类中没有定义构造器,是通过静态方法返回此类的对象实例
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia.getHostAddress());//10.85.25.18
System.out.println(ia.getHostName());//tkf77612
System.out.println(ia.getAddress());//[B@c17164
System.out.println(ia.getCanonicalHostName());//tkf77612.china.huawei.com
System.out.println(InetAddress.getByName("www.huawe.com"));//www.huawe.com/58.215.65.91
System.out.println(InetAddress.getByName(null));//localhost/127.0.0.1
byte[] bs = new byte[]{ 127, 123, 0, 1 };
InetAddress ia2 = InetAddress.getByAddress("dylan", bs);
System.out.println(ia2);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
}
}
InetAddress类详解
最新推荐文章于 2023-10-28 18:16:42 发布