IP Addresses, Ports, TCP and UDP
Overview
For a client to reach a server, the network needs to know which machine and which program to deliver to, and how carefully to deliver the data. This lesson covers IP addresses, ports, and the two main delivery styles: TCP and UDP.
Detailed Notes
What is a network address
Data on the internet travels in small chunks called packets. Each packet needs an address, just like a letter.
- An IP address identifies a machine on a network, for example
203.0.113.25. It is like the street address of a building. - A port is a number (0-65535) that identifies a specific program on that machine. It is like the apartment number inside the building.
One server can run a website, a database and an email program at the same time. Ports tell the machine which one should receive each packet. Common ports: 80 for plain web traffic, 443 for secure web traffic, 22 for remote login.
Together, an IP address plus a port, such as 203.0.113.25:443, points to exactly one running program.
The problem it solves
Without addresses, there would be no way to say which of billions of machines should get your data, and without ports, no way to say which program on that machine. Also, networks lose, delay and reorder packets. We need rules so that data still arrives correctly when it matters, and quickly when speed matters more.
How it works
Networking is built in layers, each doing one job and trusting the layer below:
- Application layer: the language apps speak (HTTP for web pages; covered in the next lesson).
- Transport layer: delivers data between programs using ports (TCP or UDP).
- Network layer: moves packets between machines using IP addresses, hop by hop through routers.
- Link layer: sends bits over the actual cable or Wi-Fi.
A router is a device that looks at a packet's destination IP and forwards it one step closer. A packet usually crosses many routers.
At the transport layer you pick one of two protocols (a protocol is an agreed set of rules for communicating):
TCP (Transmission Control Protocol) is careful. First the two sides do a handshake, a short exchange to agree they are connected. Then TCP numbers the data, confirms it arrived, re-sends lost pieces and delivers it in order.
UDP (User Datagram Protocol) is fast and light. It just sends packets with no handshake, no confirmation and no re-sending. If a packet is lost, it is lost.
| TCP | UDP | |
|---|---|---|
| Connection set-up | Yes (handshake) | No |
| Delivery guaranteed | Yes, re-sends lost data | No |
| Order preserved | Yes | No |
| Speed / overhead | Slower, more overhead | Faster, lighter |
| Typical use | Web pages, APIs, file transfer, databases | Live video, voice calls, games, DNS lookups |
A simple example
TCP is like registered mail: you get a receipt, lost letters are re-sent, and they arrive in order. UDP is like shouting a message across a room: very quick, but if someone coughs and misses a word, you do not repeat it.
For a bank transfer you want registered mail. For a live video call, a missed fraction of a second is better ignored than replayed late, so shouting is fine.
System design example
A shopping app talks to its backend over TCP, because every order and payment detail must arrive complete and in order. The same company's live-streaming feature and its multiplayer game use UDP, because fresh data matters more than perfect data and re-sending old frames would only add delay.
Another practical point: servers expose only the ports they need. A firewall (a filter that allows or blocks traffic by address and port) lets port 443 through to the web servers and blocks everything else. Closing unneeded ports shrinks what attackers can reach.
Summary
- An IP address finds the machine; a port finds the program on it.
- Networks work in layers; routers forward packets by IP address.
- TCP is reliable and ordered but heavier; UDP is fast but may lose data.
- Pick TCP when correctness matters, UDP when freshness and speed matter more.
- Firewalls allow or block traffic by address and port.
Architecture Diagram
The client's packets hop through routers and a firewall to reach a port on the server.
- 1The client addresses packets to the server's IP and port.
- 2Routers forward the packets hop by hop.
- 3The firewall allows traffic on port 443 only.
- 4The server program listening on that port receives the data.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

