Sub topic 5
Starting point: application data
This chapter covers the data that sits at the top of the stack before any network header is added. It assumes you know what an IP address is, can read bytes in hexadecimal, and can run commands in a terminal.
What application data is
Application data is the message a program wants to deliver. The network layers below treat it as opaque bytes. An HTTP request, a DNS query, an SMTP command and a chunk of a video stream are all application data.
Three properties matter for the rest of this tutorial:
- The application defines the format. For HTTP/1.1 the format is plain text with lines ending in
\r\n. - The lower layers do not parse it. TCP, IP and Ethernet never look inside it.
- It is just bytes. The text you see is one way to interpret those bytes.
In the TCP/IP model this is the application layer. The OSI model splits the same job into application, presentation and session layers. For tracing a packet, the TCP/IP view is enough.
A worked example: an HTTP request
The Python snippet below builds the smallest useful HTTP request. It is the same payload that the full encapsulation example in this tutorial wraps.
http = b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
print(len(http))
print(http.hex(" "))
print(http.decode())The request is 37 bytes long. It has three parts:
- The request line
GET / HTTP/1.1names the method, the path and the protocol version. - The header line
Host: example.comsays which site the client wants. - An empty line (
\r\n\r\nat the end) marks the end of the headers.
The hex output begins 47 45 54 20, which are the ASCII codes for GET . Every later layer will put its own bytes in front of these. The HTTP bytes themselves do not change.
Where the data comes from
A program does not build packets itself. It hands bytes to the operating system through a socket, which is the programming interface to the network stack.
import socket
with socket.create_connection(("example.com", 80)) as s:
s.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")
reply = s.recv(200)
print(reply.decode(errors="replace"))create_connection resolves the name, opens a TCP connection and returns a socket. sendall passes your bytes to the kernel. The kernel decides how to cut them into segments, which headers to add and when to send. This version adds Connection: close so the server ends the connection after replying. The program works only with the bytes and never sees a segment, a packet or a frame.
Common errors at this layer
- Confusing the message with the transmission. A 37-byte request may travel as one segment. A 5 MB upload becomes thousands of segments. The application message size and the packet size are unrelated.
- Assuming message boundaries survive TCP. TCP delivers a stream of bytes. Two
sendallcalls can arrive in onerecv, and one call can arrive in several. The application protocol must mark where a message ends, as HTTP does with the blank line and withContent-Length. - Forgetting the line endings. HTTP requires
\r\n. A bare\ncan make some servers wait or reject the request. - Expecting encryption to be invisible. With HTTPS, the application data is TLS records instead of readable text. A capture shows encrypted bytes, and the layers below still work the same way.
What comes next
The next chapter moves down one layer. The transport layer takes these bytes, adds a port-based header and hands the result to IP.
Exercise
A program calls sendall twice over a TCP connection, first with 100 bytes and then with 50 bytes. The receiver calls recv once and gets 150 bytes. Is this a bug in the network? What does it show about application data?
Show answer
It is not a bug. TCP carries a byte stream and does not preserve the boundaries between sends. The receiver's application protocol must define where one message ends, for example with a length field or a delimiter such as the blank line in HTTP.
Exercise
How many bytes is the request b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n", and which bytes does a later layer add to it?
Show answer
It is 37 bytes: "GET / HTTP/1.1" is 14 bytes, "\r\n" is 2, "Host: example.com" is 17, and the final "\r\n\r\n" is 4. Later layers do not change these bytes. They add their own headers in front, TCP then IP then Ethernet, and the network card appends a trailer.