I am developing a program on top of a basecode taking care of low level socket programming. The problem seems to be that the only way it knows (apparently) when to stop close the connection is when the amount of bytes received exceed that given in the "Content-length" field. But since that fi开发者_StackOverfloweld is not set for many sites I don't know how to tell it to stop. What happens now is that in those cases the entire file is downloaded but the connection is still kept.
There must be something to look for in the incoming data/messages? Thanks.
Since you mention a Content-Length
header, are you downloading from an HTTP server? If so, then the Content-Length
header is omitted when a Transfer-Encoding
header indicates that a chunked
transfer is being used, which means the data is sent in small sequential chunks (thus the full size cannot be reported in the Content-Length
header ahead of time). Each chunk has its own header that specifies the size of its chunk. You need to parse and discard each chunk header so you do not save them in your target file, only the chunk data. The end of the file is reached when you receive a chunk header that reports a cheunk data size of 0.
Read RFC 2616 Section 3.6.1 for more details.
精彩评论