java获取主机信息大全,网络编程:Java获取网络主机信息

c3de63d5ba3f1e30f7475d918f26204c.png

java.net.InetAddress类表示互联网协议 (IP) 地址。

有两个子类:Inet4Address, Inet6Address

通过 InetAddress可以方便获取一个域名下的IP,也可以获取一个IP的主机名。

下面是例子,通过程序查看51cto主机的IP信息,51cto是不让ping的。

代码:

import java.io.IOException;

import java.net.InetAddress;

public class TestInetAddress {

public static void main(String[] args) throws IOException {

test();

}

public static void test() throws IOException {

//获取本机的IP地址

InetAddress address1 = InetAddress.getLocalHost();

System.out.println(address1.getHostAddress());

System.out.println(address1.toString());

System.out.println("---------------");

//获取51cot的IP地址

InetAddress address2 = InetAddress.getByName("www.51cto.com");

System.out.println("getHostAddress:\t" + address2.getHostAddress());

System.out.println("getHostName:\t" + address2.getHostName());

System.out.println("---------------");

//获取我博客的IP地址

InetAddress address3 = InetAddress.getByName("lavasoft.blog.51cto.com");

System.out.println("getHostAddress:\t" + address3.getHostAddress());

System.out.println("getHostName:\t" + address3.getHostName());

//直接用ip地址构建

System.out.println(InetAddress.getByName("211.103.156.224"));

System.out.println("---------------");

//获取www.google.com域名下所有的IP地址

//InetAddress[] address4 = InetAddress.getAllByName("www.microsoft.com");

InetAddress[] address4 = InetAddress.getAllByName("www.google.com");

for (InetAddress address : address4) {

//System.out.println(address.getHostAddress());

System.out.println(address);

}

InetAddress address5 = InetAddress.getByName("211.103.156.229");

System.out.println(address5.isReachable(10));

}

}

运行输出结果:

192.168.1.2

lavasoft/192.168.1.2

---------------

getHostAddress: 211.103.156.229

getHostName: www.51cto.com

---------------

getHostAddress: 211.103.156.224

getHostName: lavasoft.blog.51cto.com

/211.103.156.224

---------------

www.google.com/64.233.189.99

www.google.com/64.233.189.104

www.google.com/64.233.189.147

false

Process finished with exit code 0

2829f2ca24f0d0090cbba57f30759f23.png

网络编程:Java获取网络主机信息.doc

下载Word文档到电脑,方便收藏和打印[全文共1212字]

编辑推荐:

8b95f2eb3d3f7ce4dc3bf1178c74941e.png

8b95f2eb3d3f7ce4dc3bf1178c74941e.png

8b95f2eb3d3f7ce4dc3bf1178c74941e.png

8b95f2eb3d3f7ce4dc3bf1178c74941e.png

8b95f2eb3d3f7ce4dc3bf1178c74941e.png

下载Word文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值