大网数据广告活动
服务器运维问答

how does an OS route messages sent to the same port to different sockets?

· 52CE 我爱测

This is something I've never quite understood: A service offered by a server can be uniquely identified by its IP address and port, for example 1.2.3.4:22, but multiple clients might connect to the same port at the same time. Whe…

问题原文

原帖链接 · Stephen · CC BY-SA 4.0

This is something I've never quite understood: A service offered by a server can be uniquely identified by its IP address and port, for example 1.2.3.4:22, but multiple clients might connect to the same port at the same time. When the different clients send new data to the server, how does the server distinguish between them so that it can route the data differently?

For example, when a server allows SSH connections, it will open port 22 by default. Multiple SSH clients can connect so that multiple people can be using SSH at the same time, and they will all be using port 22. I believe that on the server, the different sessions are distinguished by having different sockets, one for each session, though I could be wrong about that.

But if that is the case, when a particular user enters a new command during their SSH session, how does the server know which socket to route that command to? The user's computer doesn't have access to the socket descriptor, they only have the IP address and port, so I don't see how they could send enough information to the server to allow it to distinguish between their session and another session. Does the server distinguish based on the IP address and port of the client, or does it do something else?

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

Each end of the connection has its own IP address and port. The "client" (it's not a client as TCP/IP is peer to peer; it's the initiator) has the source IP and source port on its own system, to which packets are addressed. The connection is defined by all four of the source IP, source port, destination IP and destination port, which makes it trivial to disambiguate connections.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-03-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。