一、InetAddress
InetAddress不存在构造函数,所以只能通过静态类函数来返回一个InetAddress。
1、创建InetAddress
InetAddress.getLocalHost();
//返回本地的主机对象
InetAddress.getByName(String HostName);
//根据机器名字来获取对象 比如www.baidu.com
InetAddress.getByName(String Ip);
//根据IP地址来获取对象 比如14.215.177.38
InetAddress.getByAddress(String host,byte[] addr)
InetAddress getByAddress(byte[] addr)
static InetAddress[] getAllByName(String host)
//因为有可能会有多个主机
其中最常用的就是通过来获取对象,因为不管你通过主机名还是IP地址都能够创建InetAddress对象
InetAddress inetaddress= InetAddress.getByName(String hostName);
2、主要方法
getHostAddress() 返回IP地址
getHostName() 返回计算机名
还有..忘了
3、代码示例
1、获取本机信息
//获取本机
InetAddress internetAddress=InetAddress.getLocalHost();
System.out.println(internetAddress);
System.out.println(internetAddress.getHostAddress());//获取到IP地址
System.out.println(internetAddress.getHostName());//获取计算机名
实现结果:
LAPTOP-4MF1A7OE/192.168.76.1
192.168.76.1
LAPTOP-4MF1A7OE
2、获取特定网站
InetAddress[] inetAddresses=InetAddress.getAllByName("www.baidu.com");
for(int i=0;i<inetAddresses.length;i++)
{
System.out.println("百度的IP地址"+inetAddresses[i].getHostAddress());
System.out.println("网站名字也是主机名"+inetAddresses[i].getHostName());
}
实现结果:
百度的IP地址14.215.177.38
网站名字也是主机名www.baidu.com
百度的IP地址14.215.177.39
网站名字也是主机名www.baidu.com
3、通过IP地址
InetAddress internetAddress=InetAddress.getByName("192.168.76.1");
System.out.println(internetAddress.getHostAddress());
System.out.println(internetAddress.getHostName());
实现结果:
192.168.76.1
LAPTOP-4MF1A7OE
注意:如果通过Ip不允许被解析的话,那么getHostName()返回仍然是所传入的IP
2、InetSocketAddress
1、获取对象
InetSocketAddress可以构造函数来进行创建,传入InetAddress对象和端口,或者传入主机名和端口。
InetSocketAddress(int)
InetSocketAddress(InetAddress, int)
InetSocketAddress(String, int)
InetSocketAddress(int, String)
2、方法
public final int getPort() 获取端口号
public final String getHostName() 获取主机名
public final boolean isUnresolved() 查看是否已解析地址,如果已经解析成InetAddress则返回false
public final InetAddress getAddress() 解析成InetAddress ,如果没有解析成功则返回Null
3、代码实现
1、本机
InetSocketAddress inetSocketAddress=new InetSocketAddress("192.168.76.1",9999);
System.out.println( "主机名为"+inetSocketAddress.getHostName());
System.out.println("端口号为"+inetSocketAddress.getPort());
System.out.println("是否被解析InetAddress---"+inetSocketAddress.isUnresolved());
InetAddress inetAddress=inetSocketAddress.getAddress();
System.out.println("---------");
System.out.println(inetAddress.getHostName());
System.out.println(inetAddress.getHostAddress());
System.out.println("是否被解析InetAddress---"+inetSocketAddress.isUnresolved());
实现结果:
主机名为LAPTOP-4MF1A7OE
端口号为9999
是否被解析InetAddress---false
---------
LAPTOP-4MF1A7OE
192.168.76.1
是否被解析InetAddress---false
2、百度
InetSocketAddress inetSocketAddress=new InetSocketAddress("www.baidu.com",9999);
System.out.println( "主机名为"+inetSocketAddress.getHostName());
System.out.println("端口号为"+inetSocketAddress.getPort());
System.out.println("是否被解析InetAddress---"+inetSocketAddress.isUnresolved());
InetAddress inetAddress=inetSocketAddress.getAddress();
System.out.println("---------");
System.out.println(inetAddress.getHostName());
System.out.println(inetAddress.getHostAddress());
System.out.println("是否被解析InetAddress---"+inetSocketAddress.isUnresolved());
实现结果:
主机名为www.baidu.com
端口号为9999
是否被解析InetAddress---false
---------
www.baidu.com
14.215.177.38
是否被解析InetAddress---false
-----------那我就祝我自己生日快乐,O(∩_∩)O哈哈~