1.1 什么是计算机网络


1.2 网络通信的两个要素


1.3 IP地址
package web;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class testInetAddress {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress inetAddress=InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress);
InetAddress inetAddress2=InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3=InetAddress.getLocalHost();
System.out.println(inetAddress3);
//查询网站ip地址
InetAddress inetAddress1=InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress1);
//常用方法
System.out.println(inetAddress1.getAddress());
System.out.println(inetAddress1.getCanonicalHostName());//规范的名字
System.out.println(inetAddress1.getHostAddress());//ip
System.out.println(inetAddress1.getHostName());//域名或者本机电脑的名字
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
1.4 端口
端口对应计算机上一个程序的进程
- 不同的进程有不同的端口号,区分不同的程序
- 端口范围0-65535
- TCP和UDP协议,每个协议都有65535个端口,eg:TCP可以用80 UDP同时也可以使用80端口,单个协议下面,端口号不能冲突,不同协议的端口号可以冲突
端口分类:
- 公有端口:0-1023 (HTTP:80 HTTPS:443 FTP:21 Telnet:23)
- 程序注册端口:2014-49151 分配给用户或程序 (Tomcat:8080 MySQL:3306 Oracle:1521)
- 动态、私有端口:49152-65535
netstat -ano //查看所有的端口
netstat -ano | findstr "5900"
tasklist | findstr "8696" //查看指定端口的进程
| 在Linux中叫管道,过滤的意思
Ctrl+Shift+ESC 打开任务管理器
package web;
import java.net.InetSocketAddress;
public class testInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 8080);
System.out.println(inetSocketAddress);
System.out.println(inetSocketAddress2);
System.out.println(inetSocketAddress.getPort());
System.out.println(inetSocketAddress.getAddress());
System.out.println(inetSocketAddress.getHostName());
}
}
1.5 通信协议


1.6 TCP实现聊天


本文介绍了计算机网络的基本概念,包括网络通信的两大要素、IP地址的工作原理及其获取方式,并详细讲解了端口的作用及分类,最后简单提及了通信协议及TCP协议的应用。





