Article View: borland.public.cppbuilder.internet.socket
Article #5382TCP Client/Server comms using Indy 9
From: Alfred Thomas
Date: Mon, 30 Aug 2004 08:34
Date: Mon, 30 Aug 2004 08:34
54 lines
2359 bytes
2359 bytes
Hi All
I need to write an application using Indy's TIdTCPClient and TIdTCPServer. All I need is for the server to accept a connection and receive a text string, and the client to make a connection and send a text string out. Im having great difficulties with indefinate time-outs, exceptions etc.
My code is as follows:
In my TIdTCPServer->OnExecute:
char * Buffer = NULL;
TMemoryStream * pStrm = NULL;
try
{
AThread->Connection->ReadFromStack();
pStrm = AThread->Connection->InputBuffer;
if(pStrm->Size > 0)
{
Buffer = new char[pStrm->Size + 1];
ZeroMemory(Buffer,pStrm->Size + 1);
pStrm->Position = 0;
pStrm->Read(Buffer, pStrm->Size);
pStrm->Clear();
RequestEngine->RunQuery("X1001",AnsiString(Buffer));
}
}
catch(Exception &E)
{
ShowMessage(E.Message);
}
And on a Timer event the TIdTCPClient read:
try
{
IdTCPClient->ReadFromStack();
TMemoryStream *pStrm = IdTCPClient->InputBuffer;
if(pStrm->Size > 0 )
{
Buffer = new char[pStrm->Size + 1];
ZeroMemory(Buffer,pStrm->Size + 1);
pStrm->Position = 0;
pStrm->Read(Buffer, pStrm->Size);
pStrm->Clear();
ShowMessage("Client: " + AnsiString(Buffer));
}
}
catch(Exception &E)
{
ShowMessage(E.Message);
}
I have seen other demos where ReadLn is used and I have tried that, with no success. Can anyone please tell me what the best way for reading data from client and server TCP connections is
Regards
Alfred Thomas
Technical Details
Message-ID:
<4132e67f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 11508e,342abc5fb29e757e,start X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Alfred Thomas" <alfred@fischerint.co.za> Sender: "Alfred Thomas" <alfred@fischerint.co.za> Reply-To: "Alfred Thomas" <alfred@fischerint.co.za> Subject: TCP Client/Server comms using Indy 9 Newsgroups: borland.public.cppbuilder.internet.socket X-User-Info: 196.25.217.126 X-Forwarded-For: 192.96.150.59 NNTP-Posting-Host: 207.105.83.66 Message-ID: <4132e67f$1@newsgroups.borland.com> Date: 30 Aug 2004 01:34:07 -0700 X-Trace: newsgroups.borland.com 1093854847 207.105.83.66 (30 Aug 2004 01:34:07 -0700) Lines: 54 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail