🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #1518

TidTCPServer and disconnections problems

#1518
From: Stefano Bonifazi
Date: Fri, 12 Jan 2007 21:10
85 lines
3481 bytes
Hello everybody :)
  I have a client-server application implemented by using Indy 
TidTCPServer and TidTCPClient (Indy9 current Development snapshot).
Both client and server work fine, but only because the client send a 
specific order to the server when it finishes its job.. so the server 
inside its OnExecute event handler disconnect that.
Anyway if for some reason the user close the client application before 
it can send the disconnect command, the server application crashes (( .. 
even if I wrapped the code inside OnExecute inside a try catch!
The same happens if I try to close the server application before all 
clients have disconnected!
this is my code inside OnExecute:
  try
  {
   TNodzClientData* pNodz=(TNodzClientData*)AThread->Data;
   if(!pNodz)
    return;
   //Wait for instruction's beginning mark
   AThread->Connection->ReadLn("�");
   //first get command (integer)
   int command=AThread->Connection->ReadInteger(false);
   //get DataSize
   int datasize=AThread->Connection->ReadInteger(false);
   //allocate memory for Data
   unsigned char* Buffer = new unsigned char[datasize];
   //read data and put it into buffer
   AThread->Connection->ReadBuffer(Buffer,datasize);
   AnsiString Data=AnsiString((char*)Buffer,datasize);

   switch(command)
   {
    case cmdStringCommand:
     if(Data=="Disconnect")
      AThread->Connection->Disconnect();
     break;
    ...

   }
   //wait for instruction ending mark
   AThread->Connection->ReadLn("�");
  }
  catch(...)
  {

  }

Inside the OnDisconnect I call thread safe code for deleting my custom data

TDestroyClientData(AThread,NodzServerLog,pConnectedNodzClientsList).Synchronize();

where that class is defined:

class TDestroyClientData : public TIdSync
{
     protected:
         TNodzClientData* pNodzClientData;
         TRichEdit* Log;
         TList* pList;
         TIdThread* pThread;
         void __fastcall DoSynchronize()
         {
          try
          {
           Log->Lines->Add("Nodz Client disconnected");
           TNodzClientData* pNodz=(TNodzClientData*)pThread->Data;
           pList->Remove(pNodz);
           delete pNodz;
           pThread->Data=NULL;
          }
          catch(...)
          {

          }
         }
     public:
         __fastcall TDestroyClientData(TIdThread* AThread, TRichEdit* 
pLog,TList *pL)
             : TIdSync(AThread), pThread(AThread), Log(pLog), pList(pL) {}
};

How should I correct my code in order it can handle Client application 
closes and even Server closes without crashes??

Thank U very much in advance!!
Stefano B.
Technical Details
Message-ID: <45a7f92c@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,827d16ab792ef887,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII
Date: Fri, 12 Jan 2007 22:10:24 +0100
From: Stefano Bonifazi <stefboombastic@email.it>
User-Agent: Thunderbird 1.5.0.9 (Windows/20061207)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: TidTCPServer and disconnections problems
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
NNTP-Posting-Host: 80.104.236.22
Message-ID: <45a7f92c@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1168636204 80.104.236.22 (12 Jan 2007 13:10:04 -0800)
Lines: 85
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail