Skip to content
Documentation Background

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.

Transport sits on top of IP, which delivers datagrams host to host on a best-effort basis. IP can drop, duplicate, delay, or reorder packets and promises nothing about it. The transport layer extends delivery from host to host to process to process. UDP adds only ports and an optional error check. TCP builds ordering, reliability, flow control, and congestion control on top of IP’s unreliable service. Neither can promise bandwidth or delay, because IP does not.

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

One host runs many sockets, so each arriving segment needs a rule that selects its socket. UDP and TCP use different keys.

UDPTCP
Socket identified byDestination IP, destination portSource IP, source port, destination IP, destination port
Two clients, same server portBoth land in the same socketEach gets its own connected socket
StateNonePer-connection state

With UDP, the source port is only a return address. The server reads it from each datagram and uses it as the destination of the reply.

With TCP, a server listens on one port and accept() produces a new connected socket for every client. Thousands of connections can share port 443 because each has a different four-tuple.

Terminal window
ss -tn state established '( sport = :443 )'
# Recv-Q Send-Q Local Address:Port Peer Address:Port
# 0 0 10.0.1.20:443 198.51.100.7:51514
# 0 0 10.0.1.20:443 198.51.100.7:51520
# 0 0 10.0.1.20:443 203.0.113.41:40122

The four-tuple also sets a ceiling. A client that connects to one server IP and port can use only as many source ports as its ephemeral range allows, about 28,000 on Linux by default. A proxy or NAT gateway funnelling many clients to a single upstream address can run out of source ports long before it runs out of CPU. Widen the range with net.ipv4.ip_local_port_range or spread connections over more source or destination addresses. See Kubernetes Services for the same effect behind a cluster NAT.


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)

IP can lose packets, corrupt them, duplicate them, and deliver them out of order. A reliable transport hides all of that with five tools:

  • Checksum - detects corrupted data
  • Acknowledgments (ACKs) - the receiver confirms what it got
  • Sequence numbers - let the receiver spot duplicates and gaps
  • Timer - the sender waits a bounded time for an ACK, then assumes loss
  • Retransmission - the sender resends unacknowledged data

The simplest design is stop-and-wait: send one packet, wait for its ACK, send the next. It is correct but wasteful. The sender is busy for only a tiny fraction of each round trip:

utilization = (L / R) / (RTT + L / R)
Example: 10 Gbit/s link, 40 ms round trip, 1500-byte (12,000-bit) packets
L / R = 1.2 microseconds
utilization = 0.0012 ms / 40.0012 ms = about 0.003%
throughput = 12,000 bits per 40 ms = about 300 kbit/s on a 10 Gbit/s link

The fix is pipelining: keep many packets in flight before the first ACK returns. That needs a larger sequence number space, a sender buffer for unacknowledged data, and sometimes a receiver buffer. How much data must be in flight to keep the link full is the bandwidth-delay product:

BDP = link rate x round-trip time
= 10 Gbit/s x 40 ms = 400 Mbit = 50 MB in flight

Two designs for recovering from loss in a pipelined protocol:

Go-Back-NSelective Repeat
AcknowledgmentsCumulative: “everything up to N arrived”Individual: one per packet
TimersOne, for the oldest unacknowledged packetOne per packet
On a lossResend the lost packet and everything after itResend only the lost packet
ReceiverDiscards out-of-order packets, so needs no bufferBuffers out-of-order packets until the gap fills
CostWasted retransmissions when loss is frequentMore state at both ends

TCP is a hybrid of the two. Like Go-Back-N it uses cumulative ACKs and one timer. Like Selective Repeat it buffers out-of-order data and retransmits only the missing segment. The SACK option moves it closer to Selective Repeat by reporting exactly which ranges arrived.

Going deeper: Reliable delivery protocols rebuilds the mechanism step by step, with Go-Back-N and Selective Repeat traces and the window vs sequence space rule.


TCP numbers bytes, not packets. The sequence number in a segment is the stream position of its first byte. Sending a 10,000-byte response with a 1,460-byte MSS produces segments at offsets 0, 1460, 2920, and so on. The acknowledgment number is the next byte the receiver expects, and it is cumulative: ACK 4380 means every byte before 4380 has arrived.

Received bytes 0 to 2919, then 4380 to 5839 (2920 to 4379 missing)
Receiver replies ACK 2920 to every new segment until the gap is filled

Each side picks a random initial sequence number during the handshake, so delayed segments from an old connection are unlikely to be accepted by a new one with the same addresses and ports. Tools show relative numbers by default (starting at 0); use tcpdump -S to see the real ones. In an interactive session, ACKs ride along on data going the other way, which is called piggybacking.

Round-trip time and the retransmission timeout

Section titled “Round-trip time and the retransmission timeout”

The sender needs a timeout that is longer than a typical round trip but short enough to recover quickly. A fixed value fails on any path whose delay changes, so TCP measures the RTT continuously and smooths it:

EstimatedRTT = 0.875 x EstimatedRTT + 0.125 x SampleRTT
DevRTT = 0.75 x DevRTT + 0.25 x |SampleRTT - EstimatedRTT|
RTO = EstimatedRTT + 4 x DevRTT

DevRTT measures how much the samples vary, so the timeout widens on jittery paths. Segments that were retransmitted are not used as samples, because the sender cannot tell which transmission an ACK belongs to (the timestamp option removes that ambiguity). After a timeout, TCP doubles the RTO on each further failure (exponential backoff), which also reduces load on a congested path. Linux starts with a 1 second RTO and never lets it fall below 200 ms.

Terminal window
ss -ti dst 203.0.113.41
# cubic wscale:7,7 rto:204 rtt:23.5/4.2 mss:1448 cwnd:10 retrans:0/3
# ^^^^^^^ RTO ^^^^^^^^^ average RTT / variation

A lost segment is detected in one of two ways.

  • Timeout - no ACK arrives before the RTO fires. This is slow and is treated as severe congestion.
  • Fast retransmit - the receiver sends a duplicate ACK for every out-of-order segment. Three duplicate ACKs tell the sender that the next segment was lost and not merely delayed, so it resends immediately without waiting for the timer.

Wireshark labels both cases for you, and the kernel counts them:

Terminal window
# Wireshark display filters
# tcp.analysis.retransmission
# tcp.analysis.fast_retransmission
# tcp.analysis.duplicate_ack
nstat -az TcpRetransSegs TcpExtTCPFastRetrans

Flow control protects the receiver. Each side has a receive buffer, and every ACK advertises how much free space remains, called the receive window (rwnd). The sender keeps its unacknowledged data below that figure.

rwnd = RcvBuffer - (LastByteReceived - LastByteRead)
bytes in flight <= min(rwnd, cwnd)

When an application reads slowly, the buffer fills and rwnd drops to 0. The sender stops and sends tiny probe segments until the window reopens. In Wireshark this shows up as TCP ZeroWindow and TCP Window Full, and it points at the receiving application, not the network.

The 16-bit window field caps rwnd at 64 KiB, which is far below the BDP of a fast long-distance path. The window scaling option multiplies it, and Linux tunes buffer sizes automatically within the limits in net.ipv4.tcp_rmem and net.ipv4.tcp_wmem.

Flow controlCongestion control
ProtectsThe receiving hostThe network between the hosts
Limitrwnd, advertised by the receivercwnd, calculated by the sender
SignalFree buffer space in each ACKLoss, delay, or ECN marks

See TCP congestion control for the second row.

A host that receives a segment for a port with no listening socket answers with a segment that has the RST flag set. The client sees this as “connection refused” immediately, which makes a refused connection different from a firewall that silently drops packets and leaves the client timing out. (UDP has no connection to reset. A closed UDP port produces an ICMP port unreachable message instead.)

The handshake is also an attack surface. The server allocates state when it receives a SYN, before the client proves it exists. In a SYN flood, an attacker sends many SYNs and never completes the handshake, filling the table of half-open connections until real clients are refused. SYN cookies defend against it: the server encodes the connection details into its initial sequence number, keeps no state, and creates the socket only if the final ACK returns a matching value.

Terminal window
sysctl net.ipv4.tcp_syncookies
# net.ipv4.tcp_syncookies = 1
# Half-open connections piling up on a listener
ss -tn state syn-recv | wc -l

TCP Fast Open (net.ipv4.tcp_fastopen) lets a returning client put data in its SYN, saving a round trip, at the cost of the same replay caveats as TLS 0-RTT.

SymptomLikely causeFix
Throughput far below link speed on a long pathWindow smaller than the BDP (scaling off or small buffers)Confirm wscale in ss -ti, raise the tcp_rmem and tcp_wmem maximums
Many retransmissions on one pathLoss from congestion or a faulty linkRun mtr, check interface errors with ethtool -S
Connection refused at onceRST: nothing is listening, or a firewall rejectsss -tlnp on the server, then the firewall rules
Connection hangs, then times outSYN silently droppedtcpdump shows SYNs leaving with no reply, so check firewalls and security groups
Transfer stalls in burstsReceiver advertising a zero windowFix the slow reader, then check receive buffer size
Many connections in SYN-RECVSYN flood, or accept backlog fullKeep tcp_syncookies on, raise the listen backlog

UDP adds almost nothing to IP. The header is four 16-bit fields, 8 bytes in total.

FieldPurpose
Source portReturn address for replies
Destination portSelects the receiving socket
LengthHeader plus data, in bytes
ChecksumDetects corruption

The checksum is the 16-bit one’s complement sum of the data and a pseudo-header containing the IP addresses. The receiver sums everything including the checksum field and expects all ones, otherwise the datagram is discarded. UDP detects errors but never recovers from them. It is optional in IPv4 (a value of 0 means unused) and mandatory in IPv6.

A transport checksum looks redundant when links already run a CRC, but not every link checks, and a router can corrupt a packet in memory after the link check passes. Only the endpoints can verify the data end to end.


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.

A firewall decides on headers and connection state. An intrusion detection system (IDS) and an intrusion prevention system (IPS) go further and inspect the payload, a technique called deep packet inspection.

FirewallIDSIPS
Looks atHeaders and connection stateHeaders and payloadHeaders and payload
PositionInlineOut of band, on a mirror port or tapInline
On a matchAllow or deny by ruleRaise an alert onlyDrop the packet and alert
Failure impactTraffic stops if it fails closedNone, traffic keeps flowingTraffic stops or bypasses inspection, depending on fail mode

Detection works in two ways. Signature-based engines compare traffic against a database of known attack patterns, which is precise but blind to new attacks until a signature ships. Anomaly-based engines learn a baseline and flag deviations, which can catch new attacks but produce more false positives.

Two limits matter in practice. Encrypted traffic hides the payload, so inspection needs TLS termination at the device, or it falls back to headers and metadata. An inline IPS also adds latency and becomes a failure point, so decide whether it fails open or closed. Suricata, Snort, and Zeek are the common open-source engines. These devices are middleboxes in the end-to-end sense. They sit in the path and read layers above the one they are meant to work at. See Firewall rules and iptables and Network hardening for the defensive side.


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.

An application’s architecture decides which hosts talk to which. Routers and switches don’t run application code, so everything here happens on end systems.

Client-serverPeer-to-peer
StructureAn always-on server with a well-known address. Clients start every exchange and never talk to each otherHosts exchange data directly with each other
ScalingAdd servers behind a load balancer or in a clusterEvery peer that downloads also uploads, so capacity grows with the number of peers
CostServer capacity and bandwidthMostly borne by the participants
WeaknessServers are a cost and a single point of attackHarder to secure, monitor, and keep reliable
Typical useWeb, APIs, email, databasesFile and image distribution

Almost all production services are client-server. Peer-to-peer appears where one large artifact must reach many hosts at once, as in some container image distribution tools.

Either way, communication happens between processes, not hosts. The process that opens the conversation is the client and the one that waits is the server, and a process is addressed by the host’s IP address plus a port number.

What applications need from the transport layer

Section titled “What applications need from the transport layer”

Applications choose a transport protocol by what they need along four dimensions:

  • Reliability - file transfers, web pages, email, and payments need every byte intact. Voice and video calls tolerate some loss.
  • Throughput - some applications need a minimum rate to work at all. Bulk transfers are elastic and use whatever is available.
  • Timing - interactive applications such as calls and games need low and steady delay.
  • Security - confidentiality, integrity, and authentication of the endpoints.
ServiceTCPUDPQUIC
ConnectionYesNoYes
DeliveryReliable, ordered byte streamUnreliable datagramsReliable, independent streams
Congestion controlYesNoYes
EncryptionNone by default, added with TLSNoneBuilt in (TLS 1.3)
Throughput guaranteeNoNoNo
Delay guaranteeNoNoNo

None of the three offers a throughput or delay guarantee on the public internet. Applications that need steady timing, such as streaming and calls, work around it with buffering and by adapting their bitrate. See CDNs and video streaming.

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

A socket is the operating system handle an application uses to send and receive on the network. It is the boundary between the application layer and the transport layer, and it is identified by an IP address and a port.

StepTCP serverTCP clientUDP (either side)
1socket() createsocket() createsocket() create
2bind() to a portbind() to a port (server side)
3listen()connect() (three-way handshake)
4accept() returns a new socket for each clientsend() / recv()sendto() / recvfrom()
5send() / recv() on the new socketclose()close()

The accept() step explains how one port serves many clients. The listening socket stays open on the port and each completed handshake produces a separate connected socket, distinguished by the client’s address and port.

# server.py: echo one message per connection (a real server would use threads or async I/O)
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind(("0.0.0.0", 9000))
srv.listen(5) # queue of connections that finished the handshake
while True:
conn, addr = srv.accept() # blocks until a client connects
with conn:
conn.sendall(conn.recv(1024))
client.py
import socket
with socket.create_connection(("127.0.0.1", 9000)) as s:
s.sendall(b"ping")
print(s.recv(1024)) # b'ping'

UDP skips the connection steps. Each datagram carries the destination address, and the receiver learns the sender from recvfrom().

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("0.0.0.0", 9000))
data, addr = s.recvfrom(2048)
s.sendto(data, addr) # reply to whoever sent it

nc gives the same behavior from the shell, and ss shows the sockets.

Terminal window
nc -l 9000 # listen on TCP 9000
nc 127.0.0.1 9000 # connect to it
nc -u -l 9000 # UDP listener
ss -tlnp | grep 9000 # listening socket and owning process
ss -tn dst 127.0.0.1:9000 # established connections

For a listening socket, ss -ltn shows the current accept queue in Recv-Q and its maximum in Send-Q. A server that falls behind lets that queue fill, and new connection attempts are then dropped or time out on the client.


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'