异步套接字服务器如何工作?
我应该声明,我并没有询问具体的实现细节(还),而只是询问正在发生的事情的一般概述。我了解套接字背后的基本概念,并且需要澄清整个过程。我目前的理解(可能非常错误)是这样的:
套接字不断监听想要连接的客户端(在它自己的线程中)。当连接发生时,会引发一个事件,该事件会生成另一个线程来执行连接过程。在连接过程中,客户端被分配有自己的套接字,用于与服务器进行通信。然后,服务器等待来自客户端的数据,当数据到达时,会引发一个事件,该事件会生成一个线程,将数据从流读取到缓冲区中。
我的问题是:
我的理解程度如何?
每个客户端套接字是否都需要它自己的线程来侦听数据?
数据如何路由到正确的客户端套接字?这是 TCP/UDP/内核内部处理的事情吗?
在这个线程环境中,通常共享什么类型的数据,争论的焦点是什么?
任何澄清和补充解释将不胜感激。
编辑:
关于通常共享哪些数据和争论点的问题,我意识到这更多的是一个实现细节,而不是关于接受连接和发送/接收数据的一般过程的问题。我研究了几个实现(SuperSocket 和 Kayak),并注意到会话缓存和可重用缓冲池等方面的一些同步。请随意忽略这个问题。我非常感谢您的所有反馈。
I should state that I'm not asking about specific implementation details (yet), but just a general overview of what's going on. I understand the basic concept behind a socket, and need clarification on the process as a whole. My (probably very wrong) understanding is currently this:
A socket is constantly listening for clients that want to connect (in its own thread). When a connection occurs, an event is raised that spawns another thread to perform the connection process. During the connection process the client is assigned it's own socket in which to communicate with the server. The server then waits for data from the client and when data arrives an event is raised which spawns a thread to read the data from a stream into a buffer.
My questions are:
How off is my understanding?
Does each client socket require it's own thread to listen for data on?
How is data routed to the correct client socket? Is this something taken care of by the guts of TCP/UDP/kernel?
In this threaded environment, what kind of data is typically being shared, and what are the points of contention?
Any clarifications and additional explanation would be greatly appreciated.
EDIT:
Regarding the question about what data is typically shared and points of contention, I realize this is more of an implementation detail than it is a question regarding general process of accepting connections and sending/receiving data. I had looked at a couple implementations (SuperSocket and Kayak) and noticed some synchronization for things like session cache and reusable buffer pools. Feel free to ignore this question. I've appreciated all your feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个连接一个线程是糟糕的设计(不可扩展,过于复杂),但不幸的是太常见了。
套接字服务器的工作方式大致如下:
这发生在一个线程中,您可以在单个线程中轻松处理数千个已连接的套接字,并且没有什么正当理由通过引入线程使事情变得更加复杂。
IP 堆栈负责处理哪些数据包按哪个顺序发送到哪个“套接字”的所有详细信息。从应用程序的角度来看,套接字代表可靠的有序字节流(TCP)或不可靠的无序数据包序列(UDP)
编辑:响应更新的问题。
我不知道您提到的任何一个库,但根据您提到的概念:
One thread per connection is bad design (not scalable, overly complex) but unfortunately way too common.
A socket server works more or less like this:
This happens in one thread, you can easily handle thousands of connected sockets in a single thread, and there's few valid reasons for making this more complex by introducing threads.
The IP stack takes care of all the details of which packets go to what "socket" in which order. Seen from the applications point of view, a socket represents a reliable ordered byte stream (TCP) or an unreliable unordered sequence of packets(UDP)
EDIT: In response to updated question.
I don't know either of the libraries you mention, but on the concepts you mention:
相当远。
不。
TCP/IP 是多层协议。它没有“内核”。它由多个部分组成,每个部分都具有与其他部分相连的单独的 API。
IP 地址就地处理。
端口号在另一个地方处理。
IP 地址与 MAC 地址相匹配来识别特定主机。端口号将 TCP(或 UDP)套接字与特定的应用程序软件联系起来。
什么线程环境?
数据共享?什么?
争论?物理信道是第一大争论点。 (例如,以太网依赖于冲突检测。)在那之后,计算机系统的每个部分都是多个应用程序共享的稀缺资源,并且是一个争论点。
Pretty far.
No.
TCP/IP is a number of layers of protocol. There's no "kernel" to it. It's pieces, each with a separate API to the other pieces.
The IP Address is handled in on place.
The port # is handled in another place.
The IP addresses are matched up with MAC addresses to identify a particular host. The port # is what ties a TCP (or UDP) socket to a particular piece of application software.
What threaded environment?
Data sharing? What?
Contention? The physical channel is the number one point of contention. (Ethernet, for example depends on collision-detection.) After that, well, every part of the computer system is a scarce resource shared by multiple applications and is a point of contention.