🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#2966
From: Mark Jacobs
Date: Sat, 31 Dec 2005 10:42
27 lines
2223 bytes
I have a TClientSocket which is acting like a POP3 mail reader. I get all the headers and put them into a sortable 
StringGrid, so I can turf out all the spam before I download the message bodies.

I have one remaining problem which is to do with the delay I am having to put in the socket read routine in order 
to get it to work properly. It is still not perfect, but here is the code :-

// 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;
}

How do you detect when the server has finished sending data to the client in a non-blocking environment? 
Without the Sleep command, ReceiveLength can sometimes report 0 bytes to read, despite there being more 
waiting at the server end to be sent. The delay of a fifth of a second works most of the while on a broadband 
connection (not always), but it really slows the app down. I have also tried if (Socket->LookupState==lsIdle) 
dun=true; and that was no better. Any ideas?

Mark Jacobs
Technical Details
Message-ID: <43b66ea8@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!atl-c02.usenetserver.com!news.usenetserver.com!atl-c04.usenetserver.com!news.usenetserver.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,e5c1e8b6c618095e,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
X-Newsreader: MJ News Reader
From: Mark Jacobs
ReplyTo: markhatesspam@spamcatcher.com
Subject: Non-blocking socket - how to detect when server has finished writing to client
Newsgroups: borland.public.cppbuilder.internet.socket
NNTP-Posting-Host: 81.86.141.233
Message-ID: <43b66ea8@newsgroups.borland.com>
Date: 31 Dec 2005 03:42:32 -0700
X-Trace: newsgroups.borland.com 1136029352 81.86.141.233 (31 Dec 2005 03:42:32 -0700)
Lines: 27
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!atl-c02.usenetserver.com!news.usenetserver.com!atl-c04.usenetserver.com!news.usenetserver.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail