title: Transfer array of integers over a socket in C/C++
body: <p><a href="https://stackoverflow.com/questions/9140409/transfer-integer-over-a-socket-in-c">This</a> answer shows methods for sending and receiving integers over sockets in c/c++. Based on that I wrote following two functions, sendKInt and receiveKInt to send and receive K integers. But its not working correctly. Maybe my pointer arithmetic is wrong, but I cannot find the error. Please help with that, I am new to c++. Also see sendTill and receiveTill functions.</p>
  
  <pre><code>bool sendKInt(int fd, int num[], int k){ // returns true on success
      int32_t conv[k];
      char *data;
      for (int i=0; i&lt;k; i++){
          conv[i] = htonl(num[i]);
          char *data = (char*)conv;
      }
      return sendTill(sizeof(int32_t)*k, fd, data);
  }
  
  bool receiveKInt(int fd, int *num, int k){ // returns true on success
      int32_t ret[k];
      int toReturn = receiveTill(sizeof(int32_t)*k,fd,(char*)ret);
      for (int i=0; i&lt;k; i++){
          num[i] = ntohl(ret[i]);
      }
      return toReturn;
  }
  
  bool sendTill(int size, int fd, char* dataU){ // dataU - dataUser pointer is NULL then create your own buffer and send random thing // returns true on successs
      char *data;
      int left = size;
      int rc;
      if(dataU == NULL){
          data = (char*)malloc(size);
      }else{
          data = dataU;
      }
      while (left) {
          rc = write(fd, data + size - left, left);
          if (rc &lt; 0){
              cout &lt;&lt;"-1 in sendTill \n";
              return false;
          }
          left -= rc;
      }
      return true;
  }
  
  bool receiveTill(int size, int fd, char* data){ // just give a char pointer after allocating size // returns true on success
      int ret;
      int left = size;
      while (left) {
          ret = read(fd, data + size - left, left);
          if (ret &lt; 0){
              cout &lt;&lt;"-1 in receiveTill \n";
              return false;
          }
          left -= ret;
      }
      return true;
  }
  </code></pre>