TCP Basics

Is TCP layer 3 or 4?

TCP is a layer 4 protocol, the transport layer. IP is layer 3. Here is what each layer does and how they fit together in a network packet.

On this page
  1. Short answer: layer 4
  2. Why people mix them up
  3. The layers side by side
  4. How the layers appear in a packet
  5. What this means in practice
  6. Checking on your own machine

Short answer: layer 4

TCP is a layer 4 protocol, the transport layer. IP, which TCP runs on, is a layer 3 protocol, the network layer. TCP is not layer 3, even though the two are often written together as TCP/IP.

Layer 3 moves packets between networks using IP addresses. Layer 4 moves data between programs on those machines, using port numbers. TCP adds reliability at this layer, and UDP offers simpler delivery without it.

If the numbering itself is new to you, the basics are in what is L1, L2, L3, and L4 in networking.

Why people mix them up

Two things cause most of the confusion. The name TCP/IP puts the two protocols together, so they often get treated as one thing. The other is that the common layer models count differently. The OSI model has seven layers, and the TCP/IP model has four.

The full seven-layer list, with what each layer does, is in what are the 7 network layers and their functions.

The layers side by side

OSI layer TCP/IP model layer Examples
7 Application, 6 Presentation, 5 Session Application HTTP, SSH, SMTP
4 Transport Transport TCP, UDP
3 Network Internet IP, ICMP
2 Data link Link Ethernet, Wi-Fi
1 Physical Link Cables, radio signals

In the TCP/IP model, the transport layer is the one that matches OSI layer 4, and the internet layer is the one that matches OSI layer 3. That is the whole answer to the question, in a table.

How the layers appear in a packet

When data is sent, each layer adds its own header in front of what it received. From the outside in, a packet on an Ethernet network looks like this:

[ Ethernet header ][ IP header ][ TCP header ][ application data ]
     layer 2          layer 3       layer 4      layers 5 to 7

The IP header holds the source and destination IP addresses. The TCP header holds the source and destination ports. A standard router reads the IP header to decide where to send the packet, and it does not need to read the TCP header.

What this means in practice

  • A router works at layer 3. It forwards packets by IP address.
  • A stateful firewall often tracks TCP connections and their flags, so it works with layer 4 details as well.
  • A layer 4 load balancer spreads TCP connections across a group of servers, using addresses and ports rather than the content of the request.

Knowing which layer a device works at helps you pick the right tool when something breaks. A router problem looks different from a port problem.

Checking on your own machine

  • On Linux, ip addr shows IP addresses (layer 3), and ss -tn shows TCP sockets (layer 4).
  • On Windows, ipconfig shows IP addresses, and netstat -ano -p TCP shows TCP connections.

Some textbooks number the layers in different ways, so when you read one, check which model it uses before you compare numbers.

The complete guide to tcp basics

TCP Services in Computer Networks
Read the guide

Keep exploring

Guides from other topics

All topics →