异步套接字服务器如何工作? [英] How does an asynchronous socket server work?

查看:117
本文介绍了异步套接字服务器如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该指出,我还没有询问具体的实施细节(而只是询问发生了什么事).我了解套接字背后的基本概念,因此需要对整个过程进行说明.我(可能非常错误)的理解是这样的:

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.

我的问题是:

我的理解如何?

每个客户端套接字是否都需要它自己的线程来侦听数据?

Does each client socket require it's own thread to listen for data on?

如何将数据路由到正确的客户端套接字?这是TCP/UDP/内核的帮助吗?

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.

关于通常共享哪些数据和争用点的问题,我意识到这更多的是实现细节,而不是有关接受连接和发送/接收数据的一般过程的问题.我查看了几个实现(SuperSocket和Kayak),并注意到一些同步,例如会话缓存和可重用的缓冲池.随意忽略这个问题.感谢您的反馈.

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.

推荐答案

每个连接一个线程是不良设计(不可扩展,过于复杂),但不幸的是太普遍了.

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:

  • 已将侦听套接字设置为接受连接,并将其添加到套接字集中
  • 检查套接字集是否有事件
  • 如果侦听套接字具有挂起的连接,则通过接受连接来创建新的套接字,然后将其添加到套接字集中
  • 如果连接的套接字有事件,则将调用相关的IO功能
  • 再次检查套接字集是否有事件

这发生在一个线程中,您可以在一个线程中轻松处理成千上万个已连接的套接字,并且没有充分的理由通过引入线程来使其变得更加复杂.

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.

while running
    select on socketset
    for each socket with events
        if socket is listener
            accept new connected socket
            add new socket to socketset
        else if socket is connection
            if event is readable
                read data
                process data
            else if event is writable
                write queued data
            else if event is closed connection
                remove socket from socketset
            end
        end
    done
done

IP堆栈负责按顺序将哪些数据包发送到套接字"的所有详细信息.从应用程序的角度来看,套接字代表可靠的有序字节流(TCP)或不可靠的无序包序列(UDP)

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)

针对更新的问题.

我不知道您提到的任何一个库,但是关于您提到的概念:

I don't know either of the libraries you mention, but on the concepts you mention:

  • 会话高速缓存通常保持与客户端相关联的数据,并且可以将该数据重新用于多个连接.当您的应用程序逻辑需要状态信息时,这是有道理的,但它比实际的网络端还高.在上面的示例中,会话缓存将由过程数据"部分使用.
  • 缓冲池也是高流量服务器的轻松且通常有效的优化.该概念非常容易实现,您无需分配/释放用于存储读取/写入数据的空间,而是从池中获取预分配的缓冲区,然后使用它,然后将其返回到池中.这避免了(有时相对昂贵)后端分配/取消分配机制.这与网络没有直接关系,您也可以将缓冲池用于读取文件并对其进行处理的东西.

这篇关于异步套接字服务器如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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