处理数千个TCP套接字连接 [英] Handling thousands of TCP socket connections

查看:50
本文介绍了处理数千个TCP套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试评估最有效的方法来处理数千个

同时TCP连接每个连接到服务器的时间为

小时并传递少量数据通常每分钟一次。收到的数据

记录在SQL Server数据库中。


我发现读取每个套接字的唯一方法需要一个线程来支持$ /
阻止等待数据的每个连接。这看起来非常低效,因为它会产生数千个线程,每个线程都会有一个网络流和读取器对象做得很少。这是阻止

方法从TCP套接字读取数据的最佳/唯一方法吗?


我正在考虑实现我循环的开放套接字队列

通过检查每个是否有任何数据准备好处理,从线程池类产生一个线程

来处理数据的读取和记录

将套接字返回队列。是否有可能确定套接字是否已准备好读取数据以及要读取多少字节以防止它被阻止?


我可以通过在每个插座上保留时间

邮票来进行上述效率低下的变换,并使用它来等待''应该'

数据可用,然后产生一个线程来读取它,但数据并不总是每分钟传递一次,因此会导致一定程度的线程被阻止

a持续时间。 />

有没有人对最好的方法有什么建议?


谢谢


Phil ...

解决方案

查看静态方法Socket.Select()(假设您使用的是.NET

框架)。 br />

Mike

" Phil Jenson" < pH值** @ jenson.co.uk.nospam>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...

我是尝试评估处理数千个同时TCP连接的最有效方法,每个TCP连接都与服务器连接数小时,并且通常每分钟传递一次少量数据。收到的数据记录在SQL Server数据库中。

我发现读取每个套接字的唯一方法是每个连接都需要一个线程来阻止等待数据。这看起来效率很低,因为它会产生数千个线程,每个线程都会有一个网络流和读取器对象做得很少。这个阻塞
方法是从TCP套接字读取数据的最佳/唯一方法吗?

我正在考虑实现一个开放套接字队列,我循环检查是否每个有任何数据准备好处理,从线程池类产生一个
线程来处理数据的读取和记录,然后将套接字返回到队列。是否有可能确定插座是否准备好读取数据以及要读取多少字节以防止它被阻塞?

我可能会做一个低效的变换以上内容通过对每个套接字保留一个时间戳并使用它来等待应该数据可用然后产生一个线程来读取它但数据不是
总是通过每分钟所以会导致一定程度的线程被阻止
持续一段时间。

有没有人建议最好的方法?

谢谢

菲尔......



谢谢你们的建议,两者似乎都提供了解决方案我

之后。


我特别感兴趣的IO完成端口线程。

标准socket.BeginReceive是否使用IO完成端口线程?我可以简单地为每个我正在处理的套接字调用此方法吗

将使用标准线程池中的线程调用回调方法

只有当数据可用或套接字关闭时才会这样做?


Phil ...

" Stelrad Doulton" < ___ @ ____ COM>。在留言中写道

新闻:4P ************* @ newsfe1-gui.ntli.net ...

除了寻找在Socket.Select上,再看一下
ThreadPool。你会发现有两种类型的线程可供服务;工作线程和IO完成端口线程。我最近完成了一个每个客户端的线程服务器应用程序,它使用长时间运行的async-read
逐个套接字来实现你所说的。我测试过的单CPU机器将在此基础上处理1000个并发的连接。

Mike Jansen < MJ ********** @ mail.com>在消息中写道
新闻:uB ************** @ TK2MSFTNGP15.phx.gbl ...

查看静态方法Socket.Select( )(假设您使用的是.NET
框架)。

Mike

" Phil Jenson" < pH值** @ jenson.co.uk.nospam>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...

我试着评估一下处理数千个同时TCP连接的最有效方法是连接每个服务器连接到服务器
小时,并且通常每分钟传递一次少量数据。收到的数据记录在SQL Server数据库中。

我发现读取每个套接字的唯一方法需要一个线程
每个连接阻止等待对于数据。这看起来效率很低,因为它会产生数千个线程,每个线程都会使网络流和读取器对象做得很少。这个
阻止
方法是从TCP套接字读取数据的最佳/唯一方法吗?

我正在考虑实现一个我循环的开放套接字队列
通过检查每个是否有任何数据准备好处理,从线程池类产生


线程

来处理数据的读取和记录,然后返回套接字到队列。是否有可能确定
插座是否准备好读取数据以及要读取多少字节以防止它被阻挡?

我可以做通过在每个套接字上保留一个时间戳并使用它来等待应该数据可用,然后产生一个线程来读取它,这是上述的低效变化数据不是


总是

每分钟传递一次因此会导致一定程度的线程被阻止


持续时间。

有人对最好的方法有什么建议吗?

谢谢

菲尔...




我做了更多的研究,我相信标准的socket.BeginReceive

确实使用了完成端口,但我也看到了

IO完成端口线程数量的参考nd 1000.这是对的吗?这将

防止超过1000个插座扩展吗?


虽然这不是一个中级问题我们很可能会进入10-100
$ b未来$ b thoushand连接。在一个cerain点我们将调查

负载平衡服务器/路由器以进一步扩展这一点但是1000个连接

将在这个生命周期的早期证明是一个主要限制项目。


谢谢


Phil ...


" Phil Jenson" < pH值** @ jenson.co.uk.nospam>在消息中写道

新闻:ua ************** @ TK2MSFTNGP14.phx.gbl ...

谢谢你们两位建议,两者似乎都提供了我之后的解决方案。

我特别感兴趣的是IO Completion端口线程。
标准socket.BeginReceive是否使用IO完成端口线程?我可以简单地为我正在处理的每个套接字调用此方法吗?它将使用标准
线程池中的线程调用回调方法,它只会在数据时执行此操作可以使用或插座关闭吗?

菲尔...

" Stelrad Doulton" < ___ @ ____ COM>。在消息中写道
新闻:4P ************* @ newsfe1-gui.ntli.net ...

除了查看Socket.Select ,再看一下
ThreadPool。你会发现有两种类型的线程可供服务;工作线程和IO完成端口线程。我最近完成了一个每个客户端的线程服务器应用程序,它使用长时间运行的
async-read逐个插槽来实现你所说的内容。我测试的单CPU机器将在此基础上处理1000个并发连接。

" Mike Jansen" < MJ ********** @ mail.com>在消息中写道
新闻:uB ************** @ TK2MSFTNGP15.phx.gbl ...

查看静态方法Socket.Select( )(假设您使用的是.NET
框架)。

Mike

" Phil Jenson" < pH值** @ jenson.co.uk.nospam>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...
我正在尝试评估最有效的方法处理成千上万的同时TCP连接,每个TCP连接到服务器

小时,并通常每分钟传递一次少量数据。收到的数据记录在SQL Server数据库中。

我发现读取每个套接字的唯一方法需要一个线程
每个连接阻止等待对于数据。这看起来效率很低,因为它会产生数千个线程,每个线程都会使网络流和读取器对象做得很少。这个
阻止
方法是从TCP套接字读取数据的最佳/唯一方法吗?

我正在考虑实现一个我循环的开放套接字队列
通过检查每个是否有任何数据准备好处理,从线程池类产生
线程来处理数据的读取和记录

将套接字返回队列。是否有可能确定
插座是否准备好读取数据以及要读取多少字节以防止它被阻挡?

我可以做通过在每个插槽上保留一个时间戳并使用它来等待应该数据可用,然后产生一个线程来实现上述的低效变化阅读它,但数据并不总是每分钟传递一次,因此会导致一定程度的线程被封锁
一段时间。

有没有人对最佳方法有什么建议?

非常感谢...




I am try to evaluate the most efficient method of handling thousands of
simultaneous TCP connects each of which remain connected to the server for
hours and pass a small amount of data usually once a minute. The data
received is logged in a SQL Server database.

The only method I have found of reading each socket requires a thread for
each connection which blocks waiting for data. This appears to be very
inefficient as it will result in thousands of threads, each of which will
have a networkstream and reader object doing very little. Is this blocking
method the best/only method of reading data from a TCP socket?

I was thinking of implementing a queue of open sockets which I cycled
through checking if each had any data ready to handle, spawning off a thread
from a threadpool class to handle reading and logging of the data before
returning the socket to the queue. Is it possible to identify if a socket
has data ready to be read and how many bytes to read to prevent it from
blocking?

I can possibly do an inefficient variation of the above by keeping a time
stamp against each socket and using this to wait until there ''should'' be
data available and then spawning a thread to read it but data is not always
passed each minute so would result in a degree of threads being blocked for
a duration.

Does anybody have a suggestion as to the best method?

Thanks

Phil...

解决方案

Check out the static method Socket.Select() (assuming you are using .NET
framework).

Mike
"Phil Jenson" <ph**@jenson.co.uk.nospam> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

I am try to evaluate the most efficient method of handling thousands of
simultaneous TCP connects each of which remain connected to the server for
hours and pass a small amount of data usually once a minute. The data
received is logged in a SQL Server database.

The only method I have found of reading each socket requires a thread for
each connection which blocks waiting for data. This appears to be very
inefficient as it will result in thousands of threads, each of which will
have a networkstream and reader object doing very little. Is this blocking
method the best/only method of reading data from a TCP socket?

I was thinking of implementing a queue of open sockets which I cycled
through checking if each had any data ready to handle, spawning off a thread from a threadpool class to handle reading and logging of the data before
returning the socket to the queue. Is it possible to identify if a socket
has data ready to be read and how many bytes to read to prevent it from
blocking?

I can possibly do an inefficient variation of the above by keeping a time
stamp against each socket and using this to wait until there ''should'' be
data available and then spawning a thread to read it but data is not always passed each minute so would result in a degree of threads being blocked for a duration.

Does anybody have a suggestion as to the best method?

Thanks

Phil...



Thank you both for your suggestions, both seem to offer the solution I am
after.

I am particaulr interested in the IO Completion port threads. Does the
standard socket.BeginReceive use an IO Completion Port Thread? Can I simply
call this method for each and every socket I am handling on the basis it
will call the callback method using the threads in the standard threadpool
and it will only do so when data is avaiable or the socket closed?

Phil...
"Stelrad Doulton" <___@____.com> wrote in message
news:4P*************@newsfe1-gui.ntli.net...

In addition to looking at Socket.Select, have another look at the
ThreadPool. You will find that there are 2 types of thread that it will
service; worker threads and IO completion port threads. I have recently
finished a thread-per-client server app that uses long running async-reads
on a socket by socket basis to achieve what you are talking about. The
single CPU machine I have tested on will handle 1000 concurrent
connections on that basis.
"Mike Jansen" <mj**********@mail.com> wrote in message
news:uB**************@TK2MSFTNGP15.phx.gbl...

Check out the static method Socket.Select() (assuming you are using .NET
framework).

Mike
"Phil Jenson" <ph**@jenson.co.uk.nospam> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

I am try to evaluate the most efficient method of handling thousands of
simultaneous TCP connects each of which remain connected to the server
for
hours and pass a small amount of data usually once a minute. The data
received is logged in a SQL Server database.

The only method I have found of reading each socket requires a thread
for
each connection which blocks waiting for data. This appears to be very
inefficient as it will result in thousands of threads, each of which
will
have a networkstream and reader object doing very little. Is this
blocking
method the best/only method of reading data from a TCP socket?

I was thinking of implementing a queue of open sockets which I cycled
through checking if each had any data ready to handle, spawning off a


thread

from a threadpool class to handle reading and logging of the data before
returning the socket to the queue. Is it possible to identify if a
socket
has data ready to be read and how many bytes to read to prevent it from
blocking?

I can possibly do an inefficient variation of the above by keeping a
time
stamp against each socket and using this to wait until there ''should'' be
data available and then spawning a thread to read it but data is not


always

passed each minute so would result in a degree of threads being blocked


for

a duration.

Does anybody have a suggestion as to the best method?

Thanks

Phil...





I have done some more resarch and I beleive the standard socket.BeginReceive
does use Completion Ports, but I have also seen reference to the number of
IO Completion Port threads being around 1000. Is this correct? And will this
prevent scaling above 1000 sockets?

Whilst this wont be an imediate problem we could well get into 10-100
thoushand connections in the future. At a cerain point we will be look into
load balancing servers/routers to scale this further but 1000 connections
will prove a major limit very early on in the life of this project.

Thanks

Phil...

"Phil Jenson" <ph**@jenson.co.uk.nospam> wrote in message
news:ua**************@TK2MSFTNGP14.phx.gbl...

Thank you both for your suggestions, both seem to offer the solution I am
after.

I am particaulr interested in the IO Completion port threads. Does the
standard socket.BeginReceive use an IO Completion Port Thread? Can I
simply call this method for each and every socket I am handling on the
basis it will call the callback method using the threads in the standard
threadpool and it will only do so when data is avaiable or the socket
closed?

Phil...
"Stelrad Doulton" <___@____.com> wrote in message
news:4P*************@newsfe1-gui.ntli.net...

In addition to looking at Socket.Select, have another look at the
ThreadPool. You will find that there are 2 types of thread that it will
service; worker threads and IO completion port threads. I have recently
finished a thread-per-client server app that uses long running
async-reads on a socket by socket basis to achieve what you are talking
about. The single CPU machine I have tested on will handle 1000
concurrent connections on that basis.
"Mike Jansen" <mj**********@mail.com> wrote in message
news:uB**************@TK2MSFTNGP15.phx.gbl...

Check out the static method Socket.Select() (assuming you are using .NET
framework).

Mike
"Phil Jenson" <ph**@jenson.co.uk.nospam> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I am try to evaluate the most efficient method of handling thousands of
simultaneous TCP connects each of which remain connected to the server
for
hours and pass a small amount of data usually once a minute. The data
received is logged in a SQL Server database.

The only method I have found of reading each socket requires a thread
for
each connection which blocks waiting for data. This appears to be very
inefficient as it will result in thousands of threads, each of which
will
have a networkstream and reader object doing very little. Is this
blocking
method the best/only method of reading data from a TCP socket?

I was thinking of implementing a queue of open sockets which I cycled
through checking if each had any data ready to handle, spawning off a
thread
from a threadpool class to handle reading and logging of the data
before
returning the socket to the queue. Is it possible to identify if a
socket
has data ready to be read and how many bytes to read to prevent it from
blocking?

I can possibly do an inefficient variation of the above by keeping a
time
stamp against each socket and using this to wait until there ''should''
be
data available and then spawning a thread to read it but data is not
always
passed each minute so would result in a degree of threads being blocked
for
a duration.

Does anybody have a suggestion as to the best method?

Thanks

Phil...





这篇关于处理数千个TCP套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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