Socket Receive Binary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iwasinnihon

    Socket Receive Binary

    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_st r(), 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(soc k, &r, 1, 0)) {
    case 0:
    return "";
    case -1:
    if (errno == EAGAIN)
    return ret;
    else
    return "";
    }

    ret += r;
    if (r == '\n') return ret;
    }
    }

  • Andre Kostur

    #2
    Re: Socket Receive Binary

    "iwasinniho n" <iwasinnihon@ho tmail.comwrote in
    news:1170125958 .745631.306850@ a34g2000cwb.goo glegroups.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_st r(), 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(soc k, &r, 1, 0)) {
    case 0:
    return "";
    case -1:
    if (errno == EAGAIN)
    return ret;
    else
    return "";
    }
    >
    ret += r;
    if (r == '\n') return ret;
    }
    }
    >
    >

    Comment

    • Jim Langston

      #3
      Re: Socket Receive Binary

      "iwasinniho n" <iwasinnihon@ho tmail.comwrote in message
      news:1170125958 .745631.306850@ a34g2000cwb.goo glegroups.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 ( TallyBytesDownl oaded >= 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_st r(), 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(soc k, &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)
      }
      }
      >

      Comment

      Working...