编程语言
首页 > 编程语言> > Java --> 网络编程

Java --> 网络编程

作者:互联网

 

 

 1 import java.net.InetAddress;
 2 
 3 public class InetAddressDemo {
 4     public static void main(String[] args) throws Exception{
 5         InetAddress localHost = InetAddress.getLocalHost();
 6         System.out.println(localHost); //重写了toString
 7         System.out.println(localHost.getHostName());
 8         System.out.println(localHost.getHostAddress());
 9 
10         System.out.println("---------------------------");
11 
12         //获取域名ip对象
13         InetAddress localHost2 = InetAddress.getByName("www.baidu.com");
14         System.out.println(localHost2);
15         System.out.println(localHost2.getHostName());
16         System.out.println(localHost2.getHostAddress());
17 
18         System.out.println("---------------------------");
19 
20         //获取公网ip对象
21         InetAddress localHost3 = InetAddress.getByName("110.242.68.4");
22         System.out.println(localHost3);
23         System.out.println(localHost3.getHostName());
24         System.out.println(localHost3.getHostAddress());
25 
26         System.out.println("---------------------------");
27 
28         //判断是否互通:ping 5s之前测试是否可通
29         System.out.println(localHost3.isReachable(5000)); //5s能否和百度连通
30     }
31 }

 ps : 由于个人IPv4的地址属于个人隐私,结果就不完全展示了,在此就打了个小小的

标签:DatagramSocket,DatagramPacket,Java,--,编程,System,println,buf,out
来源: https://www.cnblogs.com/yumengqifei/p/16483658.html