Article View: borland.public.cppbuilder.internet.socket
Article #3246Re: i need ideas/comments with tclientsocket
From: Remy Lebeau (TeamB)
Date: Mon, 31 Oct 2005 19:31
Date: Mon, 31 Oct 2005 19:31
112 lines
4516 bytes
4516 bytes
"Alfred" <alf@no.spam.not.valid.email> wrote in message
news:43662baf$1@newsgroups.borland.com...
> - server get only half package, if i read the "1234"
(Socket->ReceiveBuf(buf,4);)
> and i check Socket->ReceiveLength()
You should be calling ReceiveLength() before ReceiveBuf(), not after. And
you should be buffering everything that is received and then process the
buffer only when enough bytes are available.
> I can see the package is not complete, but If I wait till next
> ClientSocketRead for a full package I cannot read again the
> length for another check before process it.
Yes, you can, if you buffer everything.
> This can not be a problem with a single client becouse i can save
> the lenght into a string somewhere but it is a problem with a
> high traffic server i think.
You need to store buffers on a per-connection basis. You can use the
TCustomWinSocket::Data property for that.
For example:
void __fastcall TForm1::ServerSocket1ClientConnect(TObject* Sender,
TCustomWinSocket* Socket)
{
Socket->Data = new TMemoryStream;
}
void __fastcall TForm1::ServerSocket1ClientDisconnect(TObject* Sender,
TCustomWinSocket* Socket)
{
delete (TMemoryStream*) Socket->Data;
Socket->Data = NULL;
}
void __fastcall TForm1::ServerSocket1ClientRead(TObject* Sender,
TCustomWinSocket* Socket)
{
TMemoryStream *Buffer = (TMemoryStream*) Socket->Data;
int Len = Socket->ReceiveLength();
if( Len < 1 ) // error...
return;
// expand the Buffer ...
int Old = Buffer->Size;
Buffer->Size = ( Old + Len );
// need to do this after changing the Size above in case a memory
// allocation was performed that changes the address that the
// Memory property points to...
LPBYTE *BufPtr = (LPBYTE) Buffer->Memory;
// read the new bytes into the expanded Buffer memory...
int NumRead = Socket->ReceiveBuf(&BufPtr[Old], Len);
if( NumRead < 1 ) // error...
{
// remove the expanded Buffer memory...
Buffer->Size = Old;
return;
}
// loop through the Buffer processing only complete packets...
Buffer->Position = 0;
while( (Buffer->Size - Buffer->Position) >= sizeof(int) )
{
// read the packet length...
Buffer->Read(&Len, sizeof(int));
// if not enough bytes for both the length and the data
// then leave the length in the Buffer until later...
if( (Buffer->Position + Len) >= Buffer->Size )
{
Buffer->Seek(-sizeof(int), soFromCurrent);
break;
}
// process a complete packet...
ProcessPacket(Socket, &BufPtr[Buffer->Position], Len);
// skip past the complete packet...
Buffer->Seek(Len, soFromCurrent);
}
// if any complete packets were processed then remove them from the
Buffer...
if( Buffer->Position > 0 )
{
int NewSize = ( Buffer->Size - Buffer->Position );
::MoveMemory(BufPtr, &BufPtr[Position], NewSize);
Buffer->Size = NewSize;
}
}
void__fastcall TForm1::ProcessPacket(TCustomWinSocket *Socket, void
*Data, int DataLength)
{
// process Data as needed...
}
> I dont like indy
Why not?
Gambit
Technical Details
Message-ID:
<43667112$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.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>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> Subject: Re: i need ideas/comments with tclientsocket Date: Mon, 31 Oct 2005 11:31:33 -0800 Lines: 114 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: <43667112$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1130787090 68.69.17.41 (31 Oct 2005 11:31:30 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail