Solved

Knowing how much data that is received using TCustomIPClient?

Posted on 2004-04-18
5
868 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Print Graphic and Text to Epson TM-T88v 12 380
Dev express lookupcombo 3 35
can i put an image from resource into image tag ? 6 27
Delphi Seattle StructureView color 1 14
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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