简体   繁体   English

C#套接字服务器没有更多的800个客户端

[英]C# Socket Server not have more 800 clients

I have C# socket sever. 我有C#套接字服务器。 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? 最大客户端数计算出能够与服务器连接的人数约为800。如果客户端数量超过800,则新客户端会收到套接字错误WSAECONNREFUSED10061。raizeup最大客户端数如何?

Socket write between socket.BeginXXX and socket.EndXXX. 在socket.BeginXXX和socket.EndXXX之间进行套接字写。 Target: framework 4.0. 目标:框架4.0。 Protocols: IP4, TCP 协议: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. 当积压队列已满时,Windows将开始向进一步的传入连接发送RST,这些传入连接在相关客户端处变为“拒绝连接”。 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. 确保它不执行其他任何操作 ,例如阻止I / O,磁盘I / O,其他网络操作。 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. 将serversocket设置为侦听状态时,可以设置积压。 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 这样,客户端连接数不会增加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. 通过这种方式,客户端连接计数提高了2000倍。读写操作与BeginXXX和EndXXX相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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