title: Sending ints and chars through sockets
body: <p>I'm writing code for an application using sockets where I have to send integers and strings over the network, but I'm having trouble packing the data into a buffer for transmission. I tried doing this: </p>
  
  <pre><code>sendline[0] = htons(3);
  sendline[1] = htons(strlen(argv[3]));
  for(int i = 0; i &lt; strlen(argv[3]); i++)
  {
      sendline[i + 2] = argv[3][i];
  }
  sendline[2 + strlen(argv[3])] = htons(atoi(argv[4]));
  sendline[3 + strlen(argv[3])] = '\0';
  </code></pre>
  
  <p>but it doesn't work.
  Where am I going wrong here? Also, what would be the best way to serialize this kind of data?</p>
  
  <p>This is my deserialization code :</p>
  
  <pre><code>case '3': // switching on the value of buf[0]
  {
      int i;
      int len = ntohs(buf[1]);
      char* ch = (char*)malloc(len * sizeof(char));
      for(i = 0; i &lt; len; i++)
      {
          ch[i] = buf[i + 2];
      }
  }
  </code></pre>