Source: CS341 Simple TCP Client Example + HTTP
Tags: OSI model, seven layers, TCP, UDP, HTTP, transport layer, network protocols, connection-oriented, connectionless, UIUC, CS341
Difficulty: Introductory to Intermediate Prerequisites: Basic familiarity with how computers communicate over a network. No prior socket programming knowledge needed for this portion.
This material sits at the foundation of systems-level networking in CS341. Before you write any code that talks over a network, you need a mental model of how data travels from one machine to another, and that model is the OSI stack. The lecture then narrows in on the transport layer (Layer 4), where the two protocols you will use most often live: TCP and UDP. Finally, it introduces HTTP, the application-layer protocol built on top of TCP that powers the web. If you understand these three pieces, you have the conceptual scaffolding for every networking assignment that follows.
The OSI model breaks network communication into seven layers, each with a specific job. TCP is the reliable, connection-oriented transport protocol; UDP is the fast, connectionless alternative. HTTP is a text-based application-layer protocol that runs over TCP and is the basis for web communication.
OSI Model (Open Systems Interconnection Model)
A conceptual framework that divides network communication into seven ordered layers, from the physical transmission of bits up to application-level services. Think of it as a stack of responsibilities: each layer handles one concern and passes the result up or down.
Application Layer (Layer 7)
The topmost layer, providing high-level APIs such as resource sharing, remote file access, and directory services. In simple terms, this is where programs like your web browser or email client operate. Examples: HTTP, FTP, SMTP.
Presentation Layer (Layer 6)
Handles translation between a networking service and an application, including character encoding, data compression, and encryption/decryption. Think of it as the translator that makes sure both sides understand the same data format. Examples: ASCII, EBCDIC, JPEG.
Session Layer (Layer 5)
Manages communication sessions, meaning continuous exchanges of information in the form of multiple back-and-forth transmissions between two nodes. In simple terms, it keeps a conversation going. Examples: RPC, PAP.
Transport Layer (Layer 4)
Responsible for reliable transmission of data segments between points on a network, including segmentation, acknowledgement, and multiplexing. This is where TCP and UDP live. Examples: TCP, UDP.
Network Layer (Layer 3)
Handles structuring and managing a multi-node network, including addressing, routing, and traffic control. Think of it as the postal service that figures out the best route for your data. Data unit: packet/datagram. Examples: IPv4, IPv6, IPsec, AppleTalk.
Data Link Layer (Layer 2)
Provides reliable transmission of data frames between two nodes connected by a physical layer. In simple terms, it makes sure bits get from one directly connected device to another without corruption. Data unit: bit/frame. Examples: PPP, IEEE 802.2, L2TP.
Physical Layer (Layer 1)
Transmission and reception of raw bit streams over a physical medium. This is the actual cable, radio wave, or fibre-optic link. Data unit: bit. Examples: DSL, USB.
TCP (Transmission Control Protocol)
A connection-oriented transport protocol that provides reliable, ordered delivery of data between applications. It uses a three-way handshake to establish a connection, guarantees delivery through acknowledgements and retransmission, and requires more system resources than UDP. Think of it as a phone call: you set up the line, talk, confirm what was said, and hang up.
UDP (User Datagram Protocol)
A connectionless transport protocol that sends datagrams without establishing a connection first. There is no guarantee of delivery, ordering, or duplicate protection. Think of it as posting a letter: you send it and hope it arrives, but there is no confirmation.
HTTP (HyperText Transfer Protocol)
An application-layer protocol used for transmitting web pages and other resources. It runs over TCP (not UDP). Versions 1.0 and 1.1 are text-based (human-readable) protocols. In simple terms, HTTP is the language your browser speaks to a web server.
Handshaking
The initial exchange of control messages between two systems to establish the parameters of a connection before data transfer begins. TCP uses a three-way handshake (SYN, SYN-ACK, ACK). UDP does not handshake.
The OSI model is divided into two broad groups:
Host layers (Layers 7–4): Application, Presentation, Session, Transport. These deal with what the data means and how it is reliably moved.
Media layers (Layers 3–1): Network, Data Link, Physical. These deal with how data is routed and physically transmitted.
Each layer communicates only with the layer directly above and below it.
Data is encapsulated as it moves down the stack (headers added) and de-encapsulated as it moves up (headers stripped).
Connection type:
TCP is connection-oriented (establishes a session before sending data).
UDP is connectionless (fire and forget).
Reliability:
TCP guarantees delivery, ordering, and error checking.
UDP provides no delivery guarantee. Packets may arrive out of order, duplicated, or not at all.
Handshaking:
TCP uses handshaking (three-way handshake).
UDP does not.
System resources:
TCP requires more system resources due to connection state tracking, buffering, and retransmission logic.
UDP is lighter weight.
System calls:
TCP sockets can be used with read and write system calls (they behave like file descriptors).
UDP typically uses sendto and recvfrom.
Encryption:
Neither TCP nor UDP encrypts the data payload by itself. Encryption is handled by higher-layer protocols such as TLS.
Handling missing vs late packets:
If your application prefers to handle missing packets over late packets (e.g., live video, gaming, VoIP), use UDP. UDP will not stall waiting for a retransmission; it just moves on.
HTTP is a request/response protocol used for web communication.
It runs over TCP, not UDP, because web pages require reliable, ordered delivery.
HTTP 1.0 and HTTP 1.1 are text-based (human-readable) protocols. You can read their request and response headers as plain ASCII text.
HTTP/2, by contrast, is a binary protocol (not covered in this lecture, but worth knowing for context).
The OSI model is best memorised as a table:
Layer | Name | Data Unit | Key Examples |
|---|---|---|---|
7 | Application | Data | HTTP, FTP, SMTP |
6 | Presentation | Data | ASCII, EBCDIC, JPEG |
5 | Session | Data | RPC, PAP |
4 | Transport | Segments | TCP, UDP |
3 | Network | Packet/Datagram | IPv4, IPv6, IPsec |
2 | Data Link | Bit/Frame | PPP, IEEE 802.2, L2TP |
1 | Physical | Bit | DSL, USB |
Common mnemonic (top to bottom): "All People Seem To Need Data Processing."
The TCP vs UDP distinction shows up everywhere. Web browsing, email, and file transfers use TCP because you need every byte to arrive correctly. Video calls and online games often use UDP because a slightly glitchy frame is better than a frozen one waiting for a retransmission. DNS queries typically use UDP for speed, falling back to TCP only for large responses.
"UDP is unreliable, so nobody uses it." UDP is used heavily in real-time applications (gaming, video streaming, VoIP, DNS). "Unreliable" in networking means no delivery guarantee, not that it is broken.
"TCP encrypts data." TCP does not encrypt anything. Encryption is added by protocols like TLS that run on top of TCP. HTTPS = HTTP + TLS over TCP.
"The OSI model maps one-to-one to real protocols." The OSI model is a conceptual framework. Real protocol stacks (like TCP/IP) collapse or merge layers. The internet does not implement a strict seven-layer separation.
"HTTP can run over UDP." Standard HTTP/1.0 and 1.1 run over TCP. HTTP/3 (QUIC) runs over UDP, but that is a later development and a different design.
⚠️ Know which layer each protocol belongs to (TCP/UDP at Layer 4, HTTP at Layer 7).
⚠️ Be able to state which protocol (TCP or UDP) uses handshaking, requires more resources, and supports read/write system calls.
⚠️ Know that neither TCP nor UDP encrypts data on its own.
⚠️ Understand the trade-off: if your application prefers missing packets over late packets, choose UDP.
⚠️ HTTP 1.0 and 1.1 are text-based protocols. This is a commonly tested detail.
True or False: The transport layer is Layer 3 of the OSI model.
Fill in the blank: TCP uses a ______-way handshake to establish a connection.
True or False: UDP guarantees that packets arrive in order.
Fill in the blank: HTTP runs over ______, not UDP.
True or False: HTTP 1.1 is a binary protocol.
Answers: 1. False (Layer 4). 2. Three. 3. False. 4. TCP. 5. False (it is text-based).
Q: Name the seven layers of the OSI model from bottom to top.
A: Physical, Data Link, Network, Transport, Session, Presentation, Application.
Q: What are two key differences between TCP and UDP?
A: TCP is connection-oriented and guarantees reliable, ordered delivery; UDP is connectionless and does not guarantee delivery or ordering.
Q: Which transport protocol would you choose for a live video-streaming application, and why?
A: UDP, because the application would prefer to skip a missing packet (minor visual glitch) rather than stall playback waiting for a retransmission.
Q: Does HTTP 1.1 run over TCP or UDP? Is it a text or binary protocol?
A: HTTP 1.1 runs over TCP. It is a text-based protocol.
Q: Does TCP or UDP encrypt the data payload?
A: Neither. Encryption is provided by separate protocols such as TLS.
This material connects directly to socket programming (the next set of notes), where you will use the TCP concepts here to write a client in C. Understanding that TCP is a stream-oriented, connection-based protocol explains why you can use read and write on a TCP socket as though it were a file descriptor. The OSI model also provides context for later topics on routing (Layer 3) and application protocols beyond HTTP.
OSI model, seven layer model, TCP, UDP, HTTP, transport layer, network layer, application layer, connection-oriented, connectionless, three-way handshake, reliable delivery, unreliable delivery, segments, datagrams, packets, text protocol, binary protocol, CS341, UIUC, systems programming, networking fundamentals