Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Knowing how much data that is received using TCustomIPClient?

Posted on 2004-04-18
5
Medium Priority
?
903 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 600 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 600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

963 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