Sub topic 9
Layer 7: Application, Protocols That Applications Speak
What this chapter assumes
You should know the layer order from earlier chapters, how encapsulation wraps data on the way down the stack, and that Layer 4 delivers data to a port on a host. You should also be comfortable running Python and shell commands. This chapter covers the top of the stack, where network communication becomes meaningful to a program or a person.
What Layer 7 does
Layer 7 is the Application layer. It defines the rules that two programs follow when they exchange messages for a specific purpose, such as fetching a web page, looking up a name, or delivering email. Those rules are called an application protocol. An application protocol specifies:
- Which messages exist, for example
GET,HELO, or a DNS query. - How each message is formatted, as text lines or as binary fields.
- Who speaks first and what replies are valid.
- What error conditions are reported and how.
The lower layers move bytes between hosts and between ports. They do not know what the bytes mean. Layer 7 is the first layer where the bytes have meaning.
Application versus application-layer protocol
The name "Application layer" misleads many people. A web browser is not at Layer 7. The browser is a program that uses HTTP, and HTTP is the Layer 7 protocol. The same distinction applies to other pairs:
| Program (not a layer) | Layer 7 protocol it speaks |
|---|---|
| Firefox, curl, a mobile app calling an API | HTTP or HTTPS |
| Thunderbird, Outlook | SMTP to send, IMAP or POP3 to read |
| OpenSSH client | SSH |
| Your OS resolver | DNS |
| A laptop joining Wi-Fi | DHCP |
One program can speak many protocols, and one protocol can be spoken by many programs. This is why curl, a browser, and a Python script can all talk to the same web server. They all follow HTTP.
Common Layer 7 protocols
The table lists well-known protocols with the Layer 4 transport and default port each one uses. Treat the ports as conventions, not laws.
| Protocol | Purpose | Transport | Default port |
|---|---|---|---|
| HTTP | Web requests and APIs | TCP | 80 |
| HTTPS | HTTP protected by TLS | TCP | 443 |
| DNS | Name to address lookup | UDP, TCP for large replies | 53 |
| SMTP | Sending email between servers | TCP | 25, 587 |
| IMAP | Reading mail on a server | TCP | 143, 993 (TLS) |
| SSH | Remote shell and file transfer | TCP | 22 |
| FTP | File transfer | TCP | 21 (control) |
| DHCP | Automatic IP configuration | UDP | 67 and 68 |
| NTP | Clock synchronization | UDP | 123 |
| SNMP | Device monitoring | UDP | 161 |
Notice that the protocols pick their transport to fit the job. DNS and NTP are short exchanges where the cost of a TCP handshake is not worth it, so they use UDP. Email and file transfer must not lose data, so they use TCP.
How application protocols are built
Text and binary formats
Many older protocols, including HTTP/1.1, SMTP, and FTP, are text based. A human can read the messages and even type them into a terminal. Text protocols are easy to debug but wasteful in size and slower to parse.
Other protocols are binary. DNS packs fields into fixed-width bit positions. HTTP/2 and HTTP/3 also use binary framing. Binary formats are compact and fast to parse, but you need a tool such as Wireshark to read them.
Request and response
Most application protocols follow a client-server request and response pattern. The client opens the conversation and the server replies. Some protocols differ:
- SMTP and FTP begin with the server sending a greeting.
- Protocols such as WebSocket, MQTT, and SSH allow either side to send data at any time after setup.
State
HTTP is stateless by design. Each request stands alone, and the server does not remember earlier requests unless the application adds something like a cookie. FTP and SMTP are stateful. The server tracks where the conversation is, for example "sender already given, waiting for recipient." Statefulness affects how you scale and load balance a service, because requests in a stateful dialogue must reach the same server.
Worked example: speaking HTTP by hand
Because HTTP/1.1 is plain text, you can write a client with a raw socket. This makes Layer 7 visible: your program writes the HTTP message, and the operating system handles TCP, IP, and everything below.
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])The output is a status line such as HTTP/1.1 200 OK. The pieces map onto the layers like this:
create_connectionasks the OS for a TCP connection (Layer 4). The OS also resolvesexample.comto an IP address with DNS (a Layer 7 protocol used by the OS on your behalf).requestis the Layer 7 message. Its parts are the request line (GET / HTTP/1.1), a header (Host), another header (Connection: close), and a blank line that ends the headers. HTTP requires each line to end in\r\n.recv(200)reads up to 200 bytes of the reply. One call may return fewer bytes than the server sent, and TCP gives no message boundaries. The HTTP protocol, not TCP, tells you where a response ends, throughContent-Length, chunked encoding, or connection close.
The Host header is required in HTTP/1.1. One IP address often serves many sites, and the server uses Host to choose the right one. If you remove it, most servers answer 400 Bad Request.
You can watch the same exchange with curl:
curl -v http://example.com/ 2>&1 | head -n 20Lines starting with > are what the client sent. Lines starting with < are what the server replied.
The exchange over time
Two separate Layer 7 protocols appear in one page load. DNS finds the address, and HTTP fetches the content. Each rides on its own Layer 4 transport.
What Layer 7 does not do
Layer 7 hands its message to the layer below and trusts the stack to deliver it. A pure Layer 7 protocol does not:
- Choose a route. That is Layer 3.
- Retransmit lost segments. TCP at Layer 4 does that. Some application protocols over UDP, such as DNS clients and NTP clients, add their own retry logic because UDP provides none.
- Identify a physical device. MAC addresses belong to Layer 2.
In practice, protocols often blur the layer boundaries. HTTPS performs encryption, which the OSI model assigns to Layer 6. TLS is usually described as sitting between Layer 4 and Layer 7. A later chapter on the TCP/IP model explains why the practical stack merges Layers 5 to 7 into one.
Layer 7 and the network devices
Most switches and routers ignore Layer 7. Devices that read it are:
- Reverse proxies and L7 load balancers. They read the HTTP
Hostheader or URL path and send/apito one pool of servers and/imagesto another. An L4 load balancer sees only IP addresses and ports and cannot do this. - Web application firewalls (WAFs). They inspect request contents for attacks such as SQL injection.
- Caching proxies and CDNs. They store responses and serve repeated requests without contacting the origin.
- DNS resolvers. They are Layer 7 services themselves.
Inspecting Layer 7 costs more CPU than forwarding by IP and port. It also requires the device to see plaintext, so a device that inspects HTTPS must terminate TLS and hold the certificate.
Common errors and gotchas
A "network problem" that is really a Layer 7 problem
If a page fails to load, the cause is often at Layer 7 even though the lower layers work. A ping that succeeds proves only that Layer 3 works. Typical Layer 7 failures include:
| Symptom | Layer 7 cause |
|---|---|
| `NXDOMAIN` or "could not resolve host" | The DNS name does not exist, or the resolver is wrong |
| HTTP `404 Not Found` | The server works, but the path is wrong |
| HTTP `500` or `502` | The application or an upstream server failed |
| SMTP `550` | The mail server rejected the recipient |
| TLS certificate error | The name does not match the certificate, or the certificate expired |
An HTTP status code in the reply is good news for diagnosis. It means the TCP connection succeeded, so Layers 1 to 4 are working.
Ports are conventions
Port 80 is only the default for HTTP. A server can run HTTP on 8080, and SSH on 2222. Firewalls that filter by port number alone can be bypassed, and a port number does not prove which protocol is running. To identify a protocol reliably, inspect the traffic.
One recv call is not one message
The socket example above reads at most 200 bytes. A real client must loop until the protocol says the message is complete. Treating each recv as a whole message is a classic bug. It works on small replies in testing and breaks on large ones.
Mixing up HTTP versions and transports
HTTP/1.1 and HTTP/2 use TCP. HTTP/3 uses QUIC, which runs over UDP. The statement "HTTP is a TCP protocol" is therefore not always true. The application semantics (methods, status codes, headers) stay the same, while the transport underneath changes.
Line endings and whitespace
Text protocols are strict. Sending \n instead of \r\n to an SMTP or HTTP server may be accepted by lenient servers and rejected by strict ones. If a hand-written request hangs, check that you ended the headers with a blank line (\r\n\r\n).
Troubleshooting from Layer 7
When lower layers pass their checks, move to Layer 7 tools:
dig example.com +short # DNS: does the name resolve?
curl -sS -o /dev/null -w "%{http_code}\n" https://example.com
# HTTP: what status does the server return?
openssl s_client -connect example.com:443 -servername example.com </dev/null
# TLS: is the certificate valid for this name?Read the result from the bottom of the stack upward. If dig fails, fix DNS before looking at HTTP. If curl returns a status code, the connection works and the remaining problem lies in the application's behavior.
Summary of the working model
- Layer 7 defines the language two programs use. It is not the program itself.
- Different protocols choose TCP or UDP according to their needs.
- Messages are text or binary, and the protocol itself defines where one message ends.
- A reply at Layer 7, even an error, proves the layers below are functioning.
- Devices that read Layer 7 give more control, for example routing by URL, at the cost of more processing and the need to see plaintext.
Exercise
A user reports that a website does not load. From your laptop, ping example.com succeeds, nc -zv example.com 443 reports a successful connection, and curl -I https://example.com prints HTTP/2 503. Which layers can you rule out as the cause, where does the fault most likely lie, and why does the status code help?
Show answer
The successful ping shows Layer 3 works, and DNS resolution worked because the name resolved. The successful nc test shows a TCP connection to port 443 can be made, so Layer 4 and below are working. The 503 status is a Layer 7 reply from the server, which proves the HTTP exchange reached a server and the TLS handshake completed. The fault most likely lies in the application or an upstream service behind the server, which is reporting itself as unavailable. The status code helps because the server cannot send it unless every lower layer delivered the request, so you should investigate the server or application rather than the network path.
Exercise
In the raw socket example, why does the request include the line Host: example.com, and why can a single call to recv(200) not be trusted to return a complete HTTP response?
Show answer
HTTP/1.1 requires the Host header because one IP address and port can serve many websites, and the server uses the header to select the correct site. TCP is a byte stream with no message boundaries, so recv returns whatever bytes have arrived, up to the limit. The end of an HTTP response is defined by the protocol, through Content-Length, chunked encoding, or the server closing the connection. A correct client must keep reading until one of those signals the end.