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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With