🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

(code) Re: Error on reconnection to server

#4353
From: Ben
Date: Tue, 15 Feb 2005 12:23
154 lines
4628 bytes
<g> now I forgot to include the code.

That's how things go wrong.

Ben


The most important part of the code follows down here:

The socket form:

void __fastcall TTCPForm::CreateSystem(TObject *Sender)
{
   CameraIn=CameraUit=ServerIn=ServerUit=0;
   if (COMMON::IniVal.UseIPconnection)
   {
     TCPthread=new TTCPthread(false);
     server=COMMON::IniVal.IPaddress;
     port=COMMON::IniVal.IPport;
     if (!ClientSocket->Active)
       OpenConnection(server,port);
     TCPthread=new TTCPthread(false);
   }
   else
   {
     server="";
     port=-1;
   }
   Timer1->Enabled=true;
   DoSend=true;
   DataReceived=false;
}
void __fastcall TTCPForm::ExitSystem(TObject *Sender)
{
   TCPthread->Terminate();
   Timer1->Enabled=false;
   delete TCPthread;
}
void TTCPForm::OpenConnection(String Server, int Port)
{
   ClientSocket->Host=Server;
   ClientSocket->Port=Port;
   if (!ClientSocket->Active)
     try
     {
       ClientSocket->Active = true;
     }
     catch (Exception &exception)
     {
       Application->ShowException(&exception);
     }
}

void TTCPForm::ReopenConnection()
{
   if (!ClientSocket->Active || !TCPForm->ClientSocket->Socket->Connected)
     try
     {
       ClientSocket->Host=server;
       ClientSocket->Port=port;
       ClientSocket->Active = true;
     }
     catch (...) { ; }
}

int TTCPForm::TCPClientSocketSend(TCPmessageType Message)
{
     try
     {
       if (!ClientSocket->Active)
         ReopenConnection();
       if (ClientSocket->Active && TCPForm->ClientSocket->Socket->Connected)
       try
       {
         ClientSocket->Socket->SendBuf((void 
*)&Message,sizeof(TCPmessageType));
       }
       catch (...)
       {
         return -1;
       }
       else
         return -1;
     }
     catch (...)
     {
       return -1;
     }
   return 0;
}

The thread:

void __fastcall TTCPthread::Execute()
{
   //---- Place thread code here ----
   int i=0;
   while (!Terminated)  // start execution of program, loop until terminated
   {
     if (!TCPForm->ClientSocket->Active || 
!TCPForm->ClientSocket->Socket->Connected)  // reconnect
     {
       Sleep(3000);
       TCPForm->ClientSocket->Active=true;
       Sleep(3000);                      // give time to reconnect
       TCPForm->DoSend=true;             // resend data
     }
     else
     {
       if (TCPForm->CameraUit!=TCPForm->CameraIn && TCPForm->DoSend)
       {
         TCPForm->SendPackage(TCPForm->CameraPlantdata[TCPForm->CameraUit]);
         TCPForm->DoSend=false;
       }
       else
       {
         i++;
         if (i==1000)
         {
           TCPForm->DoSend=true;      // retry sending data
           i=0;
         }
         else
           Sleep(1);
       }
     }
   }
}

void __fastcall TTCPForm::ClientSocketRead(TObject *Sender,
       TCustomWinSocket *Socket)
{
   int NmbrOfBytes;
   try
   {
     NmbrOfBytes=Socket->ReceiveBuf((void 
*)&RxMessage,sizeof(TCPmessageType));
     if (NmbrOfBytes!=sizeof(TCPmessageType)) LastError= -2;
     if (LastPlantnummer!=RxMessage.TCPplantnummer ||
         RxMessage.TCPrcode!=6)
     {
       DoSend=true;
       LastError=-2;
       return;
     }
   }
   catch (...)
   {
     return;
   }
   if (++CameraUit>=16) CameraUit=0;
   DoSend=true;
   DataReceived=true;
}
Technical Details
Message-ID: <4211e9a4$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <4208d668$1@newsgroups.borland.com> <4208fa07$1@newsgroups.borland.com> <4209bc24$1@newsgroups.borland.com> <42105bd0$1@newsgroups.borland.com> <4210767f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,7304809fda7b8b4e
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Tue, 15 Feb 2005 13:23:00 +0100
From: Ben <TerNOra@etraSPAMde.nl>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.1 (ax)
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: (code) Re: Error on reconnection to server
References: <4208d668$1@newsgroups.borland.com> <4208fa07$1@newsgroups.borland.com> <4209bc24$1@newsgroups.borland.com> <42105bd0$1@newsgroups.borland.com> <4210767f$1@newsgroups.borland.com>
In-Reply-To: <4210767f$1@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 80.126.94.138
Message-ID: <4211e9a4$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1108470180 80.126.94.138 (15 Feb 2005 04:23:00 -0800)
Lines: 154
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail