Article View: borland.public.cppbuilder.internet.socket
Article #1572Re: Using ReceiveBuff() from a client socket app?
From: Remy Lebeau (TeamB)
Date: Tue, 02 Jan 2007 17:17
Date: Tue, 02 Jan 2007 17:17
103 lines
3895 bytes
3895 bytes
"Chris" <nospam@help.com> wrote in message
news:459a8f2a$1@newsgroups.borland.com...
> The problem that I am having now is on the client side. I do
> recieve the the first 4 bytes using ReceiveBuff() to get the
> correct size that I am trying to receive. However, when I use
> ReceiveBuff() for the second time to start receiving the 1st
> packet, I get an error, "Asynchronous socket error 10053."
Your reading code is completely wrong. When using a non-blocking
socket, you can't read the entire stream inside a single event. When
the OnRead event is triggered, you have to read whatever is actually
available, buffer it somewhere, and then wait for the event to be
triggered again later on to buffer more data. You can then run
through the buffer whenever full packets are available for processing.
Try this instead:
private:
TMemoryStream *InboundBuffer;
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
InboundBuffer = new TMemoryStream;
}
__fastcall TForm1::~TForm1()
{
delete InboundBuffer;
}
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
InboundBuffer->Clear();
}
void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
InboundBuffer->Clear();
}
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
int len = Socket->ReceiveLength();
if( len < 1 )
return;
int old = InboundBuffer->Size;
InboundBuffer->Size = (old + len);
LPBYTE ptr = ((LPBYTE)InboundBuffer->Memory) + old;
try
{
len = Socket->ReceiveBuf(ptr, len);
if( len < 0 )
len = 0;
}
catch(const Exception &)
{
len = 0;
}
InboundBuffer->Size = (old + len);
if( len < 1 )
return;
InboundBuffer->Position = 0;
while( (InboundBuffer->Size - InboundBuffer->Position) >=
sizeof(int) )
{
InboundBuffer->Read(&len, sizeof(int));
if( (InboundBuffer->Size - InboundBuffer->Position) <
len )
{
InboundBuffer->Seek(-sizeof(int), soFromCurrent);
break;
}
// read len bytes from InboundBuffer as use as needed ...
}
if( InboundBuffer->Position > 0 )
{
len = (InboundBuffer->Size - InboundBuffer->Position);
if( len > 0 )
{
ptr = (((LPBYTE)InboundBuffer->Memory) +
InboundBuffer->Position);
MoveMemory(InboundBuffer->Memory, ptr, len);
InboundBuffer->Size = len;
}
else
InboundBuffer->Clear();
}
}
Gambit
Technical Details
Message-ID:
<459aa427$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<459a8f2a$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,41e3cddf77758900 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: <459a8f2a$1@newsgroups.borland.com> Subject: Re: Using ReceiveBuff() from a client socket app? Date: Tue, 2 Jan 2007 09:17:15 -0800 Lines: 106 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: 75.83.34.171 Message-ID: <459aa427$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1167762471 75.83.34.171 (2 Jan 2007 10:27:51 -0800) Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail