Solved

Knowing how much data that is received using TCustomIPClient?

Posted on 2004-04-18
5
858 Views
Last Modified: 2007-12-19
procedure tdownloaders.Execute;
begin
  client := TCustomIPClient.Create(Application);
  client.RemoteHost := Server;
  client.RemotePort := inttostr(Port);

  form_main.Label1.Caption := 'Connecting...';

  if client.Connect = False then
  begin
    messagedlg('Failed to connect to server!',mterror,[mbok],0);
    exit;
  end;

  form_main.Label1.Caption := 'Sending request...';

  if client.Sendln('GET '+Path+'?action=read HTTP/1.0') = -1 then
  begin
    messagedlg('Error while sending request!',mterror,[mbok],0);
    exit;
  end;

  if client.Sendln(chr(10)) = -1 then
  begin
    messagedlg('Error while sending request!',mterror,[mbok],0);
    exit;
  end;

  form_main.Label1.Caption := 'Receiving....';

  while client.WaitForData(5000) do
  begin
    form_main.Memo1.Lines.Add(client.Receiveln);
  end;

  form_main.Label1.Caption := 'Done!';

  client.Free;
end;


That part is OK, but what if I don't want to receive this as a string, but as binary data. Then I need to know the size of how many bytes it's going to receive. If I try to receive a single byte more than it actually has received, it freezes, and in this case where I use a thread, it just stops (timeout possibilities?). There must be a way to know how much it has received, and then read it into a buffer using receivebuf (binary transfers). What I wonder about is:

- A way to know how much it has currently received. I never got the OnReceive event to trigger, so I can't use DataLen there.
- Timeouts on receivebuf.
- An easy way to know if the connection is still established (not broken somehow), without actually sending something and wait for reply.
- What byte does a HTTP server send right before it starts transferring the file (I know it sends HTTP 200 when it's all successfull, then some content info explaining lengths, but what does it send RIGHT before it starts on the first byte on the file, so I know when to start saving what I receive)?
- How to timeout receivebuf or receiveln without creating a new thread just to check if's still receiving. I found nothing about timeouts in TCustomIPClient.
- How can I be 100% sure that what HTTP server delivers me is 100% correct, and that some transfer error hasn't corrupted it?
- What are the ETag, Accept-Ranges, and Connection tags sent by HTTP server mean?

Answer my questions, and I'll spread the points as good as I can.
0
Comment
Question by:chrml0606
  • 2
  • 2
5 Comments
 
LVL 12

Assisted Solution

by:Lee_Nover
Lee_Nover earned 300 total points
ID: 10856885
always send/receive a custom header first so you know what kind of data to expect and how much of it
custom header like :

TCommRec = record
  Version: Byte;
  CommType: Byte;
  Command: Byte;
  Param1: Byte;
  DataSize: Cardinal;
end;


so you always do:

fill in header info
send header
read header
read data if any

this is for blocking sockets (I use Indy ... haven't heard of TCustomIPClient)
0
 
LVL 11

Expert Comment

by:calinutz
ID: 10858088
listening
0
 

Author Comment

by:chrml0606
ID: 10868842
Yeah, I've figured out how to send and receive. I did receive a 700mb file from a dude I know that runs a HTTP server, and the MD5 has was correct. I used the Content-Length param to get the filesize, and then received it in max 4096 bytes chunks, and then used the return value to know if it has received all bytes, and if not, it simple doesn't add that to what it has downloaded, and then only writes what it has downloaded.

Can I trust TCP, so it simply doesn't loose a package completly without letting the program know, or somehow get it corrupted?
And still, what does the HTTP ETag, Accept-Ranges, and Connection tags that are in the return message mean?
0
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 300 total points
ID: 10875241
... so your question is HTTP specific

yes you can trust TCP unless you disable the nagle algorithm (waiting for ack)
TCP does all the checking
etag and the other stuff I don't know about
0
 

Author Comment

by:chrml0606
ID: 10908020
Okay, nice. I've solved 95% of all this stuff on my own now, thanks for answer. I've promised the points, so here you get some :).
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now