🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

system error code 6 when closing TClientSocket

#1331
From: enzo
Date: Thu, 22 Feb 2007 10:06
208 lines
7157 bytes
Using c++builder 6 I build a very simple client, which call a thread to 
connect to a server.
The first connection works well, while when I try to close the thread 
with Terminate and waitFor
I got a system error code 6.
Here a snippet of the log with the example and after tha sample code I 
used.
For the connection I use the standard TClientSocket component, used in 
blocking mode
There is something wrong when I close the thread but I can't gues what.
Please, can someone give me some hint on where I wrong .
Regards, Enzo


[22/02/2007 10.47.12] [ThrID=00E8] [Execute] Nuova connessione: 
192.168.200.218:2023 --> 192.168.200.47:19001
[22/02/2007 10.47.19] [ThrID=00E8] [Execute] END  [ connessione: 
192.168.200.218:2023 --> 192.168.200.47:19001 ]
[22/02/2007 10.47.19] [ThrID=00E8] [ThClientSocket::~ThClientSocket] 
END
[22/02/2007 10.47.23] [BtnConnect-KillConn]ERRORE System Error.  Code: 
6.

Handle non valido


***************************


ThClientSocket * thClntSckt;


void __fastcall TForm1::BtnConnectClick(TObject *Sender)
{
   try
   {
      // -------------------------------------------------------
      try
      {
          if( thClntSckt )
          {
             thClntSckt->Terminate();
             thClntSckt->WaitFor();
             thClntSckt = 0;
             return;
          }
      }
      catch( const Exception & ex )
      {
         String serr = Format( "[BtnConnect-KillConn]ERRORE %s ", 
ARRAYOFCONST(( ex.Message )) );
         Util::mylog( serr );
         return;
      }
      // -------------------------------------------------------
      try
      {
          thClntSckt = new ThClientSocket( true );
          thClntSckt->FreeOnTerminate = true;
          thClntSckt->IPAddress = EditHost->Text;
          thClntSckt->Port      = StrToIntDef( EditPort->Text, 0 );
          thClntSckt->Resume();
      }
      catch( const Exception & ex )
      {
         String serr = Format( "[BtnConnect-NewConn]ERRORE %s ", 
ARRAYOFCONST(( ex.Message )) );
         Util::mylog( serr );
      }
      // -------------------------------------------------------
   }
   catch( const Exception & ex )
   {
      String serr = Format( "[BtnConnect]ERRORE %s ", ARRAYOFCONST(( 
ex.Message )) );
      Util::mylog( serr );
   }
}



/******************************************************
  TClientSocket * mySocket
 ******************************************************/
__fastcall ThClientSocket::ThClientSocket(bool CreateSuspended)
        : TThread(CreateSuspended)
{
    mySocket = new TClientSocket( NULL );
}



/******************************************************
 ******************************************************/
__fastcall ThClientSocket::~ThClientSocket()
{
   String stmp;
   try
   {
       if( mySocket )
       {
          if( mySocket->Active )
          {
              stmp = Format( "[Execute] Chiudi connessione: %s:%d --> 
%s:%d ",
                       ARRAYOFCONST(( mySocket->Socket->LocalAddress,
                                      mySocket->Socket->LocalPort,
                                      mySocket->Socket->RemoteAddress,
                                      mySocket->Socket->RemotePort )));
              mylog( stmp );
          }
          mySocket->Close();
          while( mySocket->Active )
          {
             mylog( "[ThClientSocket::~ThClientSocket] END close 
Socket" );
             Sleep( 500 );
          }
          while( mySocket->Socket->Connected )
          {
             mylog( "[ThClientSocket::~ThClientSocket] END close 
winSocket" );
             Sleep( 500 );
          }
          delete mySocket;
          mySocket = 0;
       }
   }
   catch( const Exception & ex )
   {
      String serr = Format( "[ThClientSocket::~ThClientSocket]ERRORE %s 
", ARRAYOFCONST(( ex.Message )) );
      Util::mylog( serr );
   }
   mylog( "[ThClientSocket::~ThClientSocket] END " );
}



void __fastcall ThClientSocket::Execute()
{

   int fSocketTimeout = 30;
   TWinSocketStream * pStream = 0;
   char buffer[BUFFERSIZE];
   int nloop = 0;
   String sMsg, sMsgInp, stmp;
   int dt1, dt2;

   try
   {
       // -------------------------------------------------------
       if( ! mySocket->Active )
       {
          mySocket->Address = fIPAddress;
          mySocket->Port    = fPort;
          mySocket->ClientType = ctBlocking;
          mySocket->Open();

          stmp = Format( "[Execute] Nuova connessione: %s:%d --> %s:%d 
",
                   ARRAYOFCONST(( mySocket->Socket->LocalAddress,
                                  mySocket->Socket->LocalPort,
                                  mySocket->Socket->RemoteAddress,
                                  mySocket->Socket->RemotePort )));
          mylog( stmp );
       }
       // -------------------------------------------------------
       while( !Terminated && mySocket->Active )
       {
          if( pStream == 0 )
          {
             pStream = new TWinSocketStream(mySocket->Socket, 
fSocketTimeout );
          }

          *********          Here the working code     ********
          *********  but is commented for this example ********


       }
       // -------------------------------------------------------
       stmp = Format( "[Execute] END  [ connessione: %s:%d --> %s:%d 
]",
                 ARRAYOFCONST(( mySocket->Socket->LocalAddress,
                                mySocket->Socket->LocalPort,
                                mySocket->Socket->RemoteAddress,
                                mySocket->Socket->RemotePort )));
       mylog( stmp );
       // -------------------------------------------------------
       if( pStream )
       {
           pStream->WaitForData(fSocketTimeout);
           delete pStream;
           pStream = 0;
       }
       mySocket->Close();
       // -------------------------------------------------------
   }
   catch( const Exception & ex )
   {
      String serr = Format( "[ThClientSocket::Execute]ERRORE %s ", 
ARRAYOFCONST(( ex.Message )) );
      mylog( serr );
      mySocket->Close();
   }

}
Technical Details
Message-ID: <mn.b29a7d72294c86fa.47611@aesys.it>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,363e26bbeac0e744,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
Subject: system error code 6 when closing TClientSocket
From: enzo <enzo.arlati@aesys.it>
Newsgroups: borland.public.cppbuilder.internet.socket
Message-ID: <mn.b29a7d72294c86fa.47611@aesys.it>
X-Newsreader: MesNews/1.06.00.00-it
Date: Thu, 22 Feb 2007 11:06:34 +0100
MIME-Version: 1.0
Reply-To: enzo.arlati@aesys.it
Content-Type: text/plain; charset="iso-8859-15"; format=flowed
Content-Transfer-Encoding: 8bit
NNTP-Posting-Host: 81.208.124.114
X-Trace: newsgroups.borland.com 1172138754 81.208.124.114 (22 Feb 2007 02:05:54 -0800)
Lines: 210
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail