端口号在TCP中如何真正起作用? [英] How does Port Number really work in TCP?

查看:82
本文介绍了端口号在TCP中如何真正起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://serverfault .com/questions/296603/understanding-ports-如何做多个浏览器选项卡-在相同时间进行通信

如何应用程序可以使用端口80/HTTP而不与浏览器冲突吗?

如何多个客户端同时连接到服务器上的一个端口(例如80)吗?

我已经阅读了上述问题,但答案似乎不一致.

I have read the above questions but it seems the answers are inconsistent.

我想知道到底是什么定义了套接字连接?

I want to know what exactly defines a socket connection, is it:

(sockid, source ip, source port, dest ip, dest port)

或仅:

(source ip, source port, dest ip, dest port)

两个不同的进程(例如,两个不同的浏览器)可以与同一源端口上的Web服务器通信吗? (默认情况下,目标端口将是相同的)

Can two different processes (e.g, two different browsers) communicate with a web server on the same source port? (the dest port would be the same by default)

如果在同一浏览器中使用不同的标签,会发生什么?

What will happen in the case of different tabs in the same browser?

此外,如答案之一所述,单个网页可以同时连接到多个服务器(例如,广告服务器).同时连接多个服务器时,网络浏览器(例如Chrome,Firefox)是否使用相同的端口连接到每个服务器,或者每个服务器使用不同的端口?

Also, as mentioned in one of the answers, a single web page can connect to multiple servers (e.g., ad servers) simultaneously. When connecting to multiple servers simultaneously, does the web browser (e.g., Chrome, Firefox) connect to each server using the same port, or does it use a different port for each server?

推荐答案

我知道这很晚,但是由于线程仍在Internet上,并且由于这是一个常见问题,在网络上很少有权威性的答案,因此值得即使是在这个较晚的日期,也为那些偶然发现它的人提供了更加完整和简洁的解释.

I know this is late, but since the thread is still on the internet, and because it's a common question with very few authoritative answers on the web, it deserves a more complete and concise explanation for those who may stumble into it, even at this late date.

  1. 您打开浏览器打开一个网站,例如google.com.在指定网站的过程中,您的计算机将任意选择一个端口号用作其源端口".该数字将高于49152(这是动态,专用或临时端口"的开头),但低于65535(这是可用的最高端口号).所选的端口号与该浏览器的实例"关联.

  1. You open a browser to pull up a web site, let's say google.com. In the process of specifying the web site your computer will arbitrarily pick a port number to use as its "source port." The number will be above 49152 which is the beginning of the "Dynamic, Private, or ephemeral ports" but below 65535 which is the highest port number available. The chosen port number is associated with that browser "instance."

很有趣,您可以在浏览器上打开一个新标签,然后在网址行上键入"google.com".您的目标是要运行两个google.com实例,因为您正在寻找不同的东西.然后,您的计算机选择该会话的第二个端口号,该端口号不同于它在第一个会话中使用的源"端口.实际上,您可以执行很多次,并且每个会话将具有与每个实例相关联的唯一源"端口.

Just for fun, you open a new tab on the browser and also type "google.com" on the url line. Your goal is to have two instances of google.com running because you're looking for different things. Your computer then chooses a second port number for that session, different from the "source" port it used for the first session. You could, in fact, do this many, many times, and each session will have a unique "source" port associated with each instance.

您的数据包发送到google.com,每个实例中的目标端口号将是端口80.Web服务器在端口80上监听"传入的连接请求.对于您打开的与google.com的每次会话,从计算机的角度来看,目标端口始终是端口80,但是对于您的一个或多个浏览器上的google.com连接的每个实例,源端口都是唯一的唯一地标识一个浏览器实例上的一个特定标签.这就是它们在您的计算机上的区别方式.

Your packet goes out to google.com, and the destination port number in every instance will be port 80. Web servers "listen" on port 80 for incoming connection requests. For each session with google.com you've opened, the destination port, from the perspective of your computer, will always be port 80, but for each instance of connection to google.com on your browser or browsers, the source port will uniquely identify one specific tab on one browser instance, uniquely. This is how they are differentiated on your computer.

google.com收到您的第一个请求.它的答复将端口80指定为其源端口.这就是有趣的地方.收到您的每个google.com查询都将被视为套接字",它是您的IP地址以及与与google.com联系的进程相关联的特定端口号的组合.因此,举例来说,假设您的IP地址为165.40.30.12,并且您的计算机用作与google.com进行四个通信实例的源端口的源端口号(假设四个都打开google.com的选项卡)分别是61235、62421、58392和53925.这四个套接字"将分别为165.40.30.12:61235、165.40.30.12:62421、165.40.30.12:58392和165.40.30.12:53925. "IP地址:源端口号"的每个组合都是唯一的,并且google.com将每个实例视为唯一. google.com响应并与套接字"(即IP地址和端口的组合)通信.

google.com gets your first request. Its reply will specify port 80 as its source port. Here's where it gets interesting. Each query google.com received from you will be treated as a "socket," which is a combination of your IP address, and the specific port number associated with the process that contacted google.com. So, for instance we'll say your IP address is 165.40.30.12, and the source port number your computer used as its source port for four instances of communications with google.com (let's say four different tabs all opening google.com) were 61235, 62421, 58392, and 53925. These four "sockets" would then be 165.40.30.12:61235, 165.40.30.12:62421, 165.40.30.12:58392, and 165.40.30.12:53925. Each combination of "IP address:source port number" is unique, and google.com will treat each instance as unique. google.com responds to, and communicates with the "socket," that is, the combination of IP address and port.

您的计算机从google.com获得响应,并根据其接收的目的地套接字"进行分类,并通过端口号进行解析,然后将google.com的响应分配给相应的浏览器窗口或标签(视情况而定).从头到尾都没问题,因为您每次都会在正确的窗口中看到正确的响应.

Your computer gets its responses from google.com, and sorts them out by what it receives as its destination "socket" which it parses out by port numbers, assigning the response from google.com to the appropriate browser window or tab, as appropriate. No problem from your end of things as you see the correct response in the correct window every time.

但是,"您在想,如果其他人不小心使用了相同的端口号(例如61235),就像我使用的端口号一样,这不会使google.com感到困惑吗? "完全没有,因为google.com正在跟踪套接字",这些套接字是IP地址和端口号的组合.当然,我们衷心希望,其他人正在使用与您使用的IP地址不同的IP地址,例如152.126.11.27,并且IP地址和端口号的组合是唯一的-152.126.11.27:61235-区别在于即使端口号相同,它们的IP地址也不同.

"But," you're thinking, "what if someone else accidentally uses the same port number - let's say 61235 - as I've used for my source number? Doesn't this confuse google.com?" Not at all, because google.com is tracking "sockets" which are a combination of IP address and port number. Someone else, naturally, and we sincerely hope, is using a different IP address from the one you're using, let's say 152.126.11.27, and the combination of IP address and port number are unique - 152.126.11.27:61235 - differentiated by their different IP addresses, even though the port numbers are the same.

google.com是否从1000个用户那里获取1000条查询都没有关系,这些查询都使用端口80作为目标端口号(该端口google.com正在监听传入的通信),因为这1000个用户中的每个用户都将具有唯一的IP地址. google.com通过客户的唯一性来跟踪其客户-他们始终必须是唯一的,不是吗? -套接字号,包括其IP地址和源"端口号.即使那1000个客户端中的每个客户端设法以某种方式使用相同的源"端口号(不太可能达到最大值),它们仍将具有不同的IP地址,从而使它们的源套接字"在所有其他客户端中都是唯一的.

It doesn't matter if google.com gets 1000 queries from 1000 users, all using port 80 as their destination port number (the port google.com is listening to for incoming communications) because each of those 1000 users will all have unique IP addresses. google.com tracks its clients by their unique - and they always have to be unique, don't they? - socket numbers consisting of their IP address and "source" port number. Even if every one of those 1000 clients somehow managed to use the same "source" port number (unlikely to the max), they would still have differing IP addresses, making their source "socket" unique among all of the others.

当您看到以这种方式解释时,这一切都非常简单.很明显,Web服务器可以始终在一个端口(80)上侦听,并且仍然可以与各种客户端区分开.它不只是查看查询中收到的IP地址-您认为它们乍一看应该都是唯一的,直到您意识到可以在该Web服务器上打开多个网页为止-而是在源端口数字,这些数字的组合使每个查询都是唯一的.我希望这很清楚.当您考虑它时,它是一个优雅的系统,但是一旦您理解它,它便很简单.

This is all rather simple when you see it explained this way. It makes it clear that a web server can always listen on one port (80) and still differentiate from the various clients. It doesn't simply look at the IP address it received in the query - you'd think they should all be unique at first blush until you realize you could have more than one web page opened on that web server - but at the source port number, which in combination, make every query unique. I hope this is pretty clear. It's an elegant system when you think about it, yet simple once you understand it.

这篇关于端口号在TCP中如何真正起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆