byte order
Write the number one thousand two hundred and thirty-four as 1234, and everyone reads it the same. But computers store multi-byte numbers as a sequence of bytes, and there is a genuine disagreement about which byte to put first. Byte order, or endianness, is the rule a machine uses to lay out the bytes of a number in memory: big-endian puts the most significant byte first (the 'big end'), little-endian puts the least significant byte first.
Take the 16-bit number 0x1234. A big-endian machine stores it as the bytes 0x12, 0x34; a little-endian machine stores it as 0x34, 0x12 — same value, reversed bytes. This matters the instant two computers exchange binary numbers, because if a little-endian sender's 0x34, 0x12 is read by a big-endian receiver as-is, it becomes 0x3412, a completely wrong number. To avoid chaos, Internet protocols agreed on one convention called network byte order, which is big-endian. So before putting a port number or length field on the wire, you convert from host order to network order, and you convert back on receipt.
Why it matters: this is a real, common bug in network and binary-format code, and the socket API gives you helper functions for exactly it: htons (host-to-network short, for 16-bit values like ports), htonl (host-to-network long, 32-bit), and the inverses ntohs and ntohl. You use htons on a port number before passing it to bind or connect. On a little-endian machine (most x86 and ARM today) these functions swap the bytes; on a big-endian machine they do nothing — which is exactly why you should always call them rather than assume, so the same code is correct everywhere. Note that text protocols like HTTP sidestep this by sending numbers as human-readable digits, where byte order does not apply.
To bind to port 8080, you write addr.sin_port = htons(8080). On a little-endian laptop htons flips the two bytes so 8080 goes on the wire big-endian; on a big-endian box it leaves them alone. Either way the receiver, using ntohs, reads 8080 — not a scrambled value.
Always convert with htons/htonl — never assume your machine's order.
Forgetting htons on a port is a classic bug that often hides on big-endian-free setups: the code seems fine on one machine and breaks on another. Calling the conversion functions makes the same source correct on every architecture.