/*
一、网络编程中有两个主要的问题:
1.如何准确的定位网络上一台或多台主机;定位主机上的特定的应用
2.找到主机后如何可靠高效的进行数据传输
二、网络编程中的两个要素:
1.IP和端口号
2.提供网络通信协议TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)
三、通信要素一:IP和端口号
1.IP:唯一的标识Internet上的计算机(通信实体)
2.在Java中使用InetAddress类代表IP
3.IP分类:IPv4 和 IPv6; 万维网和局域网
4.域名: www.baidu.com www.mi.com www.sina.com
5.本地回路地址:127.0.0.1 对应着:localhost
6.如何实例化InetAddress:
getByName(String host)
getLocalHost()
两个常用方法:getHostName() / getHostAddress()
7.端口号:正在计算机上运行的进程
要求:不同的进程有不同的端口号
范围:被规定为一个16位的整数0-65535
8.端口号与IP地址的组合得出一个网络套接字:Socket
*/
public class InetAddressTest {
public static void main(String[] args) {
InetAddress inet1 = null;
try {
inet1 = InetAddress.getByName("192.168.10.14");
System.out.println(inet1);
InetAddress inet2 = InetAddress.getByName("www.baidu.com");
System.out.println(inet2);
InetAddress inet3 = InetAddress.getByName("127.0.0.1");
System.out.println(inet3);
//获取本地ip
InetAddress inet4 = InetAddress.getLocalHost();
System.out.println(inet4);
//getHostName()
System.out.println(inet2.getHostName());
//getHostAddress()
System.out.println(inet2.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
[网络编程]InetAddress类的实例化
于 2022-05-09 11:25:39 首次发布