How many connections can the Indy TCPServer support?

Source: Internet
Author: User

The most recent project, the first to use Idtcpserver, was to connect only 800 more clients (a dozens of-byte message per second after each client connection), and the server responded, at the time of the big stress test. But it lasts no more than 10 minutes and the server hangs up (often the server suddenly shuts down without any hints). Later, after optimizing the mutex, you can connect to more than 1000 clients. But the problem with the server disappearing is still there.
Today, another double cpu,4g memory server on the test, incredibly can only connect to more than 2000 clients. And then changed the Indy10.1.5 server only makes simple connection and reply, the client sends only one message after the connection, or the server can only connect to more than 2000 clients.
And then downloaded the official Help file for Indy10 (http://www.projectindy.org/downloads/IndyDocs_10.1.5.0_HtmlHelp.zip) It's TCPServer help with a passage:
But there is performance and resource limitations imposed by the platform or Operating System on the number of threads th At can is created. On Windows, for example, the number of threads was limited by the available virtual memory. By default, every thread gets one megabyte of stack space and implies a theoretical limit of 2028 threads. Reducing the default stack size would allow more threads to be created, but would adversely impact system performance.
In theory, Windows has an operating system that supports up to 2028 threads. Then fainted. Because there will be at least 5,000 of our clients.
Later, there was an asynchronous non-blocking tcpserver for Delphi, and there was no problem with testing 15,000 clients connected to each other.
Is Indy really only able to support more than 2000 client connections?
Heroes have used the Indy TCP server, how many clients can your server connect to?


I've never done this stress test before.

There really is such a large number of connections, it is recommended to do Iocp+winsock API, do not use the control

Windows has a different operating system that only supports 2028 threads!
With the CreateThread test, tens of thousands of are OK, just getting slower.

A large number of connections are very attention to memory leaks and other issues, and need to modify the source

The INDY10 supports IOCP and fiber, but installs the Supercore package (not by default) and sets the server's Iohandle, otherwise Indy10 's performance is the same in Windows and Indy9

Reducing the stack space on a thread can increase the number of threads
Alternatively, adding the/3GB option to the boot. Ini xp+ (just xp+, excluding Win2K) can also increase the address space available to the process.
But anyway,

On 32-bit windows, the maximum number of synchronization threads is at this level of 2000~3000.

Support for more users can use distributed processing, that is, your primary server to the client trying to connect to other servers to assign. Like QQ, Hotmail, and so on.
Each time you connect is www.hotmail.com, but the specific completion of the service may be xxxxx.xxxx.hotmail.msn.com and the like.

A server can handle 2000 connections is good, more than a few servers.

To Ly_liuyang,thx, looked at the IOCP, has not begun to do on the faint. Continue studying tomorrow.
To Getit911,thx, how can I not install Supercore package Ah, compile pass but, how do you install? I also saw Indy's help say fiber, then also strange, how his iohandler inside no this thing, are thread.

Another top, tomorrow to knot.

The IO model used by Indy TCPServer is a multi-threaded blocking socket, which is not a good choice for more than 1000 concurrent connections, and INDY10 does not support IOCP. The fibers inside are ported on Windows for the traditional UNIX multi-process model. In this case, it is recommended to use IOCP, see MSDN, write it yourself, it's not very complicated

Using a thread to bind a client connection is less efficient,

For intensive applications, it is not possible to use a indy such a control, he is too complex, but no efficiency.

For such applications, can only be used in a non-blocking way, although it is troublesome, but only this approach, let a thread for multiple customer service.

At the same time, to use the thread pool, do not release the thread if the client disconnects, and the thread can hang up and save it for reuse.


Distributed processing is the best way.
The hardware price for adding more servers is negligible,
Even if you are using a more complex model, complete the port + thread pool,
The number of users that can be increased is also limited, it is 5%.
And this model is also difficult to transplant.


I also thought of a way to improve the load.
is to create multiple processes, the number of threads in a single process is limited, but using multiple processes avoids this result.

It is possible to increase the number of processes flexibly depending on the load situation, however, a process can only open a handle to the same port, as if there is a copy handle, allowing other processes to share the same socket handle.

If this is done, it's not limited by the number of threads, but I'm not quite sure how Windows implements blocking the socket, blocking it as if it's looping, not releasing the thread, inefficient, and should use an asynchronous socket so that the waiting thread hangs.


Upstairs, it's impossible.
The total number of threads in a system is limited, because
Each thread is assigned a tcb,4kb, which cannot be swapped to a virtual storage, but also allocates a local stack.
The limit of memory space determines that the number of threads cannot be many.
And the active thread increases to a certain program, the system response speed is severely reduced.


Multiple processes are far more resource-intensive than multithreading.
If you are a multi-user long connection, consider using IOCP to serve a large number of customers with a few threads.
As for the thread pool, it is generally used. Can play a role in a large number of short-link communications.
Woo~ seems to be a very heated discussion. Add some more points. More discussion.

Made a simple tcpserver with the pure Winsock API, no IOCP
threading, a server can only create a maximum of 2000 or so connection threads, followed by 10053 (software caused a connection cancellation) error. Proves not a problem with Indy controls.

Then try the async mode again.

The program code for the Winsock API is as follows:

Program Winsocksvr;

{$APPTYPE CONSOLE}

(*-------------------------------------------------------------------
Description: TCP/IP Server demo program Ver0.1

Using standard WinSock2.2 API functions

With thread blocking mode, each client connection creates a send and receive thread.

Zuni

Date: 2006-3-20

Description of the function to use:

WSAStartup//Initialize Winsock
WSACleanup//Release Winsock reference

Socket//Create socket, for the server, is to create a listening socket
Bind//Bind listening socket to native
Listen//Start listening
Accept//Receive client connections
RECV//Accept data from socket
Send//transmit data to the socket
Closesocket//Close socket


Getpeername//TSOCKADDR structure of the client that gets the socket
Inet_ntoa//Turn the IPV4 network address into a point address
Ntohs//network byte order conversion to host byte order
Htons//Turn host byte order into network byte order

-------------------------------------------------------------------*)

Uses
WinSock, System, Sysutils, Strutils, Scktcomp, Windows;

Const
Def_buf = 4095;

Var
Iport:integer = 2004;
Becho:boolean = True;
Icount:integer = 0;

Procedure useage;
Begin
Writeln (' Usage:winsocksvr [-p:x] [-o] ');
Writeln ('-p:x Port number to listen on ');
Writeln ('-n:do not echoes the Receved World ');
Writeln (");
Halt;
End

Procedure Doargs;
Var
I:integer;
s:string;
sv:string;
Begin
If Paramcount<1 Then
useage;
For I:=1 to ParamCount do
Begin
S: = Paramstr (i);
SV: = Rightstr (S, Length (s)-3);
if (s[1]= '-') or (s[1]= '/') then
Begin
Try
Case UpCase (s[2]) of
' P ': iport: = Strtoint (SV);
' N ': becho: = False;
End
Except
useage;
End
End Else
useage;
End
End

function Clientthread (param:pointer): Integer; stdcall;
Var
Iret:integer;
S:tsocket;
SAPEER:TSOCKADDR;
CBUF:ARRAY[0..DEF_BUF] of Char;
sbuf:string;
Ileft:integer;
Idx:integer;
I:integer;
Begin
S: = Tsocket (Param);
I: = SizeOf (Sapeer);
Getpeername (S, Sapeer, i);

While True does
Begin
IRet: = recv (S, cbuf, def_buf+1, 0);
If Iret=0 Then
Begin
Writeln (Format (' Client (%s:%d) is close gracefully ', [Inet_ntoa (SAPEER.SIN_ADDR), Sapeer.sin_port]));
Closesocket (s);
DEC (ICount);
break;
End
If Iret=socket_error Then
Begin
Writeln (' recv () function failed, Error code ', WSAGetLastError);
Closesocket (s);
DEC (ICount);
break;
End Else
Begin
Cbuf[iret]: = #0;
Write (Format (' Get message from%s:%d ', [Inet_ntoa (SAPEER.SIN_ADDR), Sapeer.sin_port]);
SetLength (Sbuf, IRet);
Sbuf: = Cbuf;
Writeln (SBUF);
End
Send Feedback
If Becho Then
Begin
Sbuf: = ' You sent message: ' + sbuf;
ILeft: = Length (sbuf) +1; Plus 1 is to add a newline character.
For i:=0 to ILeft-2 do
Begin
Cbuf[i]: = sbuf[i+1];
End
Cbuf[ileft-1]: = #10;
IDX: = 0;

while (ileft>0) do
Begin
IRet: = Send (S, Cbuf[idx], ileft, 0);
If Iret=0 Then
Begin
Writeln (Format (' Client (%s:%d) is close gracefully ', [Inet_ntoa (SAPEER.SIN_ADDR), Sapeer.sin_port]));
Closesocket (s);
DEC (ICount);
break;
End
If Iret=socket_error Then
Begin
Writeln (' Send () function failed, Error code ', WSAGetLastError);
Closesocket (s);
DEC (ICount);
break;
End
ILeft: = Ileft-iret;
IDX: = idx + iRet;
End
End
End
Result: = 1;
End

Var
Wsd:twsadata;
SASRV:TSOCKADDR;
SACLT:TSOCKADDR;
Scktlisten:tsocket;
Scktclient:tsocket;
Ilen:integer;
Hthread:thandle;
Dwthreadid:dword;

Begin
Doargs;
Initialize Winsock, version number 2.2
if (WSAStartup ($0202, WSD) <>0) Then
Begin
Writeln (' Fail to load WinSock2.2 ');
Exit;
End Else
Writeln (' Step 1:load WinSock Succ, current Version: ', Inttohex (wsd.whighversion,4));

Creating a listening socket
Scktlisten: = Socket (af_inet, sock_stream, IPPROTO_IP);
If Scktlisten=invalid_socket Then
Begin
Writeln (' Fail to create a listen socket, Error code: ', WSAGetLastError);
WSACleanup;
Exit;
End Else
Writeln (' Step 2:creat listen socket Succ ');

Bind listen socket to native
sasrv.sin_family: = af_inet;
Sasrv.sin_port: = htons (Iport);
Sasrv.sin_addr. S_ADDR: = htonl (Inaddr_any);
If bind (Scktlisten, Sasrv, SizeOf (sasrv)) =socket_error Then
Begin
Writeln (' Fail to bind the listen socket, Error code: ', WSAGetLastError);
Closesocket (Scktlisten);
WSACleanup;
Exit;
End Else
Writeln (' Step 3:bind Succ, Listen port is ', sasrv.sin_port);

Start listening
If Listen (Scktlisten, somaxconn) =socket_error Then
Begin
Writeln (' Fail to listen, Error code: ', WSAGetLastError);
Closesocket (Scktlisten);
WSACleanup;
Exit;
End Else
Writeln (' Step 4:listening ... ');

Writeln (");

While True does
Begin
Accept client Connections
Ilen: = SizeOf (SACLT);
Scktclient: = Accept (Scktlisten, @saClt, @iLen);
If Scktclient=invalid_socket Then
Begin
Writeln (' Fail to accept a client connect, Error code: ', WSAGetLastError);
break;
End Else
Writeln (' Accept client: ', Inet_ntoa (saclt.sin_addr), ': ', Ntohs (Saclt.sin_port));
Create a client communication thread
Hthread: = CreateThread (nil, 0, @ClientThread, Pointer (scktclient), 0, dwThreadID);
If Hthread=0 Then
Begin
Writeln (' Fail to create client thread, Error code: ', GetLastError);
break;
End
INC (ICount);
Writeln (Format (' ... ..... ..... ...) ................ Count:%d ', [ICount]);
CloseHandle (Hthread);
End

Closesocket (Scktlisten);
WSACleanup;
End.

http://blog.csdn.net/tercel99/article/details/46689445

How many connections can the Indy TCPServer support?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.