使用 .NET 创建 100,000 个 tcp 连接

发布于 2024-07-20 15:22:11 字数 1029 浏览 2 评论 0原文

我正在用 C# 编写一个小型 Comet 服务器,为了测试它,我编写了一个小程序,该程序打开一堆连接,向每个连接写入一些文本,然后关闭每个连接:

int basePort = 30000;
IPAddress localAddress = new IPAddress( new byte[] { 127, 0, 0, 1 } );
List<Socket> sockets = new List<Socket>();

for( int i = 0; i < 20000; i++ ) {
    Socket s = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
    s.Bind( new IPEndPoint( localAddress, basePort + i ) );
    s.Connect( "localhost", 1999 );
    sockets.Add( s );
}

string message = "hello";
byte[] messageData = Encoding.ASCII.GetBytes( message );
foreach( Socket s in sockets ) {
    s.Send( messageData );
}

foreach( Socket s in sockets ) {
    s.Disconnect( false );
}

我正在使用 Windows XP时刻,它只分配 1025 到 5000 范围内的动态客户端端口,因此我添加了对从 30000 开始的端口的显式绑定。这使我的连接数从 4000 以下增加到略多于 16000 个,但现在我收到以下异常Socket.Connect:

“无法执行套接字上的操作,因为系统缺乏足够的缓冲区空间或队列已满 127.0.0.1:1999”

有什么想法吗? 更改发送和接收缓冲区大小似乎没有任何区别,而且似乎总是我的客户端应用程序崩溃,而不是我的服务器。 我意识到在达到 100,000 个连接之前我将耗尽客户端端口,但我仍然想更好地了解发生了什么。

I am writing a little Comet server in C#, and to test it I have written a little program that opens a bunch of connections, writes a little text to each of them, and then closes each of them:

int basePort = 30000;
IPAddress localAddress = new IPAddress( new byte[] { 127, 0, 0, 1 } );
List<Socket> sockets = new List<Socket>();

for( int i = 0; i < 20000; i++ ) {
    Socket s = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
    s.Bind( new IPEndPoint( localAddress, basePort + i ) );
    s.Connect( "localhost", 1999 );
    sockets.Add( s );
}

string message = "hello";
byte[] messageData = Encoding.ASCII.GetBytes( message );
foreach( Socket s in sockets ) {
    s.Send( messageData );
}

foreach( Socket s in sockets ) {
    s.Disconnect( false );
}

I am using Windows XP at the moment, which only assigns dynamic client ports from the 1025 to 5000 range, so I have added explicit binding to ports starting at 30000. This took me from under 4000 connections to a little more than 16000, but now I am getting the following exception on Socket.Connect:

"An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full 127.0.0.1:1999"

Any thoughts? Changing the send and receive buffer size doesn't seem to make any difference, and it always seems to be my client app that breaks, never my server. I realize I'm going to run out of client ports before I get to 100,000 connections, but I'd still like to understand what is going on a little better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻找一个思念的角度 2024-07-27 15:22:11

您可能用完了非分页内存。 每个机器和每个进程都有限制,这些限制基于已安装的 RAM、操作系统、/3GB 开关设置等的数量。32 位操作系统 sku 对非分页内存的限制比 64 位操作系统 sku 的限制低得多。

You might be running out of non-paged memory. There are per-machine and per-process limits which are based on the amount of installed RAM, OS, /3GB switch settings, etc. 32 bit OS skus' have a much lower limit on non-paged memory than 64 bit OS sku's.

北风几吹夏 2024-07-27 15:22:11

我认为 100.000 个连接不是一个可行的目标。

TCP/IP 端口是 16 位数字。 所以任何高于 65535 的值都是不行的。

I think 100.000 connections is not a feasible goal.

TCP/IP ports are 16bit numbers. So anything above 65535 is a no go anyway.

深居我梦 2024-07-27 15:22:11

Windows XP(大概还有其他版本)对任一时间允许的开放端口数量有限制。 这篇 MSDN 文章可能有助于修改 TcpIp 参数以增加可用端口的数量,并减少 Windows 在允许重新使用端口之前保持端口打开的时间。

Windows XP (and other versions presumably) have limits on the number of open ports allowed at any one time. This MSDN article may be helpful in modifying the TcpIp parameters to increase the number of ports available and decrease the amount of time Windows holds the port open before it's allowed to be reused.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文