本文翻译自:How do multiple clients connect simultaneously to one port, say 80, on a server? [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
I understand the basics of how ports work. 我了解端口工作原理。 However, what I don't get is how multiple clients can simultaneously connect to say port 80. I know each client has a unique (for their machine) port. 但是,我没有得到的是多个客户端如何同时连接到端口80。我知道每个客户端都有一个唯一的(对于他们的计算机而言)端口。 Does the server reply back from an available port to the client, and simply state the reply came from 80? 服务器是否从可用端口回复给客户端,并仅声明回复来自80? How does this work? 这是如何运作的?
#1楼
参考:https://stackoom.com/question/DyBt/多个客户端如何同时连接到服务器上的一个端口-例如-重复
#2楼
Important: 重要:
I'm sorry to say that the response from "Borealid" is imprecise and somewhat incorrect - firstly there is no relation to statefulness or statelessness to answer this question, and most importantly the definition of the tuple for a socket is incorrect. 很遗憾地说,“ Borealid”的响应不精确且有些不正确-首先,与有状态或无状态无关,无法回答此问题,最重要的是,套接字的元组的定义不正确。
First remember below two rules: 首先记住以下两个规则:
Primary key of a socket: A socket is identified by
{SRC-IP, SRC-PORT, DEST-IP, DEST-PORT, PROTOCOL}
not by{SRC-IP, SRC-PORT, DEST-IP, DEST-PORT}
- Protocol is an important part of a socket's definition. 套接字的主键:套接字由{SRC-IP, SRC-PORT, DEST-IP, DEST-PORT, PROTOCOL}
标识,而不由{SRC-IP, SRC-PORT, DEST-IP, DEST-PORT}
-协议是套接字定义的重要组成部分。OS Process & Socket mapping: A process can be associated with (can open/can listen to) multiple sockets which might be obvious to many readers. OS进程和套接字映射:一个进程可以与多个套接字关联(可以打开/可以监听),这对于许多读者而言可能是显而易见的。
Example 1: Two clients connecting to same server port means: socket1 {SRC-A, 100, DEST-X,80, TCP}
and socket2{SRC-B, 100, DEST-X,80, TCP}
. 示例1:连接到同一服务器端口的两个客户端表示: socket1 {SRC-A, 100, DEST-X,80, TCP}
和socket2{SRC-B, 100, DEST-X,80, TCP}
。 This means host A connects to server X's port 80 and another host B also connects to same server X to the same port 80. Now, how the server handles these two sockets depends on if the server is single threaded or multiple threaded (I'll explain this later). 这意味着主机A连接到服务器X的端口80,另一个主机B也连接到同一服务器X的同一端口80。现在,服务器如何处理这两个套接字取决于服务器是单线程还是多线程(我将稍后再解释)。 What is important is that one server can listen to multiple sockets simultaneously. 重要的是一台服务器可以同时侦听多个套接字。
To answer the original question of the post: 要回答帖子的原始问题:
Irrespective of stateful or stateless protocols, two clients can connect to same server port because for each client we can assign a different socket (as client IP will definitely differ). 无论有状态协议还是无状态协议,两个客户端都可以连接到同一服务器端口,因为我们可以为每个客户端分配不同的套接字(因为客户端IP肯定会有所不同)。 Same client can also have two sockets connecting to same server port -