问题描述
通常,Web 服务器正在侦听通过端口 80 的任何传入连接.所以,我的问题是,套接字编程的一般概念不应该是端口 80 用于侦听传入连接.但是在服务器接受连接后,它将使用另一个端口,例如端口 12345 与客户端通信.但是,当我查看wireshark时,服务器在通信期间始终使用端口80.我在这里很困惑.
Usually a web server is listening to any incoming connection through port 80. So, my question is that shouldn't it be that in general concept of socket programming is that port 80 is for listen for incoming connection. But then after the server accepted the connection, it will use another port e.g port 12345 to communicate with the client. But, when I look into the wireshark, the server is always using port 80 during the communication. I am confused here.
那么如果 https://www.facebook.com:443,它有数十万一秒钟连接到它.一个端口能处理这么大的流量吗?
So what if https://www.facebook.com:443, it has hundreds of thousands of connection to the it at a second. Is it possible for a single port to handle such a large amount of traffic?
推荐答案
一个特定的套接字由一个 5 元组(即 5 个特定属性的列表)唯一标识.这些属性是:
A particular socket is uniquely identified by a 5-tuple (i.e. a list of 5 particular properties.) Those properties are:
- 源 IP 地址
- 目标 IP 地址
- 源端口号
- 目的端口号
- 传输协议(通常是 TCP 或 UDP)
对于同时打开的套接字,这些参数必须是唯一的.您可能在这里感到困惑的是客户端发生的情况与 TCP 中服务器端发生的情况.不管所讨论的应用程序协议是什么(HTTP、FTP、SMTP 等等),TCP 的行为方式都是一样的.
These parameters must be unique for sockets that are open at the same time. Where you're probably getting confused here is what happens on the client side vs. what happens on the server side in TCP. Regardless of the application protocol in question (HTTP, FTP, SMTP, whatever,) TCP behaves the same way.
当您在客户端打开一个套接字时,它会为新的传出连接选择一个随机的高数端口.这是必需的,否则您将无法在同一台计算机上打开两个单独的套接字到同一台服务器.因为这样做是完全合理的(这在 Web 服务器的情况下很常见,例如在两个单独的选项卡中打开 stackoverflow.com)并且每个套接字的 5 元组必须是唯一的,一个随机的高数端口用作源端口.但是,这些套接字中的每一个都将连接到 stackoverflow.com 的网络服务器上的端口 80.
When you open a socket on the client side, it will select a random high-number port for the new outgoing connection. This is required, otherwise you would be unable to open two separate sockets on the same computer to the same server. Since it's entirely reasonable to want to do that (and it's very common in the case of web servers, such as having stackoverflow.com open in two separate tabs) and the 5-tuple for each socket must be unique, a random high-number port is used as the source port. However, each of those sockets will connect to port 80 at stackoverflow.com's webserver.
在服务器端,stackoverflow.com 已经可以区分来自客户端的这两个不同的套接字,同样,因为它们已经具有不同的客户端端口号.当它看到来自您的浏览器的传入请求数据包时,它知道由于源端口号不同,它已打开与您一起响应的套接字.同样,当它想向您发送响应数据包时,它可以通过将目标端口号设置为从中获取请求的客户端端口号将其发送到您端的正确端点.
On the server side of things, stackoverflow.com can already distinguish between those two different sockets from your client, again, because they already have different client-side port numbers. When it sees an incoming request packet from your browser, it knows which of the sockets it has open with you to respond to because of the different source port number. Similarly, when it wants to send a response packet to you, it can send it to the correct endpoint on your side by setting the destination port number to the client-side port number it got the request from.
最重要的是,每个客户端连接都没有必要在服务器端有一个单独的端口号,因为服务器已经可以通过其客户端 IP 地址和客户端端口号唯一地标识每个客户端连接.无论应用层协议如何,这都是 TCP(和 UDP)套接字的工作方式.
The bottom line is that it's unnecessary for each client connection to have a separate port number on the server's side because the server can already uniquely identify each client connection by its client IP address and client-side port number. This is the way TCP (and UDP) sockets work regardless of application-layer protocol.
这篇关于套接字编程 - 为什么 Web 服务器即使在接受连接后仍然使用侦听端口 80 与客户端通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!