[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

Sening file via Socket receiving binary for text data

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;
  //  myfile.open(szFilePath, ios::binary);

  myfile.open("testmail.txt");
  std::cout << "The filename : \n" << szFilePath;

  if(myfile.is_open())
    {
      begin = myfile.tellg();
      getline(myfile, line);
      memblock = new char[3381];
      myfile.seekg(0, std::ios::end);
      end = myfile.tellg();
      size1 = end - begin;
      myfile.read(memblock, size1);
      myfile.close();
     
      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;
    }
  else{
    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);
                }
             
              out.close();

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

-Sunnybrad

0
sunnybrad
Asked:
sunnybrad
  • 2
1 Solution
 
jkrCommented:
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
      myfile.read(memblock, size1);
      myfile.close();

Open in new window

0
 
sunnybradAuthor Commented:
Hi Jkr:

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

Best Regards,

-Sunnybrad
0
 
jkrCommented:
You're welcome ;o)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now