|
Q & A on Network byte ordering Q: What is network byte ordering, and why is it needed? A: In most computer systems, the smallest addressable unit of memory is a byte (usually 8 bits). An integer occupies multiple bytes, with a "short" requiring 2 bytes and a "long" requiring 4. Unfortunately, computer vendors do not all agree on how to store an integer in multiple bytes. On some computers, the byte with the lowest address contains the most significant bits of the integer, while on other computer, the byte with the lowest address contains the least significant bits of the integer. The two forms are known as "big endian" and "little endian". Because a network or an internet can connect multiple brands of computers, a problem can arise when sending an integer value from one computer to the other. In particular, suppose a 4-byte integer is copied from the memory of a big-endian computer into packet, which is sent across a network to a little-endian computer, where it is copied into memory. Although the bytes are an exact copy of the original, the integer will have a different value on the second computer. To avoid integer misinterpretation, network protocols require each sender to convert all integers from the sender's internal form to a standard form used on the network, and require a receiver to convert from the form used on the network to the local form. The form used on the network is called "network byte order". Thus, if you look at an IP datagram traveling across the Internet, integer fields in the header all appear in the same byte order (i.e., the network byte order), no matter which byte order happens to be used on the computer that sent the datagram or the destination computer. |