TCP/IP and Ports Explained: Your Guide to How the Internet Transports Data


Alright, so in the last article, we figured out how our browser plays detective with DNS to find a website's IP address. We got the 'where'. But what about the 'how'? How does my laptop in Chennai actually talk to a server sitting in a data centre in Mumbai or even Frankfurt? How does that massive 1GB game update I downloaded actually arrive on my machine in one piece?
If you're anything like me, you've probably hit a point in your developer journey where you're staring at a "Connection Timed Out" error and wondering what's happening in that black box between your code and the server. That black box, my friend, is where the real magic of the internet's postal service happens. Today, we're tearing it open.
Let's get our hands dirty with the fundamentals – the fundas – of how data actually travels: TCP/IP and Ports.
The Big Problem: Sending Stuff Over a Messy Network
First, let's be clear: the internet is not a single, clean pipe. It's a ridiculously complex, chaotic mesh of routers, switches, and cables. Sending a large file, like a photo, from your phone to a server is like trying to send a massive painting through a series of thousands of tiny, unreliable mailrooms.
If you just shoved the whole painting into the first mailroom, it would get stuck. And even if you could send it, what if one of the mailrooms on the way lost it or damaged a piece of it? You'd be left with a corrupted file.
The brilliant minds who built the internet came up with a simple, elegant solution.
The Solution: The Humble Data Packet
Instead of sending the whole painting at once, what if you cut it into a thousand postcard-sized pieces? You'd number each piece (1 of 1000, 2 of 1000, etc.), put the destination and return address on each one, and then send them all out.
That’s exactly what your computer does. It takes your data, whether it's a webpage, an email, or a video stream, and chops it up into thousands of tiny pieces called packets. Each packet is a small envelope containing:
A piece of the actual data (the payload).
A header with crucial info, like the source IP address, the destination IP address, and other control information.
Now, these packets are sent out onto the internet, and they might all take different routes to get to their destination. But once they arrive, the receiving computer needs a way to reassemble them correctly. For that, it relies on one of two "delivery boys."
Meet the Delivery Boys: TCP and UDP
This is where things get really interesting for us as developers. When our application sends data, it doesn't just throw packets into the void. It chooses a protocol, a set of rules for the delivery. The two most famous ones are TCP and UDP.
TCP (Transmission Control Protocol): The Reliable, Registered Post
Think of TCP as that super-sincere, responsible friend who's helping you move house. They won't just throw boxes in a truck; they'll have a checklist.
They call first: Before sending anything, TCP establishes a connection. It's a bit like a phone call:
Your computer (SYN): "Hello server, are you there? I'd like to send you some data."
The server (SYN-ACK): "Yes, I'm here and ready to receive your data."
Your computer (ACK): "Great! The data is on its way."
This whole dance is famously called the Three-Way Handshake. It ensures both ends are ready before any data is actually sent.
- They track every box: TCP numbers every single packet. If a packet gets lost, the receiver notices a gap in the sequence and asks the sender, "Hey, I didn't get packet #47, can you send it again?" It also ensures packets are reassembled in the correct order.
Because of this, TCP is called a reliable, connection-oriented protocol. It's perfect for things where data integrity is non-negotiable, like:
Loading a webpage (you need all the HTML, right?).
Sending an email.
Downloading a file.
The trade-off? All this checking and confirming adds a bit of overhead, making it slightly slower.
UDP (User Datagram Protocol): The Fast, No-Frills Courier
Now, let's talk about TCP’s cousin, UDP. If TCP is the responsible friend with a checklist, UDP is the chill cousin who just gets things done, fast. UDP is like sending a postcard. You write the message, put the address on it, and drop it in the letterbox.
You don't call beforehand to check if the person is home.
You don't get a confirmation when it's delivered.
If the postcard gets lost in the mail, well, that's just too bad.
UDP is connectionless. It's a "fire and forget" protocol. It wraps your data packet, adds the source and destination info, and sends it out, hoping for the best. There's no handshake, no packet reordering, and no error recovery.
Why on earth would anyone use such an unreliable thing? Speed.
By skipping all the reliability checks, UDP is incredibly fast and has very low overhead. It's perfect for situations where losing a tiny bit of data is acceptable, but speed is everything:
Video Streaming/Online Calls: On a Zoom or Google Meet call, if you miss a millisecond of audio, it's better for the stream to just continue rather than pausing to retrieve the lost packet. A small glitch is better than a long buffer.
Online Gaming: In a game like Valorant or BGMI, your character's latest position needs to reach the server now. Old data is useless. Speed is more important than ensuring every single packet from 2 seconds ago arrives.
DNS Lookups: The very DNS request we talked about in the last article is a small query that needs a quick response. It uses UDP for its speed. If the request fails, the application just tries again.
The Final Destination: What in the World is a Port?
Okay, so our packet, sent via TCP or UDP, has successfully navigated the internet and arrived at the correct IP address. It's reached the right building. But what now?
A single server is like a huge apartment complex. It's running multiple applications at the same time: a web server, a database, an SSH service, and maybe your own custom application. How does the packet know which "flat" to go to?
That's where ports come in. A port is simply a number (from 0 to 65535) that identifies a specific application or service running on a computer.
The Analogy is simple: IP Address = Building Address, Port Number = Flat Number.
When your browser wants to access a secure website, it sends a packet to the server's IP address, addressed specifically to port 443. The server's operating system sees this port number and says, "Aha! This one's for the Nginx/Apache web server," and hands the packet over.
Some common ports you'll see all the time are:
80: for HTTP (standard web traffic)
443: for HTTPS (secure web traffic)
22: for SSH (Secure Shell access to servers)
53: for DNS queries
5432: for PostgreSQL databases
27017: for MongoDB databases
Why Should We, as Developers, Care?
So, why did we just go through all this? Because understanding this stuff isn't just for network engineers; it's a superpower for us developers.
Smarter Debugging: The next time your application can't connect to a database, you'll know what to check. Is the server reachable (an IP issue)? Is the database actually running and listening on port 5432? Is a firewall blocking that specific port? This knowledge turns "it's not working" into a clear diagnostic path. The first time I had to debug a firewall issue, understanding that a service wasn't 'listening' on a specific port was a real eureka moment for me.
Better System Design: When you're building an application, you get to choose the tools. Are you building a real-time chat app? You'll probably use WebSockets, which run over TCP for reliability. Building a high-performance game server? You might have to work with UDP to get the lowest latency. Knowing the trade-offs is key.
Fundamental Security: Understanding ports is the first step in network security. When you set up a server, you don't just leave all 65,535 "doors" open. You configure a firewall to only allow traffic on the ports you absolutely need, like 443 and 22, drastically reducing the attack surface.
So, the next time you write a line of code that makes a network call, take a moment to appreciate the incredible journey your data is about to take, chopped into packets, wrapped in a protocol, addressed to an IP and a specific port, and sent across the world in milliseconds.
We've just unplugged another layer of the internet. Makes sense, no? Sorted.
Subscribe to my newsletter
Read articles from Ashwin R directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
