# TCP vs IP
## Understanding the Difference
TCP and IP are two distinct protocols that work together to enable internet communication, but they operate at different layers and serve different purposes. The internet is named after the Internet Protocol because IP is the fundamental protocol that makes global networking possible. TCP builds on top of IP to provide reliable communication. They are so closely associated that they are usually referred to together as TCP/IP, but understanding each individually is important.
## What IP Does
IP, the Internet Protocol, operates at the Internet layer of the TCP/IP model. It is responsible for two fundamental tasks: addressing and routing. IP defines the address scheme that gives every device connected to the internet a unique IP address. These addresses are used to identify the source and destination of every packet. IP also defines how routers forward packets toward their destination across the multiple networks that make up the internet. IP is connectionless and unreliable. It makes a best-effort attempt to deliver packets but provides no guarantee of delivery, no guarantee of ordering, and no error recovery. If a packet is lost, IP has no mechanism to detect or correct this.
## What TCP Does
TCP, the Transmission Control Protocol, operates at the Transport layer of the TCP/IP model. It uses IP to carry its segments between devices. TCP's job is to provide reliable, ordered communication between applications. TCP establishes connections before data transfer, acknowledges received data, retransmits lost data, reorders out-of-sequence segments, controls the flow of data to prevent overwhelming the receiver, and detects and responds to network congestion. TCP is connection-oriented, meaning a logical connection is established before data exchange begins and properly terminated when communication is complete.
## How They Work Together
When an application wants to send data over the internet, it passes the data to TCP. TCP divides the data into segments, adds a TCP header with port numbers and sequence numbers, and passes each segment to IP. IP adds an IP header with source and destination IP addresses and routes the packet toward its destination. At the destination, IP delivers the packet to the correct Transport layer protocol based on information in the IP header, which then delivers it to TCP. TCP uses the sequence numbers to reassemble segments in order and acknowledges receipt.
## IP is for Routing, TCP is for Reliability
A simple way to think about the difference is that IP is responsible for getting packets to the right destination across the network, while TCP is responsible for ensuring those packets arrive reliably, in order, and without errors. IP handles the network-level routing between millions of interconnected networks. TCP handles the application-level reliability between two specific applications communicating over those networks.
## When UDP is Used Instead of TCP
Not all applications require the reliability that TCP provides. Some applications prefer the speed and simplicity of UDP over the overhead of TCP's reliability mechanisms. DNS queries use UDP because a lost query can simply be resent and the overhead of TCP connection establishment would slow down the name resolution process. Video streaming uses UDP because a slightly degraded video due to occasional lost packets is preferable to the delays that TCP retransmission would introduce. Online gaming uses UDP for similar reasons. In these cases, the application layer protocol handles any necessary reliability at a higher level.Back to Subject