Socket Receive Binary

Discussion in 'C++' started by iwasinnihon, Jan 30, 2007.

  1. iwasinnihon

    iwasinnihon Guest

    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;
    }
    }
    iwasinnihon, Jan 30, 2007
    #1
    1. Advertising

  2. iwasinnihon

    Andre Kostur Guest

    "iwasinnihon" <> wrote in
    news::

    > 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;
    > }
    > }
    >
    >
    Andre Kostur, Jan 30, 2007
    #2
    1. Advertising

  3. iwasinnihon

    Jim Langston Guest

    "iwasinnihon" <> wrote in message
    news:...
    >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)

    > }
    > }
    >
    Jim Langston, Jan 30, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,767
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    948
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    531
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    648
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    702
Loading...

Share This Page