Article View: borland.public.cppbuilder.internet.socket
Article #3260Re: i need ideas/comments with tclientsocket
From: Remy Lebeau (TeamB)
Date: Tue, 01 Nov 2005 02:38
Date: Tue, 01 Nov 2005 02:38
76 lines
3101 bytes
3101 bytes
"Alfred" <alf@no.spam.not.valid.email> wrote in message
news:4366ccd3$1@newsgroups.borland.com...
> I have tryed a ProcessPacket routine like :
That code is wrong for three reasons:
> char *a = new char[DataLength];
1) You are allocating an extra buffer that is not actually being used for
anything.
> a = (char*)Data;
2) you are then reassigning the pointer to point to the Data parameter
instead of the buffer you allocated. You have now lost your pointer to the
buffer. You can never free it, thus leaking the allocated memory.
> delete[] a;
3) Since you are now pointing to the Data parameter, you are freeing memory
that is owned by the TMemoryStream.
The routine should be like this instead:
void__fastcall TForm1::ProcessPacket(TCustomWinSocket *Socket, void
*Data, int DataLength)
{
// process Data as needed...
Memo->Lines->Add(AnsiString((char*)Data, DataLength));
}
> Sender program send an example with :
That code is wrong as well, for the same reasons as above. Not only that,
but you are not checking the return value of SendBuf(), either. You need to
do so, ie:
bool __fastcall TForm1::SendData(TCustomWinSocket *Socket, void *Data,
int DataSize)
{
LPBYTE pBuf = (LPBYTE) Data;
while( DataSize > 0 )
{
int Sent = Socket->SendBuf(pBuf, DataSize);
if( Sent < 1 )
{
if( Sent == 0 )
return false; // disconnected
if( WSAGetLastError() != WSAEWOULDBLOCK )
return false; // error
Sleep(50); // keep waiting
}
else
{
pBuf += Sent;
DataSize -= Sent;
}
}
return true;
}
char *buf = "1234567890";
int len = 10;
if( SendData(Socket, &len, sizeof(int)) )
SendData(Socket, buf, len);
Gambit
Technical Details
Message-ID:
<4366d53d$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!meganewsservers.com!feeder2.on.meganewsservers.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<43662baf$1@newsgroups.borland.com> <43667112$1@newsgroups.borland.com> <4366ccd3$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,5c33b8c6a9ca7fdb X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <43662baf$1@newsgroups.borland.com> <43667112$1@newsgroups.borland.com> <4366ccd3$1@newsgroups.borland.com> Subject: Re: i need ideas/comments with tclientsocket Date: Mon, 31 Oct 2005 18:38:54 -0800 Lines: 78 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <4366d53d$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1130812733 68.69.17.41 (31 Oct 2005 18:38:53 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!meganewsservers.com!feeder2.on.meganewsservers.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail