简体   繁体   中英

C# Socket Server not have more 800 clients

I have C# socket sever. Max clients count who able to connect with server about 800. If clients more then 800 new clients get socket error WSAECONNREFUSED 10061. How raizeup max clients count?

Socket write between socket.BeginXXX and socket.EndXXX. Target: framework 4.0. Protocols: IP4, TCP

The listener backlog queue is full. When the backlog queue is full Windows will start sending RSTs to further incoming connections, which become 'connection refused' at the client(s) concerned. You can raise the backlog queue length as per other answers here, but what it really means is that you aren't processing accepts fast enough. Take a good look at the code that does that, and grease the path. Make sure it doesn't do anything else , such as blocking I/O, disk I/O, other network operations. Once the connection is accepted it is off the backlog queue so other incoming connections can succeed.

When setting the serversocket to its listen state you can set the backlog. That is the maximum number of connections that can be waiting to be accepted.

Everything else is possibly a hardware issue - try running the program on a different machine.

Here is an example of a

Socket serversocket = ...
serversocket.Listen(1000);

Hi i find answer on my question. I create additional thread for accept connection. For example:

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);
            }
        }

By this way client connection count no raize 800

Currently i write this:

 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");
                }
            }
        }

By this way client connection count raize over 2000. Read and write i do with BeginXXX and EndXXX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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