问题描述
根据我的理解,每个HTTP请求都使用自己的TCP连接(如果我错了,请纠正我)。所以,假设有两个当前连接到同一台服务器。例如,客户端javascript代码在获得对第一个的响应之前,使用XMLHttpRequest对象一个接一个地触发几个AJAX POST请求。所以我们讨论的是同一个服务器的两个连接,每个连接等待一个响应,以便将它路由到每个单独的回调函数。
From what I understand, each HTTP request uses its own TCP connection (please correct me if i'm wrong). So, let's say that there are two current connections to the same server. For example, client side javascript code triggering a couple of AJAX POST requests using the XMLHttpRequest object, one right after the other, before getting the response to the first one. So we're talking about two connections to the same server, each waiting for a response in order to route it to each separate callback function.
现在我不明白这一点:TCP数据包包括源和目标IP和端口,但这两个连接都没有相同的src和dest ip地址和端口80?如何区分数据包并将其路由到适当的位置?是否与每个连接的数据包序列号有什么关系?
Now here's the thing that I don't understand: The TCP packet includes source and destination ip and port, but won't both of these connections have the same src and dest ip addresses, and port 80? How can the packets be differentiated and routed to appropriately? Does it have anything to do with the packet sequence number which is different for each connection?
推荐答案
当您的浏览器创建新连接时到HTTP服务器,它使用不同的源端口。
When your browser creates a new connection to the HTTP server, it uses a different source port.
例如,假设您的浏览器创建了两个与服务器的连接,并且您的IP地址为60.12.34.56。第一个连接可能源自源端口60123,第二个连接源自60127.这嵌入在发送到服务器的每个数据包的TCP头中。当服务器回复每个连接时,它使用适当的端口(例如60123或60127),以便数据包返回到正确的位置。
For example, say your browser creates two connections to a server and that your IP address is 60.12.34.56. The first connection might originate from source port 60123 and the second from 60127. This is embedded in the TCP header of each packet sent to the server. When the server replies to each connection, it uses the appropriate port (e.g. 60123 or 60127) so that the packet makes it back to the right spot.
最好的一个了解这一点的方法是下载,并观察您自己网络上的流量。它将向您展示这一点以及更多内容。
One of the best ways to learn about this is to download Wireshark and just observe traffic on your own network. It will show you this and much more.
此外,这还可以深入了解网络地址转换(NAT)如何在路由器上运行。您可以让许多计算机共享相同的IP地址,路由器将重写请求以使用不同的端口,这样两台计算机就可以同时连接到AOL Instant Messenger这样的地方。
Additionally, this gives insight into how Network Address Translation (NAT) works on a router. You can have many computers share the same IP address and the router will rewrite the request to use a different port so that two computers can simultaneously connect to places like AOL Instant Messenger.
这篇关于如何识别HTTP请求中的不同TCP连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!