网络通信 1

说说网络通信至少需要几个要素 :IP、端口、协议。

IP地址是做什么的,具体有几种:定位网络上的设备的,有IPv4 , IPv6

如何查看本机IP地址,如何看是否与对方互通:ipcofig , ping 192.168.10.23

本机IP是谁? 127.0.0.1或者是localhost

/*
InetAddress类成员方法
static InetAddress getLocalHost()
*返回本主机的地址对象
static InetAddress getByName(String host)
*得到指定主机的IP地址对象,参数是域名或者IP地址
static InetAddress getHostName()
*获取此IP地址的主机名
static InetAddress getHostAddress()
*返回IP地址字符串
*/
public class InetAddressDemo01 {
public static void main(String[] args) throws Exception{
//1.获取本机地址对象
InetAddress ip1 = InetAddress.getLocalHost();
System.out.println(ip1.getHostName());
System.out.println(ip1.getHostAddress());

//2.获取域名ip对象
InetAddress ip2 = InetAddress.getByName("www.baidu.com");
System.out.println(ip2.getHostName());
System.out.println(ip2.getHostAddress());

//3.获取公网IP对象
InetAddress ip3 = InetAddress.getByName("112.80.248.76");
System.out.println(ip3.getHostName());
System.out.println(ip3.getHostAddress());

//4.判断是否能通 : ping 5s之前测试是否可通
System.out.println(ip3.isReachable(5000));
}
}

posted on   我要当程序源  阅读(26)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示