Network Communication Chapter 4

Introduction

Have you ever wondered what actually happens when you open a website or send a message online? Behind the scenes, your data goes on quite a journey — hopping through routers, getting wrapped in headers, and relying on clever protocols to arrive at the right place. In this post, we'll walk through two critical layers of networking: the Network Layer and the Transport Layer. By the end, you'll understand how devices find each other, how data gets routed across the globe, and why some applications need guaranteed delivery while others are happy to "wing it."

The Network Layer

The network layer is what allows devices (your phone, your laptop, a server halfway around the world) to exchange data across interconnected networks. It handles four fundamental processes:

  1. Addressing end devices — Every device on a network needs a unique IP address so it can be identified. Think of it as a mailing address for your device.
  2. Encapsulation — When data arrives from the transport layer above, the network layer wraps it with an IP header, creating what we call a packet. This header contains all the routing information needed to get the data where it's going.
  3. Routing — The network layer figures out how to get the packet from the source to the destination, potentially through many intermediate devices.
  4. De-encapsulation — When the packet finally arrives at the destination, the IP header is stripped off and the data is passed back up to the transport layer.

The Nature of IP

The two most common network layer protocols are IPv4 and IPv6. Before we dive into the differences, let's understand the basic characteristics that both share:

  • Connectionless — IP doesn't establish a connection before sending data. It's like dropping a letter in a mailbox — you don't call the recipient first to let them know it's coming.
  • Best Effort (Unreliable) — There's no guarantee your packet will arrive. The sender doesn't even know if the destination device is turned on. (Don't worry — reliability is handled at a higher layer!)
  • Media Independent — IP doesn't care whether your data travels over fiber optic cable, Wi-Fi, or even a satellite link. It works the same regardless of the physical medium.

IPv4 and Its Limitations

IPv4 has served us well for decades, but it comes with some significant problems:

  • Address depletion — IPv4 addresses are 32-bit, giving us roughly 4.3 billion unique addresses. That sounds like a lot, but with billions of devices connected today, we've essentially run out.
  • Lack of end-to-end connectivity — To cope with address shortages, we use Network Address Translation (NAT), which lets many devices share a single public IP. The downside? Devices behind NAT are hidden from the outside world, which breaks certain applications that need direct connections (like peer-to-peer services).
  • Increased network complexity — NAT adds extra processing, introduces latency, and makes troubleshooting harder.

IPv6: The Solution

IPv6 was designed to solve these problems:

  • Massive address space — IPv6 uses 128-bit addresses. To put that in perspective, that's roughly 340 undecillion addresses (3.4 × 10³⁸). Every grain of sand on Earth could have its own IP address — several times over.
  • Simplified header — The IPv6 header has fewer fields than IPv4, which means routers can process packets more efficiently.
  • No need for NAT — With so many addresses available, every device can have its own globally unique address. This restores true end-to-end connectivity.
  • Built-in security — IPv6 natively supports IPsec for authentication and encryption, whereas in IPv4, security features were added on as an afterthought.

Routing

Now that we understand how devices are addressed, let's look at how packets actually find their way through the network.

Where Can a Host Send Packets?

A device can send packets to three types of destinations:

  • Itself — By pinging the loopback address (127.0.0.1 for IPv4, or ::1 for IPv6). This is a quick way to test whether the device's own networking stack is working properly.
  • A local host — A device on the same local network. For example, another computer connected to the same Wi-Fi router.
  • A remote host — A device on a completely different network, like a web server on the other side of the world.

How Does a Host Know Where to Send?

When your device wants to send a packet, it first needs to determine: is the destination on my local network, or is it somewhere else?

  • In IPv4, the device checks its own subnet mask along with its IP address and the destination IP address. If they're on the same subnet, the packet stays local.
  • In IPv6, the local router advertises the network prefix to all devices, so each device knows what "local" looks like.

If the destination is on a different network, the device forwards the packet to its default gateway — usually a router that knows how to reach other networks.

The Router's Routing Table

Routers are the workhorses of inter-network communication. Each router maintains a routing table that contains three types of entries:

  • Directly-connected networks — Networks that are physically attached to the router's own interfaces.
  • Remote networks — Networks that the router has learned about, either through manual configuration or dynamic routing protocols.
  • Default route — A fallback route used when no specific match exists in the table. Think of it as "if you don't know where to go, send it this way."

Static vs. Dynamic Routing

There are two approaches to populating a routing table:

Static routing is where a network administrator manually enters routes. It's simple and predictable, but if the network topology changes (say, a link goes down), someone has to manually update the routes. This doesn't scale well for large or frequently changing networks.

Dynamic routing uses protocols that allow routers to automatically discover and share route information with each other. If a link fails, routers can detect the change and find alternative paths on their own. This is far more resilient and is what powers the internet's backbone.

ICMPv4: The Network's Diagnostic Tool

The Internet Control Message Protocol version 4 (ICMPv4) isn't about delivering user data — it's a helper protocol used for diagnostics and error reporting.

Key ICMP Messages

  • Echo Request / Echo Reply — This is the mechanism behind the ping command. Your device sends an Echo Request to a target; if the target is reachable, it sends back an Echo Reply. Simple, but incredibly useful for testing connectivity.
  • Destination Unreachable — When a router or host can't deliver a packet (maybe the destination doesn't exist, or a service isn't running), it sends this message back to the source.
  • Time Exceeded — Every IP packet has a Time to Live (TTL) field that decreases by 1 at each router. If TTL hits 0, the packet is discarded and a Time Exceeded message is sent back. This prevents packets from looping endlessly through the network.

Practical Tools: Ping and Traceroute

Ping is the most common connectivity test. You can use it in a layered approach to isolate problems:

  1. Ping the loopback (127.0.0.1) — Tests your own TCP/IP stack.
  2. Ping the default gateway — Tests connectivity to your local router.
  3. Ping a remote host — Tests end-to-end connectivity.

Traceroute (or tracert on Windows) goes a step further. It maps out the entire path a packet takes to reach a destination, listing every router (hop) along the way. It works by sending packets with incrementally increasing TTL values, so each router along the path reveals itself via Time Exceeded messages.

Traceroute sends three packets per hop and measures the round-trip time (RTT) for each. If a packet is lost or goes unreplied, you'll see an asterisk (*) in the output — a clue that something might be wrong at that hop.

The Transport Layer

We've seen how the network layer gets packets from one device to another. But what about making sure the right application gets the data? That's where the transport layer comes in.

The transport layer is responsible for:

  • Establishing temporary communication sessions between two applications.
  • Segmenting data into smaller pieces for transmission and reassembling them on the receiving end.
  • Identifying the correct application for each data stream using port numbers.

There are two main transport layer protocols, each with very different philosophies: TCP and UDP.

Port Numbers: Directing Traffic to the Right App

Both TCP and UDP use port numbers to identify which application should handle incoming data. When you send a request, two port numbers are involved:

  • Destination port — Tells the receiving device which service you're requesting. For example, port 80 means HTTP (web), port 443 means HTTPS, port 25 means SMTP (email).
  • Source port — A randomly generated number on the sending device that identifies this specific conversation. This is how your device can have multiple tabs open to different websites simultaneously — each tab uses a different source port.

The source and destination ports are placed in the segment header, and the segment is then encapsulated inside an IP packet for delivery.

Transmission Control Protocol (TCP)

TCP is the reliable workhorse of the internet. When your data absolutely, positively has to get there — and in the right order — TCP is the protocol for the job.

Core Features

  • Session Establishment — TCP is connection-oriented. Before any data flows, the two devices negotiate and establish a session. This ensures both sides are ready.
  • Reliable Delivery — TCP tracks every segment sent. If something gets lost or corrupted, the source retransmits it.
  • Same-Order Delivery — Segments are numbered and sequenced, so even if they arrive out of order, the receiving device can reassemble them correctly.
  • Flow Control — If the receiving device is overwhelmed (low on memory, limited bandwidth), it can tell the sender to slow down. This prevents data loss due to congestion.

The trade-off? All this reliability comes with overhead. Each TCP segment carries a 20-byte header — considerably more than UDP's 8 bytes.

The Three-Way Handshake

Before TCP can transmit data, it establishes a connection through a process called the three-way handshake:

  1. SYN — The client sends a segment with the SYN (Synchronize) flag set, essentially saying "I'd like to start a conversation."
  2. SYN-ACK — The server responds with both SYN and ACK (Acknowledgement) flags, saying "Got it, and I'd like to talk too."
  3. ACK — The client sends back a final ACK, confirming the connection is established.

Once this handshake is complete, data can flow freely in both directions.

Closing a Connection

To close a TCP session, a similar (but simpler) process occurs using the FIN (Finish) flag. Since TCP connections are bidirectional, each direction is closed independently with a FIN segment followed by an ACK — making it a two-way handshake for each direction (four segments total to fully close).

Handling Out-of-Order Data

Network packets don't always arrive in the order they were sent — they might take different paths or experience different delays. TCP handles this gracefully: the receiving side places incoming segments into a buffer, sorts them by sequence number, and only passes the fully reassembled data up to the application.

Acknowledgement and Retransmission

TCP ensures reliability through acknowledgements. The destination acknowledges received data, and if the source doesn't receive an ACK within a set time, it retransmits the data from the last acknowledged point.

The window size field in the TCP header controls how much data can be sent before an acknowledgement is required. A larger window allows more data to flow before pausing for confirmation, improving throughput on high-latency connections. This mechanism also supports flow control — the receiver can shrink the window to slow the sender down.

Common Applications Using TCP

TCP is the go-to protocol for applications that need reliable, ordered delivery:

  • HTTP/HTTPS — Web browsing
  • FTP — File transfers
  • SMTP — Sending email
  • Telnet — Remote terminal access

User Datagram Protocol (UDP)

If TCP is the careful, methodical courier, UDP is the "just throw it and hope for the best" approach. And that's not a bad thing — for the right use case, it's exactly what you want.

Core Characteristics

  • Connectionless — No handshake, no session establishment. Data is sent immediately.
  • Unreliable delivery — No acknowledgements, no retransmission. If a packet is lost, it's gone.
  • No ordered reconstruction — Segments might arrive out of order, and UDP won't rearrange them.
  • No flow control — The sender transmits at whatever rate it wants, with no mechanism to slow down.

Why Would Anyone Use UDP?

Because sometimes, speed matters more than perfection. UDP's minimal overhead (just an 8-byte header) makes it incredibly fast and efficient. The data units in UDP are called datagrams, and they're delivered on a best-effort basis.

Consider these use cases:

  • DNS — When your browser looks up a domain name, it needs an answer fast. If the query is lost, it can simply ask again.
  • Video streaming — A dropped frame here and there is barely noticeable, but buffering due to retransmission delays would ruin the experience.
  • VoIP (Voice over IP) — Real-time voice communication can tolerate small gaps, but it can't tolerate the delays that TCP's reliability mechanisms would introduce.
  • TFTP / SNMP — Lightweight protocols where simplicity and speed are prioritized.

TCP vs. UDP: A Quick Comparison

FeatureTCPUDP
Connection typeConnection-oriented (handshake)Connectionless
ReliabilityGuaranteed delivery with ACKsBest effort, no guarantees
OrderingSegments reassembled in orderNo ordering
Flow controlYes (window size)No
Header overhead20 bytes8 bytes
SpeedSlower (more overhead)Faster (less overhead)
Use casesWeb, email, file transferStreaming, DNS, VoIP

Wrapping Up

The network and transport layers work hand-in-hand to deliver data across the internet. The network layer handles addressing and routing — figuring out where to send your data and how to get it there. The transport layer ensures the data reaches the right application and decides how carefully it needs to be delivered.

Understanding these layers gives you a solid foundation for grasping how the internet really works — from the moment you type a URL in your browser to the instant the page appears on your screen. Every packet goes through addressing, routing, encapsulation, and transport protocol handling, all happening invisibly in milliseconds.

Whether it's TCP's meticulous reliability or UDP's carefree speed, each protocol has its place. The beauty of networking is in how all these pieces fit together to create the seamless experience we take for granted every day.

留言區