socket服务器中每个客户端连接的新线程?

时间:2023-02-10 18:55:38

I am trying to optimize multiple connections per time to a TCP socket server.

我试图每次优化多个连接到TCP套接字服务器。

Is it considered good practice, or even rational to initiate a new thread in the listening server every time I receive a connection request?

每次收到连接请求时,在监听服务器中启动新线程是否被认为是良好的做法,甚至是合理的?

At what time should I begin to worry about a server based on this infrastructure? What is the maximum no of background threads I can work, until it doesn't make any sense anymore?

我应该在什么时候开始担心基于此基础架构的服务器?什么是我可以工作的最大背景线程数,直到它没有任何意义?

Platform is C#, framework is Mono, target OS is CentOS, RAM is 2.4G, server is on the clouds, and I'm expecting about 200 connection requests per second.

平台是C#,框架是Mono,目标操作系统是CentOS,RAM是2.4G,服务器在云端,我预计每秒大约有200个连接请求。

1 个解决方案

#1


8  

No, you shouldn't have one thread per connection. Instead, you should be using the asynchronous methods (BeginAccept/EndAccept, BeginSend/EndSend, etc). These will make much more efficient use of system resources.

不,你不应该每个连接有一个线程。相反,您应该使用异步方法(BeginAccept / EndAccept,BeginSend / EndSend等)。这些将更有效地利用系统资源。

In particular, every thread you create adds overhead in terms of context switches, stack space, cache misses and so on. Linux is better at managing this stuff than Windows, for example, but that shouldn't be an excuse to give you free reign to create as many threads as you like ;)

特别是,您创建的每个线程都会在上下文切换,堆栈空间,缓存未命中等方面增加开销。例如,Linux在管理这些东西方面比Windows更好,但这不应成为让你*创建任意线程的借口;)

#1


8  

No, you shouldn't have one thread per connection. Instead, you should be using the asynchronous methods (BeginAccept/EndAccept, BeginSend/EndSend, etc). These will make much more efficient use of system resources.

不,你不应该每个连接有一个线程。相反,您应该使用异步方法(BeginAccept / EndAccept,BeginSend / EndSend等)。这些将更有效地利用系统资源。

In particular, every thread you create adds overhead in terms of context switches, stack space, cache misses and so on. Linux is better at managing this stuff than Windows, for example, but that shouldn't be an excuse to give you free reign to create as many threads as you like ;)

特别是,您创建的每个线程都会在上下文切换,堆栈空间,缓存未命中等方面增加开销。例如,Linux在管理这些东西方面比Windows更好,但这不应成为让你*创建任意线程的借口;)