Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Socket Receive Binary

Reply
Thread Tools

Socket Receive Binary

 
 
iwasinnihon
Guest
Posts: n/a
 
      01-30-2007
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;
}
}

 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      01-30-2007
"iwasinnihon" <> wrote in
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.


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?)

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


 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      01-30-2007
"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)

> }
> }
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57