Article View: borland.public.cppbuilder.internet.socket
Article #2975Re: Non-blocking socket - how to detect when server has finished writing to client
From: Vladimir Stefanovic
Date: Sun, 01 Jan 2006 07:43
Date: Sun, 01 Jan 2006 07:43
92 lines
3776 bytes
3776 bytes
Hi Mark,
> [...] the reply to all POP3 commands is either very short, or multi-line
> and terminated with a line starting with a full stop. In the long requests
> like
> RETR and TOP x y, I stop waiting to read any more when I get this line
> in my buffer.
Yes you must know the rules (protocol) to finalize you block of received
data.
if "AnsiString(bf).Pos("\r\n.")>0" is the rule, than OK.
> [...] Also, is 2048 a good read value to use?
Yes it is, but you *must not* count on that.
I already posted for you in attachments group the whole
TClientSocket/TServerSocket
code, which has all elements of ctNonBlocking / stBlocking architecture.
This is PSEUDO, code, what you have to do:
void __fastcall TClientForm::AskClientSocketRead(TObject *Sender,
TCustomWinSocket *Socket)
{
// read the incoming size of data that are passed to client...
int IncomingSize = Socket->ReceiveLength();
// if its less than zero, feel free to return and wait for another try...
if ( IncomingSize < 1 )
return;
// calculate the LAST size of your data buffer where you store the
portions...
// ... sent to client to finalize one logical block of data...
int OldSize = DataBuffer->Size;
// set the new position where the new reading start...
DataBuffer->Position = OldSize;
// calculate the NEW size of DataBuffer
int NewSize = OldSize + IncomingSize;
// set the new size of the buffer, so that we can work with allocated
buffer
DataBuffer->Size = NewSize;
// this will read the sent data into DataBuffer
Socket->ReceiveBuf( &((LPBYTE)(DataBuffer->Memory))[OldSize],
IncomingSize );
// now, check if it's the end of header...
if ( /* somethng with Pos("\r\n.") on dataBuffer* / )
{
// if yes, then:
// 1) place the DataBuffer in your Grid
// 2) reset the DataBuffer's Size & Position
// DataBuffer->Size = NewSize;
// DataBuffer->Position = 0;
}
}
...and this is DataBuffer:
//---------------------------------------------------------------------------
__fastcall TClientForm::TClientForm(TComponent* Owner)
: TForm(Owner)
{
DataBuffer = new TMemoryStream();
DataBuffer->Clear();
DataBuffer->Position = 0;
}
__fastcall TClientForm::~TClientForm()
{
delete DataBuffer;
}
Look at my code in attachemnts group.
Also you may look at the generic code for all sending amd receiving of
Remy Lebeau, which is placed many times before in this group:
http://tinyurl.com/dkbe9
Happy New Year
--
Best regards,
Vladimir Stefanovic
Technical Details
Message-ID:
<43b7878e@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<43b66ea8@newsgroups.borland.com> <43b6bd83@newsgroups.borland.com> <43b70dbb$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,e5c1e8b6c618095e X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Vladimir Stefanovic" <antivari@po.sbb.co.yu> Newsgroups: borland.public.cppbuilder.internet.socket References: <43b66ea8@newsgroups.borland.com> <43b6bd83@newsgroups.borland.com> <43b70dbb$1@newsgroups.borland.com> Subject: Re: Non-blocking socket - how to detect when server has finished writing to client Date: Sun, 1 Jan 2006 08:43:01 +0100 Lines: 94 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 82.117.212.65 Message-ID: <43b7878e@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1136101262 82.117.212.65 (31 Dec 2005 23:41:02 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail