5

我有 C# 套接字服务器。最大客户端数谁能够与服务器连接大约 800。如果客户端超过 800 个新客户端,则会收到套接字错误 WSAECONNREFUSED 10061。如何 raizeup max clients 计数?

socket.BeginXXX 和 socket.EndXXX 之间的套接字写入。目标:框架 4.0。协议:IP4、TCP

4

3 回答 3

4

侦听器积压队列已满。当积压队列已满时,Windows 将开始向进一步的传入连接发送 RST,这些连接在相关客户端上变为“连接被拒绝”。您可以根据此处的其他答案提高积压队列长度,但这真正意味着您处理接受的速度不够快。仔细查看执行此操作的代码,并为路径加油。确保它不做任何其他事情,例如阻塞 I/O、磁盘 I/O、其他网络操作。一旦连接被接受,它就会离开积压队列,因此其他传入连接可以成功。

于 2012-06-13T11:26:31.180 回答
2

将 serversocket 设置为其侦听状态时,您可以设置 backlog。这是可以等待接受的最大连接数。

其他一切都可能是硬件问题 - 尝试在另一台机器上运行程序。

这是一个示例

Socket serversocket = ...
serversocket.Listen(1000);
于 2012-06-13T11:00:13.237 回答
2

嗨,我找到了我的问题的答案。我为接受连接创建了额外的线程。例如:

以前的

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 进行读写。

于 2012-06-15T05:07:23.337 回答