TCP/IP Explained: Layers, Handshake and Ports
What is TCP/IP?
TCP/IP is the suite of protocols that runs the internet and almost every private network. IP (Internet Protocol) addresses and routes packets between networks, while TCP (Transmission Control Protocol) delivers data reliably and in order between applications. The suite also includes UDP, ICMP, DNS, HTTP and many others, organised into four layers: link, internet, transport and application.
The four layers of TCP/IP
| Layer | Responsibility | Key protocols |
|---|---|---|
| Application | Services that programs use directly | HTTP, HTTPS, DNS, SSH, SMTP, DHCP |
| Transport | Delivery between applications using port numbers | TCP, UDP, QUIC |
| Internet | Logical addressing and routing across networks | IPv4, IPv6, ICMP |
| Link | Delivery on the local medium | Ethernet, Wi-Fi, ARP |
If you have studied the seven OSI layers, the TCP/IP application layer covers OSI Layers 5 to 7 and the link layer covers Layers 1 and 2. The middle two layers line up directly.
IP: addressing, routing and delivery
IP gives every interface an address and puts the source and destination addresses in each packet header. Routers read the destination and forward the packet towards it. IP itself does not promise delivery: packets can be lost, duplicated or arrive out of order. The header also carries a TTL (time to live, called hop limit in IPv6) that each router decrements, so a packet caught in a loop is eventually discarded.
ICMP carries control and error messages for IP. ping uses ICMP echo requests and replies, and traceroute relies on ICMP "time exceeded" messages to reveal each router along a path.
TCP versus UDP
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented, uses a handshake | Connectionless, no handshake |
| Reliability | Acknowledges data and retransmits losses | No acknowledgement or retransmission |
| Ordering | Reassembles data in order | Delivers datagrams as they arrive |
| Overhead | Larger header, more round trips | Small header, low latency |
| Typical uses | Web pages, SSH, email, file transfer | DNS queries, voice and video calls, online games, DHCP |
QUIC, the transport behind HTTP/3, runs over UDP but adds its own reliability and encryption. That is why modern browsers send much of their traffic to UDP port 443 as well as TCP port 443.
The TCP three-way handshake
Before any data flows, TCP opens a connection with three messages. Each side picks a random initial sequence number (ISN) so that both ends can track which bytes have been received.
- SYN: the client sends a segment with the SYN flag set and its ISN, say 1000, to the server port, for example 443.
- SYN-ACK: the server replies with SYN and ACK set, its own ISN, say 5000, and an acknowledgement number of 1001.
- ACK: the client acknowledges with acknowledgement number 5001. The connection is now established and data can flow both ways.
Client Server
| ---- SYN seq=1000 ----------> |
| <--- SYN-ACK seq=5000 ack=1001 |
| ---- ACK ack=5001 ----------> |
| connection open |Closing a connection normally uses FIN and ACK messages from each side, while a RST flag aborts it immediately. In Wireshark, the display filter tcp.flags.syn == 1 on a capture of your own traffic shows each handshake clearly.
Common port numbers to know
| Port | Protocol | Service | Note |
|---|---|---|---|
| 20, 21 | TCP | FTP | Unencrypted; prefer SFTP |
| 22 | TCP | SSH and SFTP | Encrypted remote administration |
| 23 | TCP | Telnet | Unencrypted; avoid on production networks |
| 25, 587 | TCP | SMTP | Mail transfer and mail submission |
| 53 | UDP and TCP | DNS | Name resolution |
| 67, 68 | UDP | DHCP | Server and client ports |
| 80 | TCP | HTTP | Unencrypted web |
| 123 | UDP | NTP | Time synchronisation |
| 143, 993 | TCP | IMAP, IMAPS | Mailbox access |
| 161 | UDP | SNMP | Device monitoring |
| 443 | TCP and UDP | HTTPS, HTTP/3 | Encrypted web |
| 3389 | TCP | RDP | Windows remote desktop; never expose directly |
A port number identifies a service on a host, while an IP address identifies the host itself. Together, the source IP, source port, destination IP, destination port and protocol form a five-tuple that uniquely identifies each connection. Firewalls, NAT devices and load balancers all track traffic using this five-tuple, which is why port knowledge matters as much for security work as for troubleshooting.
Ports 0 to 1023 are well-known ports, 1024 to 49151 are registered ports, and higher numbers are dynamic ports that clients pick for their side of a connection.
Inspecting TCP/IP on Linux
A few commands show what your own machine is doing. Run them on a lab VM or your laptop.
ip a # interfaces and IP addresses
ip route # routing table and default gateway
ss -tulpn # listening TCP and UDP sockets with process names
ss -tn state established
dig example.com # DNS lookup over UDP 53For a fuller tour of these tools, see the Linux for networking guide. If you prefer a guided route, the online networking and security course from SkillNewton builds on these fundamentals through project-based work.
Common mistakes
- Assuming UDP is unreliable in a bad sense, when it is simply the right choice for real-time traffic.
- Forgetting that DNS uses TCP as well as UDP, which matters when writing firewall rules.
- Reading a timeout as "the server is down" without checking whether a firewall is dropping SYN packets.
- Leaving legacy plain-text services such as Telnet or FTP enabled on production devices.
- Learning port numbers by rote without matching them to traffic in a real capture.
Frequently asked questions
Is TCP/IP the same as the internet?
No. TCP/IP is the protocol suite; the internet is the global network of networks that uses it. Private office networks run TCP/IP too.
Why does the handshake use random sequence numbers?
Random initial sequence numbers make it harder for an outsider to inject forged segments into a connection and help avoid confusion with old segments.
Can I scan ports to learn which services are open?
Only on systems you own or have written permission to test. Nmap is a standard tool for authorised scanning in labs and audits; scanning other networks without consent can be illegal.
What should I study after TCP/IP?
Move on to how routers and switches forward traffic, then firewall and security concepts.