Actually, I understand that the network order and the order of the host is different. I wonder when you need a short and why use it? (In the head "neponyatka").
4 answers
In IPv4, the host address is 4 bytes in size; htonl () and int variables must be used for it (on 32-bit machines, you can long. 16-bit ones, for simplicity, forget it).
The port address in IP is 2 bytes in size, respectively htons () and short.
Host and port addresses are transmitted over the network and their bits must be ordered in a uniform manner.
short (2 bytes) int (on some platforms 4 bytes) long (8 bytes) are data types (size). Why these data types? Well, programmers tend to use different types of data in their programs. :)
- I understand. But do I use short now for storing the network address? - Jakeroid
- oneLook at these functions not as a means for working with addresses, but as useful functions for working with data. This can be useful when you need to transfer data via a socket between platforms where the order of bytes is different. And then you don’t have to mess up the exchange protocol, you don’t need to hard-thread some checks in the code, but simply transfer the data to the network order when sending data to the socket, and put the host information into order. For numbers with a floating point, such a focus will not work. :) - Hedgehog
Type is not size. This is a meaning.
short and long is a relic of BSD. In general, I advise you to read Stevens, so that there are less "misunderstandings" And yes, short and long are not byte order.
- This is not a byte order; it is a type for storing bytes. Right? - Jakeroid