The OSI Model and Application-Layer Protocols
The OSI model divides network communication into seven layers. Each layer has a defined responsibility and a set of protocols. This post maps the key protocols to their layers and default ports.
The Seven Layers
The OSI (Open Systems Interconnection) model is a conceptual framework that standardises network communication into seven layers. Each layer provides services to the layer above and uses services from the layer below.
| Layer | Name | Primary function |
|---|---|---|
| 7 | Application | End-user protocols (HTTP, SMTP, DNS) |
| 6 | Presentation | Encoding, encryption, compression |
| 5 | Session | Session management, synchronisation |
| 4 | Transport | End-to-end delivery, flow control (TCP, UDP) |
| 3 | Network | Logical addressing, routing (IP) |
| 2 | Data Link | Hop-to-hop delivery, MAC addressing |
| 1 | Physical | Bit transmission over physical medium |
In practice, TCP/IP collapses layers 5, 6, 7 into a single Application layer, and layers 1, 2 into a Network Access layer.
Protocol Reference
Key protocols by layer and default port:
| Protocol | Layer | Port | Purpose |
|---|---|---|---|
| HTTP | 7 | 80 | Web traffic |
| HTTPS | 7 | 443 | Encrypted web traffic |
| SMTP | 7 | 25 | Email sending |
| IMAP | 7 | 143 | Email retrieval (keeps on server) |
| POP3 | 7 | 110 | Email retrieval (downloads) |
| DNS | 7 | 53 | Domain name resolution |
| FTP | 7 | 21 | File transfer |
| SSH | 7 | 22 | Secure remote shell |
| RDP | 7 | 3389 | Remote desktop |
| LDAP | 7 | 389 | Directory services |
| SNMP | 7 | 161 | Network device management |
| XMPP | 7 | 5222 | Messaging |
| MQTT | 7 | 1883 | IoT messaging (pub/sub) |
| CoAP | 7 | 5683 | Constrained IoT devices |
| SMB | 7 | 445 | File sharing (Windows) |
| NFS | 7 | 2049 | Network file system (Unix) |
| TCP | 4 | N/A | Reliable stream transport |
| UDP | 4 | N/A | Unreliable datagram transport |
Socket Programming Concepts
Sockets are the API through which applications access the transport layer. A socket represents one endpoint of a two-way communication link.
Two types:
- Stream socket (TCP): reliable, ordered, connection-oriented byte stream.
- Datagram socket (UDP): unreliable, unordered, connectionless packets.
The socket lifecycle:
- Creation:
socket()creates a socket of the specified type. - Binding and addressing:
bind()associates the socket with a local address and port. - Listening and accepting (server):
listen()andaccept()wait for incoming connections. - Connecting (client):
connect()initiates a connection to a remote address. - Transmission:
send()/recv()orwrite()/read(). - Closing:
close()releases the socket.
Sockets can be blocking (calls wait until data is available) or non-blocking (calls return immediately, requiring polling or event-driven handling). Socket options allow configuration of behaviour such as socket reuse (SO_REUSEADDR) and timeouts.
Client-Server Architecture
The client-server model separates two roles:
- Server: listens for incoming connections, processes requests, sends responses. Examples: web server (Apache, Nginx, IIS), database server, file server.
- Client: initiates connections, sends requests, receives responses. Examples: browsers, mobile apps, CLI tools.
Key concerns in a client-server system: protocol choice (HTTP vs gRPC vs WebSocket), security (TLS, authentication), performance (load balancing, caching), and deployment (GCP, AWS, bare metal).