🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #2968

Re: Non-blocking socket - how to detect when server has finished writing to client

#2968
From: Vladimir Stefanovic
Date: Sat, 31 Dec 2005 17:20
65 lines
3223 bytes
> // Globals char *bf, int wh=0; and bool dun=false; to begin with.
> //---------------------------------------------------------------------------
> void __fastcall TForm1::ClientSocket1Read(TObject *Sender,TCustomWinSocket 
> *Socket)
> {
>  int ii,jj=Socket->ReceiveLength();
>  while (jj>0)
>  {
>    ii=Socket->ReceiveBuf(&bf[wh],jj);
>    wh+=ii; bf[wh]='\0'; Sleep(200); jj=Socket->ReceiveLength();
>  }
>  dun=true;
> }

This code (IMO) is not reliable, although it may work for some time.

The presented concept relies on the assumption that *all* the buffer will
be received in *one* pass, ie on *one* ClientSocket1Read().

In practice, max 8192 really will be sent to: jj=Socket->ReceiveLength(),
or less if the header is smaller that 8192, which it is most of the time.

But, the buffer of 8192 is not guaranteed at all to be sent.

In real world, *one* SEND (on server side) is not *one* RECEIVE
(on client side). The data is SENT or RECEIVED in *portions*.

AFAIK, it's the responsibility of YOUR code to *finalize* the block
of code which is received. Typically, the client & server must nagotiate
about the protocol, for example

buffer_length BUFFER

...so when staring receiving, the server (or client in your case) reads 
those
4 bytes (buffer_length) and knows how much data is to be received. Also
sometimes, OnDisconnect can be the initiator to finalize the block of data.

Now, I will post you the *working code* example for this scenario:

1) client asks the server if autoexec.bat exeists on server machine
2) server receives request
3) server decodes request
4) server sends client the answer
5) client receives answer
6) client shows the message

In this example, the point 5, is ClientSocket1Read(), which
you are interested in.

P.S.

Also, I strongly suggest you (in the future) to look at Indy
set of components which is MUCH more easier for usage,
and does the threading for you internaly, and may alredy have
you task solved in the presented components.






-- 
Best regards,
Vladimir Stefanovic
Technical Details
Message-ID: <43b6bd83@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newscon06.news.prodigy.com!newscon02.news.prodigy.com!prodigy.net!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
References: <43b66ea8@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>
Subject: Re: Non-blocking socket - how to detect when server has finished writing to client
Date: Sat, 31 Dec 2005 18:20:58 +0100
Lines: 68
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
NNTP-Posting-Host: 82.117.212.65
Message-ID: <43b6bd83@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1136049539 82.117.212.65 (31 Dec 2005 09:18:59 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newscon06.news.prodigy.com!newscon02.news.prodigy.com!prodigy.net!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