"iwasinnihon" <> wrote in message
news: oups.com...
>I am writing a simple program to receive over http using windows
> sockets. my program does fine receiving text files. But returns the
> incorrect data when receiving image files. (The size of the files are
> different). My code is below. The header has already been read using
> the same Receive() function. What have I done wrong.
>
> ofstream os("file.dat", ios::binary);
>
> //Retrieve the body
> try {
> while (1) {
> l = s.Receive();
> if (l.empty()) break;
If found that while receiving binary files it is possible to not receive a
packet during a read, yet the file is still transmitting. In my own code to
receive a binary file via HTTP I don't quit until I reach the file size:
if ( TallyBytesDownloaded >= TotalFileSize )
Since my code is totally different than yours, you probably wouldn't use and
if statement but put it in the while statement.
while ( BytesDownloaded < FileSize )
{
// ...
}
>
> os.write(l.c_str(), l.length());
>
> //cout << l;
> //length += l.length();
> //cout.flush();
> }
> }
> catch (const char* s) {
> cerr << s << endl;
> }
> catch (string s) {
> cerr << s << endl;
> }
> catch (...) {
> cerr << "unhandled exception\n";
> }
> os.close();
>
> string Socket::Receive() {
> string ret;
> while (1) {
> char r;
>
> switch(recv(sock, &r, 1, 0)) {
> case 0:
> return "";
> case -1:
> if (errno == EAGAIN)
> return ret;
> else
> return "";
> }
>
> ret += r;
> if (r == '\n') return ret;
This is a binary file you're downloading, don't expect carrige returns or
line feeds. Just return whatever you received (which is why you may have a
blank packet)
> }
> }
>
|