服务器是否为每个客户端进程打开了套接字? [英] Has server opened socket for each client process?

查看:60
本文介绍了服务器是否为每个客户端进程打开了套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对套接字感到困惑.据我所知,socket 是 ip 地址和端口号的组合.它只是允许写入或读取流的编程抽象(在 TCP 的情况下).现在我不确定的是,服务器在为客户端提供服务时是否有一个或多个套接字?让我们说 http 在端口 80.

I am confused regarding the sockets. As far as I know socket is combination of ip address and port number. Its just programming abstraction to allow to write to, or read from a stream (in case of TCP). Now what I am not absolutely sure of, is whether the server has ONE or MORE sockets when serving the client? Lets say http at port 80.

来自不同客户端的所有数据是否都发送到一个套接字(服务器:80)并且某些 UBER 服务器进程根据传入地址区分它们,还是更多的套接字基于 TCP 层创建的客户端地址和端口号的组合?.有人可以用分步算法(同时为多个客户端提供服务)彻底描述这一点,而不仅仅是服务器将套接字绑定到端口,服务器侦听套接字,服务器提供数据.

Does all data from various clients got sent to one socket (server:80) and some UBER server process distinguishes them based on incoming address or are more sockets based on combination of client address and port number created by TCP layer?. Can someone describe this thoroughly with step-by-step algorithm (for multiple clients served at the same time) and not just with Server binds socket to port, Server listens to socket, Server serves data.

推荐答案

您将 TCP 连接与套接字混淆了.套接字不是网络级概念.Is 是一个操作系统概念.TCP 连接作为(源IP、源端口、目标IP、目标端口)的唯一组合存在于网络上.套接字是对开放端口开放连接的句柄(此语句略有简化).当我开始时,我也认为这很令人困惑并且是一个操作系统设计错误(但它就是这样,我们被它困住了).设计错误是每个不同套接字允许的操作非常不同.这些用例应该是两个独立的概念,具有不同的名称和不同的 API.

You're confusing TCP connections with sockets. A socket is not a network-level concept. Is is an OS concept. A TCP connection exists on the network as a unique combination of (source-ip, source-port, dest-ip, dest-port). A socket is a handle to an open port or an open connection (this statement is slightly simplified). When I got started I also thought this was confusing and an OS design error (but it is what it is and we are stuck with it). The design error is that the allowed operation for each of the different sockets are very much different. These use cases should have been two independent concepts with different names and different APIs.

如您所见,套接字和连接之间没有 1:1 的关系.

As you can see there is no 1:1 relationship between sockets and connections.

有人可以用分步算法彻底描述这一点

Can someone describe this thoroughly with step-by-step algorithm

服务器打开一个套接字让操作系统知道它想要监听或连接.然后,每个接受的连接都会产生一个新的、独立的套接字.不过,每个新连接都在相同的服务器 IP 和服务器端口上.只是 client-ip 和/或 client-port 不同.服务器在每个连接的套接字上读写.开放端口套接字仅用于接受新连接.

A server opens a socket to let the OS know that it wants to listen or connect. Then, every accepted connection will result in a new, independent socket. Every new connection is on the same server-ip and server-port though. Just the client-ip and/or client-port are different. The server reads and writes on the per-connection sockets. The open-port socket is just for accepting new connections.

服务器概念上是这样的:

The server conceptually goes like this:

var openPortSocket = Open(80); //HTTP port
while(true) {
 var connectionSocket = openPortSocket.Accept();
 ServeConnectionOnNewThread(connectionSocket);
}

这是一个逻辑模型.实际的 API 调用是不同的.例如,大多数服务器使用异步 IO.不过,这与您的问题无关.

This is a logical model. The actual API calls are different. Most servers use async IO for example. That is not relevant to your question though.

客户端必须为每个连接使用不同的客户端端口.这正是您的浏览器所做的.

Clients must use a different client port for each connection. This is exactly what your browser does.

这篇关于服务器是否为每个客户端进程打开了套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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