I
iwasinnihon
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;
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;
}
}
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;
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;
}
}