Skip to content

The Transport and Application Layers

The transport layer is layer 4 in the TCP/IP model. Its job is to take data from the application layer above and deliver it to the right application on the right host - reliably (TCP) or as-fast-as-possible (UDP).

Two core responsibilities:

  • Multiplexing - combining data streams from multiple sockets into one transmission channel, tagging each with a port number so the receiver knows where to send it.
  • Demultiplexing - at the receiving end, reading that port number and routing each segment to the correct application socket.

A port is a 16-bit number (0-65535) that identifies which application on a host should receive incoming traffic. IP addresses get you to the right machine; ports get you to the right program.

Port RangeCategoryDescription
1 - 1023System / Well-knownReserved for standard services (HTTP, SSH, DNS). Binding requires root/admin.
1024 - 49151User / RegisteredVendor-registered ports. Not as strict.
49152 - 65535Ephemeral / DynamicOS-assigned temporary ports for outgoing client connections.
PortProtocolService
20 / 21TCPFTP (data / control)
22TCPSSH
23TCPTelnet (unencrypted - avoid)
25TCPSMTP (email sending)
53TCP/UDPDNS
67 / 68UDPDHCP (server / client)
80TCPHTTP
110TCPPOP3
143TCPIMAP
443TCPHTTPS (TLS)
3306TCPMySQL
3389TCPRDP (Remote Desktop)
5432TCPPostgreSQL
6443TCPKubernetes API server
8080TCPHTTP alternate / dev servers

Transmission Control Protocol (TCP) is a connection-oriented, reliable transport protocol. Before any data moves, the two sides agree to communicate. Along the way, every segment is acknowledged. Lost segments are retransmitted.

TCP Segment Structure

A TCP segment has a mandatory header followed by the data payload. The header contains 10 mandatory fields:

FieldSizePurpose
Source Port16 bitsSending application’s port
Destination Port16 bitsReceiving application’s port
Sequence Number32 bitsByte offset in the data stream - used for ordering
Acknowledgment Number32 bitsNext expected byte from the other side
Header Length (Data Offset)4 bitsHeader size in 32-bit words (tells OS where data starts)
Control Flags6-8 bitsURG, ACK, PSH, RST, SYN, FIN (+ ECE, CWR for ECN)
Window Size16 bitsHow many bytes the receiver can accept before needing an ACK (flow control)
Checksum16 bitsIntegrity check over header + data + IP pseudoheader
Urgent Pointer16 bitsOnly relevant when URG flag is set. Rarely used in practice.
Options0-40 bytesExtensions: MSS, Window Scaling, SACK, Timestamps

Key TCP Options:

  • MSS (Maximum Segment Size) - negotiated at connection setup; the largest chunk of data TCP will send in one segment
  • Window Scaling - allows the 16-bit window field to be multiplied (necessary for high-bandwidth long-distance links)
  • SACK (Selective Acknowledgement) - receiver tells sender exactly which out-of-order segments were received, avoiding retransmitting already-delivered data
  • Timestamps - used for RTT measurement and PAWS (Protection Against Wrapped Sequence numbers)

Six flags you’ll see constantly:

FlagWhat it does
SYNInitiates a connection. Synchronizes sequence numbers.
ACKAcknowledges received data. Set on almost every segment after the first SYN.
FINBegins graceful connection teardown. “I’m done sending.”
RSTAbruptly terminates a connection. No graceful close.
PSHTell the receiver: flush this to the application now, don’t buffer.
URGUrgent data present (pointer field valid). Almost never used in practice.

The Three-Way Handshake

TCP requires a handshake before any application data flows. The goal: both sides synchronize sequence numbers and confirm they can reach each other.

Client Server
| |
|------ SYN (seq=1000) -------->| Client picks ISN (Initial Sequence Number)
| |
|<-- SYN-ACK (seq=5000, | Server picks its own ISN, ACKs client's SYN
| ack=1001) --------|
| |
|------ ACK (ack=5001) -------->| Client ACKs server's SYN
| |
|====== Data flows now =========|

After the handshake, the connection is full-duplex - both sides can send simultaneously, and every segment gets an ACK.

Four-Way Teardown

Closing is also graceful - each side independently signals it’s done sending:

Client Server
| |
|-------- FIN ----------------->| "I'm done sending"
|<-------- ACK -----------------| "Got it"
| | (Server may still send data here)
|<-------- FIN -----------------| "I'm also done sending"
|--------- ACK ---------------->| "Got it"
| |
|===== Connection closed =======|

A socket is an instantiated endpoint - a port that a program has actually opened and is listening on. You can send to any port, but only get a response if a socket is open there.

TCP connections move through a state machine:

StateWhoMeaning
CLOSEDBothNo connection exists
LISTENServer onlyWaiting for incoming SYN
SYN-SENTClient onlySent SYN, waiting for SYN-ACK
SYN-RECEIVEDServer onlyGot SYN, sent SYN-ACK, waiting for ACK
ESTABLISHEDBothConnection open, data flows
StateMeaning
FIN-WAIT-1Sent FIN, waiting for ACK
FIN-WAIT-2Got ACK for our FIN, waiting for remote FIN
CLOSE-WAITReceived FIN, notified application to close
LAST-ACKSent our FIN, waiting for final ACK
TIME-WAITBoth sides closed; lingering to catch any late packets (lasts 2x MSL)

TCP vs UDP

PropertyTCPUDP
ConnectionYes (3-way handshake)No
ReliabilityGuaranteed delivery + orderingBest-effort
OverheadHigher (headers, ACKs, retransmits)Lower
SpeedSlower (wait for ACKs)Faster
Flow controlYes (window size)No
Use casesHTTP, SSH, FTP, databasesDNS, video streaming, VoIP, gaming

A firewall blocks or permits network traffic based on rules. Most commonly deployed at the transport layer, making decisions based on ports and connection state.

Firewall

TypeWorks atHow it decidesExample
Packet filterNetwork layer (L3)Source/dest IP + port + protocol per-packetRouter ACLs
Stateful inspectionTransport layer (L4)Tracks connection state; allows return traffic automaticallyiptables conntrack, pfSense
Application layer (proxy)Application layer (L7)Inspects content - HTTP headers, DNS payloadsWAF, squid proxy
Next-generation (NGFW)L3-L7All of above + IPS, app identification, user identityPalo Alto, FortiGate

Stateful vs Stateless

Common Linux firewall tools:

Terminal window
# iptables - classic stateful firewall (older but ubiquitous)
# Allow established/related return traffic
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow SSH inbound
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# nftables - modern replacement for iptables
# List all rules
sudo nft list ruleset
# ufw - user-friendly frontend for iptables (Ubuntu/Debian)
sudo ufw allow 22/tcp
sudo ufw allow 443/tcp
sudo ufw status verbose
# firewalld - zone-based firewall (RHEL/CentOS/Fedora)
sudo firewall-cmd --list-all
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload

Firewalls also appear at other layers now: cloud security groups (AWS/GCP/Azure), Kubernetes NetworkPolicies, and container-level rules - same concept, different control plane.


The application layer (Layer 5 in TCP/IP) is where actual applications exchange data. It sits on top of transport - it doesn’t care how the data got there, just whether it arrived.

Application Layer

Key principle: interoperability through protocol standardization. Chrome and Firefox both speak HTTP. Apache and Nginx both speak HTTP. Any browser can talk to any web server because they all implement the same HTTP spec. The same logic applies to FTP clients, SMTP mail servers, SSH clients vs servers.

TCP segments carry the full application-layer payload in their data section.

Application Layer and OSI Model

The TCP/IP model’s application layer (layer 5) maps to three layers in the 7-layer OSI model:

OSI LayerNameWhat it doesTCP/IP equivalent
7ApplicationUser-facing protocols: HTTP, FTP, SMTP, DNSApplication layer
6PresentationEncryption, encoding, compression (SSL/TLS lives here conceptually)Application layer
5SessionManaging sessions, checkpoints, reconnectsApplication layer / TCP

Terminal window
# Test TCP connectivity to a specific port (the "is this port open?" check)
nc -zv example.com 443 # Linux/Mac - verbose, zero i/o mode
# -z = scan only, don't send data
# -v = verbose output
# Exit code 0 = success, non-zero = failed
# Windows equivalent
Test-NetConnection -ComputerName example.com -Port 443
# Quick port scan of common ports on a host (use only on hosts you own)
nc -zv 192.168.1.1 20-25 80 443 8080
# Test UDP connectivity (harder - nc sends a probe, may or may not get response)
nc -zuv 8.8.8.8 53
# Trace a TCP connection - see where it drops
traceroute -T -p 443 example.com # TCP traceroute (bypasses ICMP blocks)
sudo tcptraceroute example.com 443 # alternative tool
# Watch live TCP connections
watch -n1 'ss -tan | grep ESTABLISHED'