title: htons and ntohs not working? C UNIX networking
body: <p>I'm new to network programming in C and I started off by trying to send an array read from the keyboard to the server. The problem is that whenever I read the length of my array from the keyboard, it will add 2/3 and just not do anything right anymore. For example:</p>
  
  <p>Length or array that I give: 2
  The client will read from the keyboard 4 or 5 elements and if itry to print them afterwards, they don't have the value that I gave them. They get some totally different numbers.</p>
  
  <p>This is how my client looks like:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(int argc, char* argv[]) {
      int sockfd;
      struct sockaddr_in server;
      /*char msg[1000], serv_reply[2000];*/
  
  
      if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {
          perror("Error in creating the stupid sock client ");
          exit(errno);
      }
  
      puts("Socket created in client ");
      memset(&amp;server, 0, sizeof(server));
      server.sin_addr.s_addr = inet_addr("127.0.0.1");
      server.sin_family = AF_INET;
      server.sin_port = htons(9255);
  
      if ( connect(sockfd, (struct sockaddr*)&amp;server, sizeof(server)) &lt; 0) {
          perror("Connection failed ");
          exit(errno);
      }
  
      puts("Yey! Connected");
  
      printf("Let's try \n" );
      printf("Nr of elem = ");
      **int a;
      scanf("%d", &amp;a);
      a = htons(a);
      if ( send(sockfd, &amp;a, sizeof(a), 0 ) &lt; 0 ) {
          perror("CLient sent error");
          exit(errno);
      }
      int i, elem;
      for( i =0; i&lt;a; i++) {
          printf("Enter a number: ");
          scanf("%d", &amp;elem);
          elem = htons(elem);
          printf("%d", elem);
          if ( send(sockfd, &amp;elem, sizeof(elem), 0) &lt; 0) {
              perror("Couldn't send the damn element ");
              exit(errno);
          }
      }**
      int somth;
      recv(sockfd, &amp;somth, sizeof(somth), 0);
      somth = ntohs(somth);
      printf("WE GOT IT %d ", somth);
      close(sockfd);
  }
  </code></pre>
  
  <p>Here is my server:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;resolv.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define MY_PORT 9999
  #define MAXBUF 1024
  
  int main (int argc, char* argv[]) {
      int sockfd;
  struct sockaddr_in server, client;
  int c, l;
  
  if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {
      perror("Error creating the stupid socket ");
      exit(errno);
  }
  
  memset(&amp;server, 0, sizeof(server));
  server.sin_port = htons(9255);
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  
  if ( bind(sockfd, (struct sockaddr *) &amp;server, sizeof(server)) &lt; 0) {
      perror("Error in socket binding ");
      exit(errno);
  }
  
  if ( listen (sockfd, 20) &lt; 0) {
      perror("Erro listening to the dumb sock ");
      exit(errno);
  }
  printf("WE'RE ON!! " );
  l = sizeof(client);
  memset(&amp;client, 0, l);
  
  while(1) {
      int a;
  
  
      c = accept(sockfd, (struct sockaddr *) &amp;client, &amp;l);
          if (c == -1) {
              perror("Error accepting connection");
              continue;
          } else {     
              printf("Cool, we have a new client! \n");
          }
  
  
      if (recv( c, &amp;a, sizeof(a), MSG_WAITALL) &lt; 0) {
          perror("Error reading in server ");
          exit(errno);
      }
  
      a = ntohs(a);
  
      int i, elem,sum;
      for ( i = 0; i&lt;a; i++) {
          if ( recv(c, &amp;elem, sizeof(elem), MSG_WAITALL)&lt; 0) {
              perror("Error reading elems ");
              exit(errno);
          }
          elem = ntohs(elem);
          sum = sum + elem;
      }
      sum= ntohs(sum);
  
      if ( send(c, &amp;sum, sizeof(sum), 0) &lt; 0) {
          perror("Error sending to client ");
          exit(errno);
      }
  
      close(c);
  }
  }
  </code></pre>
  
  <p>I tried switching the htons to ntohs and the inverse and the same problem. Any idea what's wrong?</p>