网络通信 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 2022-04-01 13:24  我要当程序源  阅读(19)  评论(0编辑  收藏  举报

导航