绵阳市三台县城乡建设局网站成都外贸seo
1.1 概述
java程序员面对统一的网络编程环境
B/S 架构 和 C/S架构
1.2 网络通信的两个要素
通信双方的地址:ip + 端口号
网络通信协议:TCP/IP协议(事实上的国际规则)、OSI模型(理想化)
1.3 Inet Address
本地回环地址(hostAddress):127.0.0.1
主机名(hostName):localhost
IP地址分类
分类一:
ipv4:4个字节,即4个0-255,
ipv6:16个字节,写成8个无符号整数,
分类二:
公网地址:万维网使用
私有地址:198.168开头的,专为组织机构内部使用
InetAddress类
www.baidu.com => DNS解析(150.194.117.44)=> 本机host文件判断是否有域名地址 =>DNS服务器找主机
public static void main(String[] args) {try {// 获取指定IP地址InetAddress inetAddress1 = InetAddress.getByName("192.168.1.1");System.out.println(inetAddress1); ///192.168.1.1InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");System.out.println(inetAddress2); //www.baidu.com/110.242.68.4// 获取本地地址InetAddress inetAddress3 = InetAddress.getByName("127.0.01");System.out.println(inetAddress3); // /127.0.0.1InetAddress inetAddress4 = InetAddress.getByName("localhost");System.out.println(inetAddress4); // localhost/127.0.0.1InetAddress inetAddress5 = InetAddress.getLocalHost();System.out.println(inetAddress5); // DESKTOP-OKFCPBN/10.61.5.154// getHostNameSystem.out.println(inetAddress2.getHostName()); // www.baidu.comSystem.out.println(inetAddress2.getAddress()); // [B@b1bc7edSystem.out.println(inetAddress2.getCanonicalHostName()); // 110.242.68.4}catch (UnknownHostException e){e.printStackTrace();}}
1.4 端口号
标识正在计算机上运行的进程 0-65535
公认端口: 0-1023, 预先定义的服务通信占用
http: 80
https: 443
ftp: 21
telnet: 23
注册端口:1024-49151, 分配给用户进程、程序
Tomcat:8080
mysql: 3306
oracle: 1521
动态私有端口:
49152-65535
端口+IP,得到一个网络套接字,Socket。所以网络编程也叫Socket编程。
public static void main(String[] args) {InetSocketAddress inetSocketAddress1 = new InetSocketAddress("127.0.0.1", 9090);System.out.println(inetSocketAddress1.getHostName()); // 127.0.0.1System.out.println(inetSocketAddress1.getAddress()); // 地址:127.0.0.1/127.0.0.1System.out.println(inetSocketAddress1.getPort()); // 端口:9090}