When you want to bind to port in C you have to use htons(port)
to convert the port from host byte order to network byte order. This happens because the port number is copied directly to the TCP packets, so they have to match on little-endian and big-endian machines.
Consider the following example in C:
int port = 5000;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
Notice the use of htons
. Now, if you run netstat -anp --tcp
(on Linux) you'll see that 0.0.0.0:5000
is being listened. It looks like the port number uses host endianness.
Now, a question arises: if port number is host-endian in URL, does this mean that a big-endian client can't use http://a.b.c.d:5000
URL to connect to a little-endian server listening on 0.0.0.0:5000
?
No, you're almost certainly misapplying the rules at the wrong abstraction level.
The local browser, in all likelihood, will grab the :5000
string off the end of the URL and use that to create the integer 5000
in host format. It will then pass that to htons
as part of constructing the session, in exactly the same way your code snippet does.
And, voila, the structures contain the correct network order.