Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 237
Андрей Павлов Avatar asked Jun 13 '12 10:06

Андрей Павлов


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


3 Answers

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.

like image 190
user207421 Avatar answered Oct 20 '22 07:10

user207421


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);
like image 26
Johannes Avatar answered Oct 20 '22 05:10

Johannes


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.

like image 36
Андрей Павлов Avatar answered Oct 20 '22 07:10

Андрей Павлов