Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9080695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:07:58+00:00 2026-06-16T20:07:58+00:00

I have three questions: is it possible to destroy IdTCPServer by to many connection?

  • 0

I have three questions:

  1. is it possible to destroy IdTCPServer by to many connection?
    I tried to test my application and when I have several connections – it works very good (even several days) but when sometimes number of connection increases application gives acess violation. I wrote application similates 50 clients sending data constantly (with only sleep(200)). And in this situation IdTCPServer gives exceptions?
    My application reseives information from clients by onExecute event and modyfies databases table using
    TidNotify and TIdSync classes. I believe it protects crosses connections threads?
    Sending information to clients is doing by TTimer (it is only now, I’ll change it to other thread).
    Have I use in this situation special protection or something like that is enough:

    type
      PClient = ^TClient;
      TClient = record
        Activity_time:TDateTime;
        AContext: TIdContext;
      end;
    ...
    list := server.Contexts.LockList;
    try
     for i := 0 to list.Count - 1 do
      with TIdContext(list[i]) do
      begin
    
        if SecondsBetween(now(), PClient(data)^.activity_time) > 6 then
        begin
          Connection.IOHandler.Close;
          Continue;
        end;
        try
          Connection.IOHandler.writeln('E:');
        Except
          Connection.IOHandler.Close;
        end;
      end;
    finally
      server.Contexts.UnlockList;
    end;
    

2.Is a simple way to refuse connection when server is to busy (I think my database isn’t complicated (100 rows, only one row is modyfied by one connection) but maybe here is a way to keep stability of server?

3.I know that this question was repeating many times but I didn’t find satisfying answer: how to protect application to avoid message exception: “Connection closed gracefully” and “Connection reset by peer”?

Thank You for all advices

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:07:59+00:00Added an answer on June 16, 2026 at 8:07 pm

    is it possible to destroy IdTCPServer by to many connection?

    You are asking the wrong question, because you are not actually destroying TIdTCPServer itself, you are simply closing idle connections from an outside thread. That kind of logic can be (and should be) handled inside of the OnExecute event instead, where it is safest to access the connection, eg:

    type
      PClient = ^TClient;
      TClient = record
        Activity_time: TDateTime;
        Heartbeat_time: TDateTime;
        AContext: TIdContext;
      end;
    
    procedure TForm1.serverConnect(AContext: TIdContext);
    var
      Client: PClient;
    begin
      New(Client);
      Client^.Activity_time := Now();
      Client^.Heartbeat_time := Client^.Activity_time;
      AContext.Data := TObject(Client);
    end;
    
    procedure TForm1.serverDisconnect(AContext: TIdContext);
    var
      Client: PClient;
    begin
      Client := PClient(AContext.Data);
      AContext.Data := nil;
      if Client <> nil then Dispose(Client);
    end;
    
    procedure TForm1.serverExecute(AContext: TIdContext);
    var
      Client: PClient;
      dtNow: TDateTime;
    begin
      Client := PClient(AContext.Data);
      dtNow := Now();
    
      if SecondsBetween(dtNow, Client^.Activity_time) > 6 then
      begin
        AContext.Connection.Disconnect;
        Exit;
      end;
    
      if SecondsBetween(dtNow, Client^.Heartbeat_time) > 2 then
      begin
        AContext.Connection.IOHandler.WriteLn('E:');
        Client^.Heartbeat_time := dtNow;
      end;
    
      if AContext.Connection.IOHandler.InputBufferIsEmpty then
      begin
        if not AContext.Connection.IOHandler.CheckForDataOnSource(100) then
          Exit;
      end;
    
      // process incoming data as needed ...
    
      Client^.Activity_time := Now();
    end;
    

    Is a simple way to refuse connection when server is to busy (I think my database isn’t complicated (100 rows, only one row is modyfied by one connection) but maybe here is a way to keep stability of server?

    The current architecture does not allow for refusing connections from being accepted. You can let the server accept connections normally and then close accepted connections when needed. You can do that in the OnConnect event, or you can set the server’s MaxConnection property to a low non-zero number to allow the server to auto-disconnect new connections for you without wasting resources creating new TIdContext objects and threads for them.

    Another option is to call the server’s StopListening() method when the server is busy, so that new connections cannot reach the server anymore, and then call the server’s StartListening() method when you are ready to accept new clients again. Existing clients who are already connected should not be affected, though I have not actually tried that myself yet.

    I know that this question was repeating many times but I didn’t find satisfying answer: how to protect application to avoid message exception: “Connection closed gracefully” and “Connection reset by peer”?

    You should not avoid them. Let them happen, they are normal errors. If they are happening inside of the server’s events, just let the server handle them normally for you. That is how TIdTCServer is designed to be used. If they are happening outside of the server’s events, such as in your timer, then just wrap the socket operation(s) in a try/except block and move on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query that returns test questions, possible answers to those questions
There have been several questions posted to SO about floating-point representation. For example, the
There have been several questions over the past few days about the proper use
I know there have been many questions on grid and pack in the past
I have three questions to ask If I create only one block of threads
there are three (open) questions about the internationalization of GWT, I have: 1) Is
I've got three questions here. First, does pom.xml of maven have any difference from
I was redirected here by Shopify support. I have three main questions for a
I have three general JavaEE + Tomcat questions: According to this link ( Using
Possible Duplicate: How to create a Menubar application for Mac I have implemented a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.