I am new to socket programming and I am trying to understand the operation of htons()
. I've read a few tutorials on the Internet like this and this one for instance. But I couldn't understand what htons()
does exactly. I tried the following code:
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main( int argc, char *argv[] ) { int sockfd, newsockfd, portno, clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n; /* First call to socket() function */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } /* Initialize socket structure */ bzero((char *) &serv_addr, sizeof(serv_addr)); portno = 5001; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); /* Now bind the host address using bind() call.*/ if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR on binding"); exit(1); } /* Now start listening for the clients, here process will * go in sleep mode and will wait for the incoming connection */ listen(sockfd,5); clilen = sizeof(cli_addr); /* Accept actual connection from the client */ newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen); if (newsockfd < 0) { perror("ERROR on accept"); exit(1); } /* If connection is established then start communicating */ bzero(buffer,256); n = read( newsockfd,buffer,255 ); if (n < 0) { perror("ERROR reading from socket"); exit(1); } printf("Here is the message: %s\n",buffer); /* Write a response to the client */ n = write(newsockfd,"I got your message",18); if (n < 0) { perror("ERROR writing to socket"); exit(1); } return 0; }
The value of sin_port
was shown as 35091
while debugging and I don't understand how portno
changed from 5001
to 35091
. Could someone explain the reason for that change in value please?
htons is host-to-network short. This means it works on 16-bit short integers. i.e. 2 bytes. This function swaps the endianness of a short.
When you are about to send data from your host over the network, you prepare it using hton*. When you receive data fir your host from the network, you use ntoh*.
From the python socket reference: socket.htons(x) Convert 16-bit positive integers from host to network byte order. On machines where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 2-byte swap operation.
The htons() function converts the unsigned short integer hostshort from host byte order to network byte order. The ntohl() function converts the unsigned integer netlong from network byte order to host byte order.
It has to do with the order in which bytes are stored in memory. The decimal number 5001
is 0x1389
in hexadecimal, so the bytes involved are 0x13
and 0x89
. Many devices store numbers in little-endian format, meaning that the least significant byte comes first. So in this particular example it means that in memory the number 5001
will be stored as
0x89 0x13
The htons()
function makes sure that numbers are stored in memory in network byte order, which is with the most significant byte first. It will therefore swap the bytes making up the number so that in memory the bytes will be stored in the order
0x13 0x89
On a little-endian machine, the number with the swapped bytes is 0x8913
in hexadecimal, which is 35091
in decimal notation. Note that if you were working on a big-endian machine, the htons()
function would not need to do any swapping since the number would already be stored in the right way in memory.
The underlying reason for all this swapping has to do with the network protocols in use, which require the transmitted packets to use network byte order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With