Solved

Knowing how much data that is received using TCustomIPClient?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calling Tcolordialog in dll make the form disappear 8 103
TVirtualStringTree  search using TEdit 7 106
indy Tmemory stream non latin charcter issue 2 98
Base1 Encode/Decode 3 68
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

14 Experts available now in Live!

Get 1:1 Help Now