How to tell if end of remote file is reached using Winsock (VB6)?

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 field 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.

1 Answer

  1. Leopold- Reply

    2019-11-15

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>