Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Knowing how much data that is received using TCustomIPClient?

Posted on 2004-04-18
5
Medium Priority
?
893 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 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