Network - 网络端口

时间:2024-10-09 08:03:29
网络端口是网络中的一个虚拟端点,用于将流量导向计算机或设备上的特定服务或应用程序。端口有助于区分系统上运行的不同服务,确保正确类型的网络流量到达正确的应用程序。每个端口都与一个 IP 地址和一个通信协议(通常为 TCP 或 UDP)相关联。
A network port is a virtual endpoint used in networking to direct traffic to a specific service or application on a computer or device. Ports help differentiate between different services running on a system, ensuring that the right type of network traffic reaches the right application. Each port is associated with an IP address and a communication protocol (usually TCP or UDP).
Key Concepts:
- Port Number: 
端口号是一个 16 位整数,范围从 0 到 65535。常见的端口包括
  - HTTP: 80
  - HTTPS: 443
  - SSH: 22
  - FTP: 21
A port number is a 16-bit integer that ranges from 0 to 65535. Common ports include:
  - HTTP: 80
  - HTTPS: 443
  - SSH: 22
  - FTP: 21
- Protocols:
  - TCP(传输控制协议): 确保在客户端和服务器之间可靠、有序并经过错误检查地传输数据。
  - UDP(用户数据报协议): 速度更快,但不可靠,用于对速度要求较高的应用,如视频流或 DNS。
  - TCP (Transmission Control Protocol): Ensures reliable, ordered, and error-checked delivery of data between client and server.
  - UDP (User Datagram Protocol): Faster but unreliable, used in applications where speed is crucial, like video streaming or DNS.
Server-Client Communication with Ports
在客户端-服务器模式中,服务器和客户端都使用端口建立通信。
In a client-server model, both the server and client use ports to establish communication.
1. Server-Side Port:
   - 服务器在特定端口(称为 “监听端口”)上监听传入的连接。这个端口是固定的,客户端事先就知道。
   - 例如 网络服务器通常监听 80 端口(HTTP)或 443 端口(HTTPS)。
   - The server listens on a specific port (called a "listening port") for incoming connections. This port is fixed and known to the client in advance.
   - Example: A web server typically listens on port 80 (HTTP) or 443 (HTTPS).
   
2. Client-Side Port:
   - 客户端使用从一系列可用端口(通常为 49152 至 65535,称为 “短暂端口”)中随机分配的端口启动通信。
   - 当客户端连接到服务器时,它知道服务器的 IP 地址和端口号,但它自己的端口号是由操作系统动态分配的。
   
   - The client uses a randomly assigned port from a range of available ports (usually 49152 to 65535, called "ephemeral ports") to initiate communication.
   - When a client connects to the server, it knows the server's IP address and port number, but its own port number is dynamically assigned by the operating system.
   
   Example Workflow:
   - 客户希望浏览一个网站(HTTP):
     1. 客户端向位于 `server_ip:80`(HTTP)的服务器发送请求。
     2. 客户端使用一个短暂端口,例如 `client_ip:52000`。
     3. 服务器响应客户端,向`client_ip:52000`发送数据。
     4. 建立通信,直至会话关闭。
   - A client wants to browse a website (HTTP):
     1. The client sends a request to the server at `server_ip:80` (for HTTP).
     2. The client uses an ephemeral port, say `client_ip:52000`.
     3. The server responds back to the client, sending data to `client_ip:52000`.
     4. Communication is established until the session is closed.
Example of Port Use in Client-Server Communication
Web Browser (Client) and Web Server
- Server:
  - 网络服务器(如 Apache 或 Nginx)通过 80 端口(HTTP)或 443 端口(HTTPS)监听。
  - A web server (e.g., Apache or Nginx) listens on port 80 (for HTTP) or 443 (for HTTPS).
  
- Client:
  - 浏览器(客户端)通过选择一个短暂端口(如 50321)来启动连接。
  - The browser (client) initiates a connection by choosing an ephemeral port, e.g., 50321.
  
   Example:
  - Client: `192.168.1.10:50321` (source)
  - Server: `93.184.216.34:80` (destination)
客户端请求一个网页,服务器使用相应的端口进行回复。
The client requests a webpage, and the server replies using the respective ports.
Why Use Specific Ports?
- Servers use fixed or well-known ports so that clients know where to connect. For example:
- 服务器使用固定或众所周知的端口,以便客户知道从哪里连接。例如:
  - DNS: Port 53 (UDP)
  - SMTP (Email): Port 25 (TCP)
  - FTP: Port 21 (TCP)
- 客户端不需要固定的端口,因为它们会启动通信。它们的端口号为每个会话随机分配,确保多个应用程序或会话可以同时运行而不会发生冲突。
- Clients do not need fixed ports because they initiate the communication. Their port numbers are randomly assigned for each session, ensuring that multiple applications or sessions can run simultaneously without conflict.
Bi-Directional Communication
连接一旦建立:
- 服务器和客户端使用各自的端口进行双向通信。
- 服务器使用客户端发出请求的端口向客户端发回响应。
Once a connection is established:
- The server and client use both their respective ports for bi-directional communication.
- The server sends responses back to the client using the port from which the client made the request.
Summary:
- 服务器使用一个特定的、众所周知的端口来监听传入请求(如 HTTP 的 80 端口)。
- 客户端使用随机分配的短暂端口与服务器通信。
- 两者都使用这些端口来促进网络上的双向通信。
- The server uses a specific, well-known port to listen for incoming requests (e.g., port 80 for HTTP).
- The client uses a randomly assigned ephemeral port to communicate with the server.
- Both use these ports to facilitate two-way communication over the network.
端口可确保多个服务和应用程序在同一台机器上共存,并正确路由到各自的网络请求。
Ports ensure that multiple services and applications can coexist on the same machine and be correctly routed to their respective network requests.