title: C++ socket programming ( read() write() )
body: <p>I'm writing a program that transfers the contents of some files from client to server but I have some problems:</p>
  
  <ol>
  <li>The number of bytes that he client writes for server is not equal to the number of bytes that the server reads.</li>
  <li>Is my solution for sending the size of the files for server correct?</li>
  </ol>
  
  <h3>client.cpp</h3>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;cstdio&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sstream&gt;
  
  using namespace std ;
  
  const int BF_SIZE = 2048 ;
  
  void R_directory ( int socket_id ){
      DIR *dir ;
      struct dirent *pent ;
      dir = opendir( "address of files" ) ;
      string path = "address of files" ;
  
      char BF_read[BF_SIZE] ;
      int n ;
  
      if ( dir == NULL ){
          cout &lt;&lt; "No such directory" &lt;&lt; endl ;
      }
      else{
          while (  pent = readdir ( dir ) ){
              if ( pent == NULL ) break ;
              if( !strcmp( pent-&gt;d_name , ".") || !strcmp ( pent-&gt;d_name , "..") ) continue ;
              string abspath = path , a ;
              for ( int i = 0 ; i &lt; pent-&gt;d_name[i] ; i++ ) abspath.push_back( pent-&gt;d_name[i] ) ;
              FILE *fi = NULL ;
              fi = fopen ( abspath.c_str() , "r" ) ;
              struct stat fi_sp ;
              stat( abspath.c_str() , &amp;fi_sp );
              if ( fi == NULL ){
                  cout &lt;&lt; "Can't open file " &lt;&lt; abspath &lt;&lt; endl ;
                  continue ;
              }
              if ( fi_sp.st_size == 0 ){
                  cout &lt;&lt; "File " &lt;&lt; abspath &lt;&lt; " is empty" &lt;&lt; endl ;
                  continue ;
              }
              int file_size = 0 ;
              file_size = fi_sp.st_size ;
              write ( socket_id , &amp;file_size , 4 ) ;
              cout &lt;&lt; abspath &lt;&lt; " " &lt;&lt; file_size &lt;&lt; endl ;
              while ( ( n = fread ( BF_read , 1 , BF_SIZE , fi ) ) &gt; 0 &amp;&amp; file_size &gt; 0){
                  cout &lt;&lt; n &lt;&lt; endl ;
                  file_size -= n ;
                  if ( file_size &gt; 0 &amp;&amp; n &gt;= BF_SIZE ){
                      write ( socket_id , BF_read , sizeof ( BF_read ) ) ;
                  }
                  else{
                      write ( socket_id , BF_read , n - 1 ) ;
                  }
              }
  
              cout &lt;&lt; "=====================================" &lt;&lt; endl ;
              fclose(fi);
          }
          int file_size = 0 ;
          write ( socket_id , &amp;file_size , 4 ) ;
      }
      closedir(dir);
      return ;
  }
  
  int main ( int argc , char* argv[] ){
      int sockfd = 0,n = 0;
      struct sockaddr_in serv_addr;
  
      if((sockfd = socket(AF_INET, SOCK_STREAM, 0))&lt; 0)
      {
          cout &lt;&lt; "Error : Could not create socket" &lt;&lt; endl ;
          return 1;
      }
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_port = htons(5000);
      serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  
      if(connect(sockfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr))&lt;0)
      {
          cout &lt;&lt; "Error : Connect Failed" &lt;&lt; endl ;
          return 1;
      }
  
      R_directory( sockfd );
  
      return 0 ;
  }
  </code></pre>
  
  <h3>server.cpp</h3>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std ;
  
  const int BF_SIZE = 2048 ;
  
  int main(void)
  {
      int listenfd = 0,connfd = 0;
  
      struct sockaddr_in serv_addr;
  
      char sendBuff[BF_SIZE] , recvBuff[BF_SIZE] ;
      int numrv;
  
      listenfd = socket(AF_INET, SOCK_STREAM, 0);
      printf("socket retrieve success\n");
  
      memset(&amp;serv_addr, '0', sizeof(serv_addr));
      memset(sendBuff, '0', sizeof(sendBuff));
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
      serv_addr.sin_port = htons(5000);
  
      bind(listenfd, (struct sockaddr*)&amp;serv_addr,sizeof(serv_addr));
  
      if(listen(listenfd, 10) == -1){
          printf("Failed to listen\n");
          return -1;
      }
  
      connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
      ofstream cout ( "output.txt" , std::ios::app ) ;
  
      while(1)
      {    
          int file_size = 0 , n ;
          n = read ( connfd , &amp;file_size , 4 ) ;
          //cout &lt;&lt; "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" &lt;&lt; endl ;
          if ( file_size == 0 ) break ;
          while ( file_size &gt; 0 ){
              n = read( connfd , recvBuff, sizeof(recvBuff) ) ;
              cout &lt;&lt; n &lt;&lt; endl ;
              if ( n == 0 ) break ;
              file_size -= n ;
              /*for ( int i = 0 ; i &lt; n ; i++ ){
                  cout &lt;&lt; recvBuff[i] ;
              }*/
          }
          cout &lt;&lt; endl ;
      }
      close(connfd);
  
      return 0;
  }
  </code></pre>
  
  <p>In client side, the number of bytes that I send is equal to file size in bytes but in server I got more bytes — why?</p>