title: C++ sending a packet over socket
body: <p>I'm trying to communicate with a server program I installed. The server sends and receives all data in the form of constructed packets that follow the setup of:
  int int int string nullbyte
  like this:</p>
  
  <p>little endian signed int -> The size of the ID (4 bytes) + size of Type (4 bytes) + size of Body (minimum 1 for null terminator) + null byte for a minimum of 10 as the value;</p>
  
  <p>little endian signed int -> id</p>
  
  <p>little endian signed int -> packet type</p>
  
  <p>Null terminiated ascii string -> body</p>
  
  <p>null byte</p>
  
  <p>I've managed to read the packets just fine but when I try to send the packet with the password, the server completely ignores it which means the packet is wrong some how. I construct the packet like this:</p>
  
  <pre><code>void Packet::build(){
  
  /*
   * Create unsigned char vector to store
   * the data while we build the byte array
   * and create a pointer so the byte array can
   * be modified by the required functions.
   */
  std::vector&lt;unsigned char&gt; packet(m_size);
  unsigned char *ptr = packet.data();
  
  /*
   * Convert each of the three integers as well
   * as the string into bytes which will be stored
   * back into the memory that ptr points to.
   *
   * Packet data follows format:
   * int32 -&gt; Size of ID + Server Data + body (minimum of 10).
   * int32 -&gt; ID of request. Used to match to response.
   * int32 -&gt; Server Data type. Identifies type of request.
   * String -&gt; Minimum of 1 byte for null terminator.
   * String -&gt; Null terminator.
   */
  storeInt32Le(ptr, m_sizeInPacket);
  storeInt32Le(ptr, m_requestID);
  storeInt32Le(ptr, m_serverData);
  storeStringNt(ptr, m_body);
  
  /*
   * Store the vector in member variable m_cmdBytes;
   *
   */
  m_cmdBytes = packet;
  }
  </code></pre>
  
  <p>storeInt32Le:</p>
  
  <pre><code>void Packet::storeInt32Le(unsigned char* &amp;buffer, int32_t value) {
  /*
   * Copy the integer to a byte array using
   * bitwise AND with mask to ensure the right
   * bits are copied to each segment then
   * increment the pointer by 4 for the next
   * iteration.
   */
  buffer[0] = value &amp; 0xFF;
  buffer[1] = (value &gt;&gt; 8) &amp; 0xFF;
  buffer[2] = (value &gt;&gt; 16) &amp; 0xFF;
  buffer[3] = (value &gt;&gt; 24) &amp; 0xFF;
  buffer += 4;
  }
  </code></pre>
  
  <p>storeStringNt:</p>
  
  <pre><code>void Packet::storeStringNt(unsigned char* &amp;buffer, const string &amp;s) {
  /*
   * Get the size of the string to be copied
   * then do a memcpy of string char array to
   * the buffer.
   */
  size_t size = s.size() + 1;
  memcpy(buffer, s.c_str(), size);
  buffer += size;
  </code></pre>
  
  <p>}</p>
  
  <p>And finally, I send it with:</p>
  
  <pre><code>bool Connection::sendCmd(Packet packet) {
  unsigned char *pBytes = packet.bytes().data();
  size_t size = packet.size();
  
  while (size &gt; 0){
      int val = send(m_socket, pBytes, size, 0);
  
      if (val &lt;= 0) {
          return false;
      }
  
      pBytes += val;
      size -= val;
  }
  
  return true;
  }
  </code></pre>
  
  <p>Packet::bytes() just returns m_cmdBytes</p>