简体   繁体   English

使用c#一次接受多个tcp连接

[英]Accepting many tcp connections at one time using c#

I want to accept about 5000 tcp client that are trying to connect exactly at one time. 我想接受大约5000个tcp客户端,它们正在尝试一次完全连接。
when i test the program many of client can connect succusfully but many of them cant by giving "No connection could be made because the target machine actively refused it" error. 当我测试程序时,许多客户端可以真正地连接但很多人不能通过给出“因为目标机器主动拒绝它而无法建立连接”错误。
i increased backlog parameter of listen method of my socket but it didn't help the code i used is the example of msdn with this link. 我增加了我的socket的listen方法的backlog参数,但它没有帮助我使用的代码是带有链接的msdn的示例。 can anybody help me? 有谁能够帮助我?

It is ok for underlying stack to refuse connections while busy accepting other connections(nothing is really parallel inside). 底层堆栈可以在忙于接受其他连接时拒绝连接(内部没有任何内容真正并行)。 If you really need to connect that many clients at a time, you can change client logic a bit: reconnect on failing (like proposed in comments). 如果你真的需要一次连接那么多客户端,你可以稍微改变一下客户端逻辑:重新连接失败(如评论中提出的那样)。 Or you can start multiple listeners on different threads on different ports and choose which port to connect by fair dice on clientside. 或者,您可以在不同端口上的不同线程上启动多个侦听器,并在客户端选择通过公平骰子连接的端口。

You may want to look into using the SocketAsyncEventArgs object. 您可能希望研究使用SocketAsyncEventArgs对象。 Then you can have accept sockets to handle the initial connections and once the connection is established the accept socket will hand it off to a worker SocketAsyncEventArgs object. 然后你可以接受套接字来处理初始连接,一旦建立了连接,接受套接字就会把它交给一个worker SocketAsyncEventArgs对象。

Check out this project to get started with it. 查看此项目以开始使用它。 http://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod http://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod

What I have found is that using this technique works really well, but you will run up against the limitations of the OS and hardware. 我发现使用这种技术非常有效,但是你会遇到操作系统和硬件的限制。 I tested my tcp server (running on windows server 2008 R2), which uses SocketAsyncEventArgs object, with a few thousand connections and it worked successfully without the clients getting rejected (Had to increase the backlog for this). 我测试了我的tcp服务器(在Windows服务器2008 R2上运行),它使用SocketAsyncEventArgs对象,有几千个连接,它成功运行,没有客户端被拒绝(不得不为此增加积压)。 The problem was that the time between the client and server establishing the connection, and the client getting a response, grew as the number of simultaneous connection requests grew. 问题在于,建立连接的客户端和服务器之间的时间以及客户端获得响应的时间随着同时连接请求数量的增加而增长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM