Socket Receive Binary

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;
}
}
 
A

Andre Kostur

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.

Out of curiosity.. have you tried this code with a text file that doesn't
end in a newline? (Hint: I suspect you have a problem when recv returns a
0.... what happens to all of the data that you have accumulated since the
last newline?)
 
J

Jim Langston

iwasinnihon said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top