Network Programming Fundamentals
Network programming is the technology that enables programs on different hosts to communicate with each other over a network. Understanding the underlying TCP/IP protocol and the Socket abstraction layer is the foundation for writing network programs.
Architecture Patterns
- B/S (Browser/Server): The client is a browser that communicates with the server over HTTP/HTTPS. No client installation is required, but functionality is limited by the browser.
- C/S (Client/Server): A dedicated client communicates with the server. Protocols can be customized for richer functionality, but a client must be installed.
The OSI Seven-Layer Model
The OSI (Open Systems Interconnection) reference model divides network communication into seven abstract layers, each with a clearly defined responsibility:
| Layer | Name | Protocol / Function Examples | Data Unit |
|---|---|---|---|
| 7 | Application | HTTP, FTP, SMTP, DNS | Data |
| 6 | Presentation | Encryption, compression, encoding conversion | Data |
| 5 | Session | Session establishment and management | Data |
| 4 | Transport | TCP, UDP | Segment |
| 3 | Network | IP, ARP, ICMP | Packet |
| 2 | Data Link | Ethernet, MAC address | Frame |
| 1 | Physical | Cables, fiber optics, wireless signals | Bit |
- When sending, data is encapsulated top-down through each layer (headers are added at each layer).
- When receiving, data is de-encapsulated bottom-up through each layer.
The TCP/IP Protocol Suite
The actual Internet uses the TCP/IP protocol suite (a four-layer model):
| TCP/IP Layer | Corresponding OSI Layers | Key Protocols |
|---|---|---|
| Application | Layers 5–7 | HTTP, HTTPS, FTP, DNS, SMTP |
| Transport | Layer 4 | TCP, UDP |
| Network | Layer 3 | IP, ARP, ICMP |
| Network Interface | Layers 1–2 | Ethernet, Wi-Fi |
IP Addresses and Ports
- IP Address: A 32-bit binary number (IPv4) that identifies the location of a host on the network. Format:
192.168.1.100.127.0.0.1: The loopback address, used for local self-testing.0.0.0.0: Listen on all network interfaces.255.255.255.255: Broadcast address.
- Port Number: 0–65535, identifies a specific process or service on a host.
- Well-known ports (0–1023): HTTP 80, HTTPS 443, SSH 22, FTP 21, SMTP 25.
- Dynamic ports (49152–65535): Randomly assigned by the client.
TCP vs UDP
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented (three-way handshake) | Connectionless |
| Reliability | Guaranteed order, no loss | Not guaranteed |
| Speed | Slower (acknowledgement mechanism) | Faster |
| Use Cases | Web, file transfer, email | Video streaming, DNS, gaming |
TCP Three-Way Handshake and Four-Way Termination
Three-Way Handshake (Establishing a Connection)
Client Server
| ---SYN(seq=x)---> | Step 1: Client sends SYN, enters SYN_SENT
| <--SYN+ACK(seq=y,ack=x+1)-- | Step 2: Server sends SYN+ACK, enters SYN_RECV
| ---ACK(ack=y+1)---> | Step 3: Client sends ACK, both enter ESTABLISHEDWhy three steps? Two handshakes cannot confirm the client’s receive capability; three handshakes allow both sides to verify that communication is working.
Four-Way Termination (Closing a Connection)
Active Closer Passive Closer
| ---FIN---> | Step 1: Sends FIN, enters FIN_WAIT_1
| <--ACK--- | Step 2: Replies ACK, enters CLOSE_WAIT
| <--FIN--- | Step 3: Sends FIN, enters LAST_ACK
| ---ACK---> | Step 4: Replies ACK, waits 2MSL then closesWhy four steps? TCP is full-duplex; closing each direction requires its own FIN+ACK exchange.
The Socket Abstraction Layer
A Socket is the network programming interface provided by the operating system, hiding the low-level TCP/IP details from applications.
AF_INET: IPv4 network socket (most common)AF_INET6: IPv6 network socketAF_UNIX: Unix domain socket, used for inter-process communication on the same machineSOCK_STREAM: TCP socket (connection-oriented, reliable)SOCK_DGRAM: UDP socket (connectionless, fast)
Quick Port Reference
import socket
# Look up port by service name
print(socket.getservbyname("http")) # 80
print(socket.getservbyname("https")) # 443
print(socket.getservbyname("ssh")) # 22
# Look up service name by port
print(socket.getservbyport(80)) # "http"
# Get local machine IP
print(socket.gethostbyname(socket.gethostname()))The next section, Socket Programming, introduces how to implement TCP/UDP communication using Python’s socket module.