Article View: borland.public.cppbuilder.internet.socket
Article #4458Re: Error on reconnection to server
From: Ben
Date: Wed, 09 Feb 2005 07:30
Date: Wed, 09 Feb 2005 07:30
162 lines
4833 bytes
4833 bytes
Hi Remy.
The reconnection times can be hours. What is very strange is that
reconnection can helped by pressing a button on the keyboard or starting
another program.
When I would use blocking, what are the concequences for reading and
sending datapackages.
Would it be better to create a clientsocket at runtime and delete it in
stead of reconnecting.
Tnx in advance,
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:
<4209bc24$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.maxwell.syr.edu!newspeer1.nwr.nac.net!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<4208d668$1@newsgroups.borland.com> <4208fa07$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: Wed, 09 Feb 2005 08:30:46 +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: Re: Error on reconnection to server References: <4208d668$1@newsgroups.borland.com> <4208fa07$1@newsgroups.borland.com> In-Reply-To: <4208fa07$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: <4209bc24$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1107934244 80.126.94.138 (8 Feb 2005 23:30:44 -0800) Lines: 162 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.maxwell.syr.edu!newspeer1.nwr.nac.net!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail