Socket Foundations, Byte Order, and Encoding
Sockets
A socket exposes network-protocol services through a programming interface, allowing processes to communicate without implementing the entire protocol stack themselves. Programming languages wrap the underlying mechanisms in accessible APIs.
- TCP maintains connection state, establishes a connection through a handshake, and supports reliable delivery. Connection management has costs.
- UDP sends datagrams without TCP's connection handshake or teardown. It does not guarantee that every datagram arrives; suitable addressing and options also allow broadcast use.
A TCP handshake establishes a connection, not every send on that connection. Reliability also depends on sequence numbers, acknowledgments, and retransmission. Performance depends on workload and environment; TCP is not universally slower. Ordinary UDP unicast does not automatically broadcast to multiple receivers.
Endianness
Endianness determines the byte order of a multibyte value in memory or on a wire. Little endian places the least significant byte at the lowest address; big endian places the most significant byte there. Consider 0x12345678 and 0x11223344:
Memory addresses increase from left to right.
| Order | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Little endian | 78 | 56 | 34 | 12 | 44 | 33 | 22 | 11 |
| Big endian | 12 | 34 | 56 | 78 | 11 | 22 | 33 | 44 |
0x12345678 · 0x11223344
CPU designs do not all use the same order. Network byte order is big endian, as specified for Internet numeric fields in RFC 1700. Programs convert applicable host-order values to network order for sending and back for receiving, independently of CPU and operating system.
htons() and htonl() convert host to network order; ntohs() and ntohl() convert network to host order.
This convention applies to multibyte numeric fields defined by a protocol, not to reversing arbitrary payload text. Python's struct.pack("!I", value) encodes an unsigned 32-bit integer in network order; struct.unpack decodes it. ! explicitly selects network order rather than the host's memory layout.
Send a word the receiver can interpret correctly
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
Text and Bytes
Python str contains text, while sockets transmit bytes. text.encode("utf-8") produces bytes; data.decode("utf-8") recovers text from a complete encoding. Character count need not equal byte count: “网络” has two characters but six UTF-8 bytes.
A UTF-8 character can span several bytes. A TCP receive need not end at a character or message boundary. Accumulate data according to framing rules or use an incremental decoder rather than assuming every received fragment is complete text.
KNOWLEDGE QUIZ
Encode an integer and count text bytes
Use Python's struct to encode 0x12345678 as an unsigned 32-bit network-order integer, print the hex bytes, and decode it back. Then print the character and UTF-8 byte counts for '网络'. Expected lines: 12345678; 305419896; 2 6. Do not rely on your CPU's native byte order.