端口号背后的技术是什么?

What is the technology behind port numbers?

我知道端口号用于识别服务器上的不同进程运行,以便多个进程可以使用相同的网络资源。但它在内部是如何工作的?

例如,如果对网站 http://www.my-awesome-website.com:80 的请求到达服务器,服务器如何知道端口 80 上有 Web 服务器 运行?我的意思是,从获取请求到发现 Web 服务器 运行 在端口 80 上并将请求转发到 Web 服务器之间,请求管道是什么样的?

端口号只是某些传输层协议的地址,例如 TCP 和 UDP,就像 IP 地址用于第 3 层协议,MAC 地址用于第 2 层协议一样.并不是所有的传输层协议都使用端口,每个传输层协议都独立维护自己的端口,所以TCP的80端口和UDP的80端口不一样,可以被不同的应用同时使用。

第 2 层地址仅与 LAN 链路相关,第 3 层地址仅与第 3 层网络上的主机到主机相关,第 4 层地址与应用程序到应用程序相关。

IANA 在 Service Name and Transport Protocol Port Number Registry.

注册端口并维护官方注册表列表

来自RFC 793, TRANSMISSION CONTROL PROTOCOL

Multiplexing:

To allow for many processes within a single Host to use TCP communication facilities simultaneously, the TCP provides a set of addresses or ports within each host. Concatenated with the network and host addresses from the internet communication layer, this forms a socket. A pair of sockets uniquely identifies each connection. That is, a socket may be simultaneously used in multiple connections.

The binding of ports to processes is handled independently by each Host. However, it proves useful to attach frequently used processes (e.g., a "logger" or timesharing service) to fixed sockets which are made known to the public. These services can then be accessed through the known addresses. Establishing and learning the port addresses of other processes may involve more dynamic mechanisms.

Connections:

The reliability and flow control mechanisms described above require that TCPs initialize and maintain certain status information for each data stream. The combination of this information, including sockets, sequence numbers, and window sizes, is called a connection. Each connection is uniquely specified by a pair of sockets identifying its two sides.

When two processes wish to communicate, their TCP's must first establish a connection (initialize the status information on each side). When their communication is complete, the connection is terminated or closed to free the resources for other uses.

Since connections must be established between unreliable hosts and over the unreliable internet communication system, a handshake mechanism with clock-based sequence numbers is used to avoid erroneous initialization of connections.

打开套接字后(类似于打开的文件,但用于网络通信),套接字的用户可以直接使用临时端口(由OS选择),这是典型的如果应用程序是客户端应用程序。

服务器进程做的是调用bind()套接字API调用为套接字设置端口,然后在TCP套接字的情况下调用listen()开始监听用于传入的连接请求。

因为 bind() 调用 OS 会知道这个特定的套接字是接收发送到特定端口号的数据的套接字。

通过网络发送的数据包包含源和目标 IP 地址以及源和目标端口:

http://www.techrepublic.com/article/exploring-the-anatomy-of-a-data-packet/

所以 OS 有一个数据结构,其中打开的套接字按端口号列出,它将接收到的数据传递到正确套接字的输入缓冲区。发送的数据会被发送套接字的端口号标记。