Java --> 网络编程

2022/7/25 1:52:53

本文主要是介绍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的地址属于个人隐私,结果就不完全展示了,在此就打了个小小的



这篇关于Java --> 网络编程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程