Java InetAddress类的方法
2023-04-18 16:17:31 时间
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/218
Java InetAddress类的方法
这个类表示互联网协议(IP)地址。下面列出了 InetAddress 类常用的方法:
序号 | 方法描述 |
---|---|
1 | static InetAddress getByAddress(byte[] addr) 在给定原始 IP 地址的情况下,返回 InetAddress 对象。 |
2 | static InetAddress getByAddress(String host, byte[] addr) 根据提供的主机名和 IP 地址创建 InetAddress。 |
3 | static InetAddress getByName(String host) 在给定主机名的情况下确定主机的 IP 地址。 |
4 | String getHostAddress() 返回 IP 地址字符串(以文本表现形式)。 |
5 | String getHostName() 获取此 IP 地址的主机名。 |
6 | static InetAddress getLocalHost() 返回本地主机。 |
7 | String toString() 将此 IP 地址转换为 String。 |
package com.joshua317;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Main {
public static void main(String[] args){
InetAddressTest inetAddress = new InetAddressTest();
try {
//根据域名获取
InetAddress address1 = inetAddress.getInetAddress("www.baidu.com");
System.out.println(address1);
System.out.println(address1.getHostName() + "--" +address1.getHostAddress());
//根据ip获取
InetAddress address2 = inetAddress.getInetAddressByIp("103.235.46.39");
System.out.println(address2);
System.out.println(address2.getHostName() + "--" +address2.getHostAddress());
//根据byte类型获取
byte[] bytes = {103,34,46,39};
InetAddress address3 = inetAddress.getInetAddress(bytes);
System.out.println(address3);
//如果数值超出Byte最大范围,需要进行转换,例如:(byte)(235 &0xff)
System.out.println("Byte最小值:" + Byte.MIN_VALUE);
System.out.println("Byte最大值:" + Byte.MAX_VALUE);
byte[] bytes2 = {103,(byte)(235 &0xff),46,39};
InetAddress address4 = inetAddress.getInetAddress(bytes2);
System.out.println(address4);
//查看是否是回环地址
InetAddress address5 = inetAddress.getInetAddressByIp("127.0.0.1");
System.out.println(address5.isLoopbackAddress());
//查看是否是本地地址
System.out.println(address5.isLinkLocalAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
@SuppressWarnings("all")
class InetAddressTest {
public InetAddressTest() {
}
public InetAddress getInetAddress(String host) throws UnknownHostException{
return InetAddress.getByName(host);
}
public InetAddress getInetAddress(byte[] host) throws UnknownHostException{
return InetAddress.getByAddress(host);
}
public InetAddress getInetAddress(String host,byte[] ipByte) throws UnknownHostException{
return InetAddress.getByAddress(host, ipByte);
}
public InetAddress getInetAddressByIp(String ip) throws UnknownHostException{
String[] ipStr = ip.split("\.");
byte[] ipByte = new byte[4];
for (int i =0; i<ipByte.length; i++) {
ipByte[i] = (byte) (Integer.parseInt(ipStr[i]) &0xff);
}
return InetAddress.getByAddress(ipByte);
}
}
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/218
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击