Sening file via Socket receiving binary for text data

Posted on 2011-10-24
Last Modified: 2012-05-12
Hi All:

I am trying to send text file via a socket to client.....I get binary data instead of the expected source format of text.

Here is the code fragment:

int CPop3S::SendMsgFile(char* szFilePath)
  std::ifstream::pos_type size;
  string line;
  char* memblock;
  long begin, end, size1;
  std::ifstream myfile;
  char c;
  //, ios::binary);"testmail.txt");
  std::cout << "The filename : \n" << szFilePath;

      begin = myfile.tellg();
      getline(myfile, line);
      memblock = new char[3381];
      myfile.seekg(0, std::ios::end);
      end = myfile.tellg();
      size1 = end - begin;, size1);
      std::cout << "The complete file content in memory \n" << size1;
      //std::cout << "The complete file content is \n" << memblock;
      std::cout << "A line is \n" ;
      myfile.seekg(0, std::ios::beg);
      std::cout.write(memblock, size1);

      ::send(nSoc, (char*)memblock, size1, 0);
      delete[] memblock;
      return 1;
    std::cout << "Unable to open file\n";
    return 0;

Here is the code on the client side:

              ofstream out("test.txt", ios::out, ios::binary);

              char* mfcc;
              mfcc = new char[3381];

              while( recv1 < 3381)
                  int recv = ::recv(nsock, mfcc, 1499, 0);
                  std::cout << " The first line received:" << recv << "\n";
                  recv1 += recv;
                  std::cout << " The line received:" << recv1 << "\n";
                  std::cout << "Reading in data \n";
                  out.write(&mfcc, recv);

Here is the fragment from the binary file:

<80><F7><8C>^@<80><F7><8C>^@<D8>G;      <D8>G;  ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
Fixing the file sent to start with. Please let me know how to resolve this issue via improving my code fragment and comments.

Best Regards


Question by:sunnybrad
    LVL 86

    Accepted Solution

    Well, the problem is simple - you are moving the file pointer to the end of the file to obtain the size, and are forgetting to move it back at teh beginning of the file before calling 'read()' - thus the 'binary' garbage (actually, nothing was read) - try the following instead:
    begin = myfile.tellg();
          getline(myfile, line);
          memblock = new char[3381];
          myfile.seekg(0, std::ios::end);
          end = myfile.tellg();
          size1 = end - begin;
          myfile.seekg(0, std::ios::beg); // move back to the beginning!
          // *now* read
, size1);

    Open in new window


    Author Comment

    Hi Jkr:

    Thanks for your comment....did work by moving the cursor back to beginning .

    Best Regards,

    LVL 86

    Expert Comment

    You're welcome ;o)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [sā€¦
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more ā€¦
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now