views:

175

answers:

3

i have content length available in the beginning of the download. so i know how much bytes i need to request for. i download in chunks of 1024 bytes. in the last chunk i request for the number of bytes remaining. i am using the read function. but the last chunk takes a lot of time to arrive. is it normal?

+3  A: 

Maybe your last chunk is not big enough to flush the buffer.

You could check how to flush the fd and do it manually after last chunk is sent.

Arkaitz Jimenez
i could not get your point.
iamrohitbanga
When you send 1024 bytes, it looks big enough for the file descriptors internal buffer to flush by itself. If the content size is small the fd will wait a timeout and then flush, or maybe when you close it.
Arkaitz Jimenez
i am asking for as many bytes as required for the last chunk and not 1024.
iamrohitbanga
Again, it doesn't happen with 1024 apparently, but with less... "If the content size is small the fd will wait a timeout and then flush, or maybe when you close it"
Arkaitz Jimenez
The question is not about *sending*, it’s about *receiving*.
Bombe
See Nagle's algorithm for why this can occur. Looks like your problem is the server does not close the connection after the content is written to the socket.
phsiao
+2  A: 

No. My guess is that the server is missing a call to flush() so the output hangs in some buffer until it gets a timeout (and then, the server will flush).

Aaron Digulla
but then my web browser should also take time to download the page. that is not the case. browser loads the page immediately. whereas my program takes about 8 seconds, waiting for the last chunk most of the time.
iamrohitbanga
the browser will begin to act based on what it has been sent so far. for instance, in PHP it is regarded as a good idea to call `flush` after you write out the `head` of a webpage so the browser can start loading the contents of the `head` while the rest of the page is writing/loading. browsers will also try to parse/render pages as soon as it gets the markup across the wire. that's likely why you see the immediate rendering in a browser.
geowa4
ok forget the browser. i see the same behavior with telnet.
iamrohitbanga
It works in the browser because the browser has cached the page.
Aaron Digulla
A: 

Thanks i figured out the problem. my code uses an application level buffer and hence the last chunk takes a long time to download. i have been using robust io functions given in Bryant's book. i had studied that code some time back and had forgotten about it. i revised the code and found buffering being used by the code.

bryant's book - rio functions

Another mistake that i was making was using HTTP/1.1. HTTP 1.0 causes the server to close the connection once it has transmitted the data. So that solved the problem.

iamrohitbanga
You could use the request header `Keep-Alive: close` in HTTP/1.1
Piskvor