Sub topic 9
Layer 7: Application, protocols that applications speak
This chapter assumes you know how Layers 1 to 4 move data between hosts, including IP addresses, ports, and the difference between TCP and UDP. Layer 7 sits at the top of the stack. It defines the rules that two programs use to talk to each other once the lower layers have delivered their bytes.
What Layer 7 does
An application-layer protocol defines the messages two programs exchange. It specifies the following:
- The message formats, such as a text request line or a binary header.
- The valid commands and replies.
- The order of the exchange, such as who speaks first.
- How errors are reported.
The protocol does not say how bytes cross the wire. It hands its messages to the layers below and relies on them to arrive.
The layer is not the application itself. A browser is a program. HTTP is the protocol the browser speaks. You can replace the browser with curl or a Python script, and the server cannot tell the difference as long as the messages follow the HTTP rules.
Common protocols
| Protocol | Purpose | Typical transport and port |
|---|---|---|
| HTTP | Web pages and APIs | TCP 80 |
| HTTPS | HTTP protected by TLS | TCP 443 |
| DNS | Names to IP addresses | UDP or TCP 53 |
| SMTP | Sending email between servers | TCP 25 or 587 |
| IMAP | Reading mail on a server | TCP 143 or 993 |
| SSH | Remote shell | TCP 22 |
| DHCP | Automatic IP configuration | UDP 67 and 68 |
Protocols choose their transport for a reason. DNS usually uses UDP because a lookup is one small question and one small answer. HTTP and SMTP use TCP because they need reliable, ordered delivery. The port number is a convention, not a law. A web server can listen on port 8080, and clients must then be told to use it.
A worked example: HTTP by hand
HTTP/1.1 is a text protocol, so you can speak it directly over a TCP socket. The script below opens a connection (Layer 4) and sends a request (Layer 7). It then prints the status line of the reply.
import socket
request = b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n"
with socket.create_connection(("example.com", 80), timeout=5) as s:
s.sendall(request)
data = s.recv(200)
print(data.decode("ascii", errors="replace").splitlines()[0])A typical result is HTTP/1.1 200 OK. The request has three parts:
- A request line with a method (
GET), a path (/), and a version. - Header lines such as
Host, which tells the server which site you want when many sites share one IP address. - A blank line (
\r\n\r\n) that marks the end of the headers.
The recv(200) call reads at most 200 bytes and may return fewer. This is fine for a status line. Real clients loop until the response is complete.
One exchange across the stack
Most application traffic starts with a name lookup. The client asks DNS for an address and then speaks HTTP to that address.
Both exchanges are Layer 7. Each one is carried by its own transport: DNS usually by UDP, and HTTP by TCP.
Design traits to recognize
- Request and response. HTTP, DNS, and SMTP all follow a pattern in which one side asks and the other answers.
- Stateless or stateful. HTTP treats each request independently, and cookies add state on top. SSH keeps a long-lived session.
- Text or binary. HTTP/1.1 and SMTP are readable text. HTTP/2 and DNS use compact binary encodings, so you need a tool such as Wireshark to read them.
Common errors
- Blaming the wrong layer. A
404 Not Foundor500response is itself proof that Layers 1 to 4 worked, because the server answered. Look at the application, not the cables. - Treating the port as the protocol. Port 80 usually carries HTTP, but nothing enforces that. Check the traffic, not just the number.
- Forgetting the
Hostheader. Without it, an HTTP/1.1 server hosting several sites may return an error or the wrong site. - Mixing up HTTP and HTTPS. Sending plain HTTP to port 443 fails because the server expects a TLS handshake first.
Exercise
A script connects to a web server on port 80 and receives "HTTP/1.1 403 Forbidden". Which layers can you rule out as the cause, and why?
Show answer
You can rule out Layers 1 to 4. The reply travelled over a working link, IP path, and TCP connection, and the server produced a valid HTTP message. The 403 is an application-layer decision, such as a permission rule on the server.