Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: Limiting TCP connections

I'm using the TServerSocket component in my Delphi application. I would like to limit client connections, let's say to one thousand. Unfortunately i don't know how to do that. The component does not provide any properties like 'MaxConnections' or any other like this one.

I create new server threads (TServerClientThread) using the OnGetThread event. To get the number of active connections I used the 'Socket.ActiveConnections' property.

Unfortunately I don't know what to do then. I tried not to create any thread inside the OnGetThread procedure when the number of connections is above the limit, but it changed nothing - client, even though it is unable to send or receive any information, it can connect to the server and stay connected. What to do not to allow new clients to connect or just allow them to connect but break the connection instantly?

like image 858
user51138 Avatar asked Jan 03 '09 12:01

user51138


1 Answers

Last time i used Delphi was some years ago, but i had a similar situation to deal with and my experience could be useful for you: i was facing the same problem and didn't want to switch to the "Indy" components since the (big) application wasn't worth the port. As far as i can remember, you should have an onClientConnect event on the server socket and here is were i checked for the limit:

.onClientConnect( Sender: TObject; aSocket: T... )
begin
    if( YourServerSocket.ActiveConnections > YourDefinedMaxConnections )
    begin
        // Drop the connection
        aSocket.Close;
    end;
end

I can't remember more other than that but i think i did something on these lines, or at least this was the thing i came up with.

like image 167
Manuel Avatar answered Nov 18 '22 08:11

Manuel