C # Socket Server does not have more than 800 clients

I have a C # socket sever. Maximum clients consider who can connect to the server for about 800. If clients with more than 800 new clients receive a WSAECONNREFUSED 10061 socket error. How are raizeup maximum clients calculated?

Socket write between socket.BeginXXX and socket.EndXXX. Purpose: wireframe 4.0. Protocols: IP4, TCP

+5
source share
3 answers

The remaining queue for the listener is full. When the delay queue is full, Windows will start sending RST to further incoming connections, which become the “disconnected” connection for the corresponding clients. You can increase the length of the lag queue according to the other answers here, but in reality this means that you are not processing fast enough. Take a look at the code that does this and lubricate the path. Make sure it does nothing, such as blocking I / O, disk I / O, and other network operations. Once the connection is accepted, it will disconnect from the queue so that other incoming connections can be successful.

+3
source

Hi, I find the answer to my question. I create an additional thread to accept the connection. For instance:

Previous

IPEndPoint myEndpoint = new IPEndPoint(IPAddress.Parse(_serverAddress), _port);
 _serverSocket = new Socket(myEndpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

 _serverSocket.Bind(myEndpoint);
 _serverSocket.Listen((int)SocketOptionName.MaxConnections);

_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), _serverSocket);

.....


private void AcceptCallback(IAsyncResult result)
        {
            ConnectionInfo connection = new ConnectionInfo();
            try
            {
                Socket s = (Socket)result.AsyncState;
                connection.Socket = s.EndAccept(result);

                connection.Buffer = new byte[1024];
                connection.Socket.BeginReceive(connection.Buffer,
                    0, connection.Buffer.Length, SocketFlags.None,
                    new AsyncCallback(ReceiveCallback),
                    connection);
            }
            catch (SocketException exc)
            {
                CloseConnection(connection, "Exception in Accept");
            }
            catch (Exception exc)
            {
                CloseConnection(connection, "Exception in Accept");
            }
            finally
            {

                    _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), result.AsyncState);
            }
        }

, 800

:

 IPEndPoint myEndpoint = new IPEndPoint(IPAddress.Parse(_serverAddress), _port);
 _serverSocket = new Socket(myEndpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

 _serverSocket.Bind(myEndpoint);
 _serverSocket.Listen((int)SocketOptionName.MaxConnections);

acceptThread = new Thread(new ThreadStart(ExecuteAccept));
acceptThread.Start();

......

private void ExecuteAccept()
        {

            while (true)
            {

                ConnectionInfo connection = new ConnectionInfo();
                try
                {
                    connection.Socket = _serverSocket.Accept();

                    connection.Buffer = new byte[1024];
                    connection.Socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), connection);
                }
                catch (SocketException exc)
                {
                    CloseConnection(connection, "Exception in Accept");
                }
                catch (Exception exc)
                {
                    CloseConnection(connection, "Exception in Accept");
                }
            }
        }

, 2000. BeginXXX EndXXX.

+2

When you set the serverocket state to its listening state, you can set the lag. This is the maximum number of connections that can be expected to be accepted.

Everything else is probably hardware related - try running the program on another machine.

Here is an example

Socket serversocket = ...
serversocket.Listen(1000);
+1
source

All Articles