title: Sending files via socket cpp
body: <p>Hi I'm trying to send files via sockets from a server to a client and then open them on the client. My Problem is that I can send pictures and documents without a problem, but when I try to send a video the data that reaches its destination is corrupt. The destinationfile has the same size as the sourcefile.</p>
  
  <p>Server </p>
  
  <pre><code>int Server::data_trans_send(Client *client, const char *filename, const char *loc){
      int re;
      //client object stores socket ID
  #ifdef _WIN32
      SOCKET dest = (client-&gt;socketfd());
  #else
      int dest = (client-&gt;socketfd());
  #endif
      string sloc;
      int resp = 0;
      sloc.append (loc);
      sloc.append (filename);
      //opens a file in binary
      FilePrep file(sloc.c_str ());
      long int filesize = file.filesize();
      //reads binary data into a char array
      char *data = file.preptdata();
      //sends the filesize
      re = send(dest, &amp;filesize, sizeof(long int), 0);
      if(re &lt; 0){return -1;}
      re = 0;
      re = recv (dest, &amp;resp, sizeof(int), 0);
      if(re &lt; 0){return -2;}
      re = 0;
      //sends the filename
      re = write(dest, filename, strlen(filename));
      if(re &lt; 0){return -3;}
      re = 0;
      re = recv (dest, &amp;resp, sizeof(int), 0);
      if(re &lt; 0){return -4;}
      re = 0;
      //sends the filedata
      re = write(dest, data, filesize);
      if(re &lt; 0){return -5;}
      re = 0;
      re = recv (dest, &amp;resp, sizeof(int), 0);
      if(re &lt; 0){return -6;}
      re = 0;
      return 1;
  }
  </code></pre>
  
  <p>Client</p>
  
  <pre><code>int Client::data_trans_recv(const char *loc){
      //server socket ID is stored in the private section of class
      long int filesize = 0;
      int re;
      int resp = 1;
      char *filename = new char[64];
      //recieves the filesize 
      re = recv(*sockfd, &amp;filesize, sizeof(long int), 0);
      if(re &lt; 0){return -1;}
      re = 0;
      re = send(*sockfd, &amp;resp, sizeof(int), 0);
      if(re &lt; 0){return -2;}
      re = 0;
      //recieves the filename 
      re = read(*sockfd, filename, 64);
      if(re &lt; 0){return -3;}
      re = 0;
      re = send(*sockfd, &amp;resp, sizeof(int), 0);
      if(re &lt; 0){return -4;}
      re = 0;
      save_filename (filename);
      //creates an empty file and opens it in binary
      BuildData file(filesize);
      //returns ptr to array to write data to file
      char *dest = file.fileptr();
      //recieves the filedata 
      re = read(*sockfd, dest, filesize);
      if(re &lt; 0){return -5;}
      re = 0;
      re = send(*sockfd, &amp;resp, sizeof(int), 0);
      if(re &lt; 0){return -6;}
      re = 0;
      string floc;
      floc.append (loc);
      floc.append (filename);
      //writes array to file
      file.writeData(floc.c_str (), dest);
      delete filename;
      return 1;
  }
  </code></pre>
  
  <p>The Send and recv with the 1 integer is just to synchronize both functions.</p>
  
  <p>Build Data</p>
  
  <pre><code>#include "BuildData.h"
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  BuildData::BuildData(long int file_size) {
      data = new char[file_size];
      filesize = file_size;
  }
  
  BuildData::~BuildData() {
      delete data;
      file.close();
  }
  
  int BuildData::writeData(const char *loc, char *bits){
      file.open(loc, ios::out | ios::binary | ios::trunc);
      if(file.is_open()){
          file.write(bits, filesize);
          return 1;
      }
      return -1;
  }
  
  char *BuildData::fileptr(){return data;}
  </code></pre>