title: Understanding htonl() and ntohl()
body: <p>I am trying to use unix sockets to test sending some udp packets to localhost.</p>
  
  <p>It is my understanding that when setting ip address and port in order to send packets, I would fill my <code>sockaddr_in</code>with values converted to network-byte order.
  I am on OSX and I'm astonished that this</p>
  
  <pre><code>printf("ntohl: %d\n", ntohl(4711));
  printf("htonl: %d\n", htonl(4711));
  printf("plain: %d\n", 4711);
  </code></pre>
  
  <p>Prints</p>
  
  <pre><code>ntohl: 1729232896
  htonl: 1729232896
  plain: 4711
  </code></pre>
  
  <p>So  neither function actually returns the plain value. I would have expected to see either the results differ, as x86 is little-endian (afaik), or be identical and the same as the actual number 4711. Clearly I do not understand what <code>htonl</code> and <code>ntohl</code> and their variants do. What am I missing?</p>
  
  <p>The relevant code is this:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
     if (argc != 4)
     {
        fprintf(stderr, "%s\n", HELP);
        exit(-1);
     }
  
     in_addr_t rec_addr = inet_addr(argv[1]); // first arg is '127.0.0.1'
     in_port_t rec_port = atoi(argv[2]);      // second arg is port number
     printf("Address is %s\nPort is %d\n", argv[1], rec_port);
     char* inpath = argv[3];
  
     char* file_buf;
     unsigned long file_size = readFile(inpath, &amp;file_buf); // I am trying to send a file
     if (file_size &gt; 0)
     {
        struct sockaddr_in dest;
        dest.sin_family      = AF_INET;
        dest.sin_addr.s_addr = rec_addr; // here I would use htons
        dest.sin_port        = rec_port;
        printf("ntohs: %d\n", ntohl(4711));
        printf("htons: %d\n", htonl(4711));
        printf("plain: %d\n", 4711);
        int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
        if (socket_fd != -1)
        {
           int error;
           error = sendto(socket_fd, file_buf, file_size + 1, 0, (struct sockaddr*)&amp;dest, sizeof(dest));
           if (error == -1)
              fprintf(stderr, "%s\n", strerror(errno));
           else printf("Sent %d bytes.\n", error);
        }
     }
  
     free(file_buf);
     return 0;
  }
  </code></pre>