title: Usage of htonl() and ntonhl() for unsigned char
body: <p>I am pasting my client and server code below. My program is running fine, except that i am trying to send an ipaddress in my src and dest field, and for some reason, even though i am sending it as 131.199.166.232, it is printing as 232.166.199.131. But rest of my packet values are printed in a proper way. I have used memcpy(), so I feel its a memcpy thing, which somewhere I did wrong, but in Beej's guide as there is a section @ the byte ordering in different computer architectures.....I have not used htonl() and all, so maybe it is because of that.Please guide me as where I am going wrong. Also kindly tell me the way i am sending data, how should the htonl() function can be used in my code....Thanks in advance.</p>
  
  <p>Client:   </p>
  
  <pre><code>       #include &lt;stdio.h&gt;
       #include &lt;stdlib.h&gt;
       #include &lt;string.h&gt;
       #include &lt;math.h&gt;
       #include &lt;sys/types.h&gt;
       #include &lt;sys/socket.h&gt;
       #include &lt;netinet/in.h&gt;
       #include &lt;netdb.h&gt;
       #define MAXPROFILES  2
  
      int main(int argc, char *argv[])
  
    {
      int sockfd, portno, n;
      struct sockaddr_in serv_addr;
      struct hostent *server;
      unsigned char buf[1024];
      unsigned int srcAddress = 2193598184;
      unsigned int destAddress = 2193598182;
  
     struct profile_t
   {
     unsigned char length;
     unsigned char type;
     unsigned char *data;
   };
  
     typedef struct profile_datagram_t
   {
  unsigned char src[4];
  unsigned char dst[4];
  unsigned char ver;
  unsigned char n;
  struct profile_t profiles[MAXPROFILES];
   } header;
  
  
      header outObj;
  
      int j =0;
      int i =0;
     // for loop for doing the malloc so that we can allocate memory to all profiles
      for(i=0;i&lt;MAXPROFILES;i++){
  outObj.profiles[i].data = malloc(5);
    }
  
  
      for(i=3;i&gt;=0;i--){
  outObj.src[i] = (srcAddress &gt;&gt; (i*8)) &amp; 0xFF;
  outObj.dst[i] = (destAddress &gt;&gt; (i*8)) &amp; 0xFF;
  printf("%d",outObj.src[i]);
   }
      outObj.ver = 1;
      outObj.n = 2;
  
      memcpy(buf,&amp;outObj.src,4);
      memcpy(buf+4,&amp;outObj.dst,4);
      memcpy(buf+8,&amp;outObj.ver,1);
      memcpy(buf+9,&amp;outObj.n,1);
  
  
      outObj.profiles[0].length = 5;
      outObj.profiles[0].type = 1;
      outObj.profiles[1].length = 5;
      outObj.profiles[1].type = 2;
  
      for(i=0;i&lt;MAXPROFILES;i++){
  for(j=0;j&lt;5;j++){
      outObj.profiles[i].data[j] = j+1;
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>    int k = 10;
  
          // for loop to do memcopy of length,type and data.
      for(i=0;i&lt;MAXPROFILES;i++){
  memcpy(buf+k,&amp;outObj.profiles[0].length,1);
  memcpy(buf+k+1,&amp;outObj.profiles[0].type,1);
  memcpy(buf+k+2,outObj.profiles[0].data,5);
  k +=7;
  </code></pre>
  
  <p>}</p>
  
  <pre><code>   if (argc &lt; 3) {
     fprintf(stderr,"usage: %s hostname port\n", argv[0]);
     exit(0);
  }
     portno = atoi(argv[2]); //Convert ASCII to integer
     sockfd = socket(AF_INET, SOCK_STREAM, 0); // socket file descriptor
  
  
     if (sockfd &lt; 0)
     error("ERROR DETECTED !!! Problem in opening socket\n");
  
     server = gethostbyname(argv[1]);
     if (server == NULL) {
     fprintf(stderr,"ERROR DETECTED !!!, no such server found \n");
      exit(0);
  }
  
    bzero((char *) &amp;serv_addr, sizeof(serv_addr)); //clear the memory for server address
  
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server-&gt;h_addr,
          (char *)&amp;serv_addr.sin_addr.s_addr,
       server-&gt;h_length);
  
    serv_addr.sin_port = htons(portno);
  
    printf("Client 1 trying to connect with server host %s on port %d\n", argv[1], portno);
  
  
    if (connect(sockfd,(struct sockaddr *)&amp;serv_addr,sizeof(serv_addr)) &lt; 0)
    error("ERROR in connection");
  
        printf("SUCCESS !!! Connection established \n");
  
  
     if (write(sockfd, buf, k) &lt; 0)
     {
    error("Write error has occured ");
     }
  
  
    return 0;
  </code></pre>
  
  <p>Server:      </p>
  
  <pre><code>          #include &lt;stdio.h&gt;
             #include &lt;stdlib.h&gt;
             #include &lt;string.h&gt;
             #include &lt;sys/types.h&gt;
             #include &lt;sys/socket.h&gt;
             #include &lt;netinet/in.h&gt;
             #define MAXPROFILES  2
  
             int main(int argc, char *argv[])
          {
             int sockfd, newsockfd, portno, clilen;
             struct sockaddr_in serv_addr, cli_addr;
             unsigned char buf[1024];
             int my_data2[10] = {1,3,9,10};
             int my_data[10] = {1,2,3,4,5};
         int myDataBinary[500] = {0};
         int myDataBinary2[500] = {0};
         int recData[500] = {0};
         int index1=0;
  
  
         struct profile_t
          {
          unsigned char length;
          unsigned char type;
          unsigned char *data;
          };
  
              typedef struct profile_datagram_t
         {
          unsigned char src[4];
          unsigned char dst[4];
          unsigned char ver;
          unsigned char n;
         struct profile_t profiles[MAXPROFILES];
          } header;
  
  
              header outObj;
  
          int j =0;
          int i =0;
  
  
  
              if (argc &lt; 2) {
              fprintf(stderr,"usage: %s port_number1",argv[0]);
              exit(1);
           }
              sockfd = socket(AF_INET, SOCK_STREAM, 0);
              if (sockfd &lt; 0)
              error("ERROR DETECTED !!! Problem in opening socket");
  
              bzero((char *) &amp;serv_addr, sizeof(serv_addr));
              portno = atoi(argv[1]);
  
              serv_addr.sin_family = AF_INET;
              serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
              serv_addr.sin_port = htons(portno);
  
              if (bind(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0)
              error("ERROR DETECTED !!! There was a problem in binding");
  
              listen(sockfd, 10);
              clilen = sizeof(cli_addr);
  
  
  
               printf("Server listening on port number %d...\n", serv_addr.sin_port);
  
               newsockfd = accept(sockfd,(struct sockaddr *) &amp;cli_addr, &amp;clilen);
  
               if (newsockfd &lt; 0)
           error("ERROR DETECTED !!! the connection request was not accepted");
  
           int rc = read(newsockfd,buf,100);
           if(rc &lt; 0){
       printf("error");
        }
           else {
          printf("success %d",rc);
        }
  
  
        memcpy(&amp;outObj.src,buf+0,4);
        memcpy(&amp;outObj.dst,buf+4,4);
        memcpy(&amp;outObj.ver,buf+8,1);
        memcpy(&amp;outObj.n,buf+9,1);
  
            printf("\nsrc ip = ");
            for(int i=0;i&lt;4;i++){
         printf("%d ",outObj.src[i]);
         }
  
            printf("\ndest ip = ");
            for(int i=0;i&lt;4;i++){
           printf("%d ",outObj.src[i]);
          }
  
           printf("\nversion = %d",outObj.ver);
           printf("\nnumber = %d",outObj.n);
  
           int k = 10;
  
           for(i=0;i&lt;outObj.n;i++){
       memcpy(&amp;outObj.profiles[i].length,buf+k,1);
       memcpy(&amp;outObj.profiles[i].type,buf+k+1,1);
       outObj.profiles[i].data = malloc(outObj.profiles[i].length);
       memcpy(outObj.profiles[i].data,buf+k+2,5);
          k +=7;
         }
  
           for(int i=0;i&lt;outObj.n;i++){
      printf("\nMessage %d :",i+1);
      printf("\nLength : %d",outObj.profiles[i].length);
      printf("\nType : %d",outObj.profiles[i].type);
      for(int j=0;j&lt;5;j++){
          printf("\ndata %d : %d",j,outObj.profiles[i].data[j]);
      }
  }
  
  
           for(int i=0; i&lt;sizeof(my_data)/sizeof(int);i++)
        {
       if(my_data[i] &gt; 0){
      index1 = my_data[i];
      myDataBinary[index1] = 1;
      printf("my data %d = %d\n",index1,myDataBinary[index1]);
  }
  }
  
         for(int i=0; i&lt;sizeof(my_data2)/sizeof(int);i++)
  {
      if(my_data2[i] &gt; 0){
          index1 = my_data2[i];
          myDataBinary2[index1] = 1;
          printf("my data %d = %d\n",index1,myDataBinary2[index1]);
      }
  }
  
  
           int sumRecievedData = 0;
           int sumMyData = 0;
           int sumMultpliedData = 0;
           float Cov =0;
           float sdMyData = 0;
           float sdRecievedData =0;
           int n = 500;
           float rho;
  
                  for(int i=0;i&lt;outObj.n;i++){
                index1=0;
                for (int j=0; j&lt;outObj.profiles[i].length;j++) {
  
                 if(outObj.profiles[i].data[j] &gt; 0){
                 index1 = outObj.profiles[i].data[j];
                 recData[index1] = 1;
                printf("rec data %d = %d\n",index1,recData[index1]);
         }
       }
     }
  
  
          return 0;
  
      }
  </code></pre>