Last updated on

Networking Basics

💻 Networking Basics: Client-Server Communication

Communication between a client computer and a server happens over a network, and it relies on knowing the address of each machine.

IP Addresses: Unique Identification

  • Definition: An IP address (Internet Protocol address) is a unique identifier assigned to every machine communicating over the public internet. It distinguishes one machine from another.
  • IPv4: This is the common version, using a 32-bit integer.
    • Format: It is typically written as four sets of three digits separated by dots (e.g., 192.168.0.1).
    • Value Limit: The maximum value for each segment is 255 (not 999), meaning the range is from 0 to 255.
    • Limitation: A 32-bit address can store up to about 4 billion unique public IP addresses, which is becoming a limiting factor.
  • IPv6: This newer version uses 128 bits, providing a virtually limitless number of unique addresses.
  • Static vs. Dynamic IP:
    • Static: An IP address that does not change (more important for servers).
    • Dynamic: An IP address that may change over time (common for clients and some servers, which is managed by protocols like Dynamic DNS).

✉️ Data Transmission: Packets and Protocols

Data is sent across the network in units called packets. The rules governing how this data is sent are defined by protocols.

The Internet Protocol (IP) and IP Packets

  • Role: The Internet Protocol (IP) defines the basic rules for sending data between machines, which is why the addresses are called IP addresses.
  • Data Structure (IP Packet): A packet of data includes two main parts:
    • IP Header (Metadata): This contains essential descriptive information (metadata), including the source IP address and the destination IP address. This is analogous to the address information on an envelope.
    • Data: The actual content being sent.

Transmission Control Protocol (TCP) and Reassembly

  • Problem Solved by TCP: IP alone does not handle sending large amounts of data that must be split into multiple packets. The receiving machine needs a way to reassemble the data in the correct order.
  • Role: The Transmission Control Protocol (TCP) solves this by adding more information to the packet’s data section.
  • Data Structure (TCP Packet within an IP Packet): The data portion of the IP packet is further segmented:
    • TCP Header (More Metadata): This includes crucial information like the sequence number of the packet.
    • Application Data: The actual content (e.g., an HTTP request or response) being sent.
  • Function: The sequence number allows the receiving machine (e.g., the server) to reassemble the multiple packets in the correct order to reconstruct the original large message.

🧱 The Networking Layers

Protocols operate at different levels, which is often described using a layered model (like the OSI model or TCP/IP model).

LayerProtocol ExamplesPrimary Concern
ApplicationHTTP (Hypertext Transfer Protocol)What applications use and worry about; the data itself.
TransportTCP (Transmission Control Protocol)Ensuring the data is transported correctly and can be reassembled at the destination.
NetworkIP (Internet Protocol)Routing the data to the correct device (computer/machine) using the IP address.

As software engineers/application developers, we primarily interact with the Application Layer and can largely take the details of the IP and TCP protocols for granted.


🌐 Public vs. Private IP Addresses

  • Public IP Address: Required for a server (or a router) to be publicly accessible over the internet.
  • Private IP Address: Used internally within a Local Area Network (LAN), such as a home network.
    • How it Works: Multiple devices (clients) connect to a single router.
    • Addressing: Only the router needs a public IP address. The router then assigns each connected device a private IP address (often via DHCP).
    • External Access : The router uses Network Address Translation (NAT) to allow all internal devices to share its single public IP address when communicating with the external internet. It rewrites the source IP of outgoing packets and tracks the connections to send returning data to the correct private device.
    • Accessibility: Private IP addresses are not directly accessible from the public internet.

Localhost: The Loopback Address

  • Special IP: The IP address 127.0.0.1 is a reserved address that points to your local machine.
  • Alias: localhost is a special name for this IP address, used to access a server running on your own computer.

Of course, here is the consolidated and revised English text for the “Ports: Channels of Communication” section, including the accurate rule and the underlying mechanism.

🚪 Ports: Channels of Communication

  • Concept: A port acts as a specific channel of communication on a machine. Communication is specified by an IP address and a port number.
  • Size/Capacity: Ports are a 16-bit value, allowing for about 65,536 distinct channels (from $0$ to $65,535$).
  • Default Ports: Many application protocols have a specified default port:
    • HTTP (unsecure web): Port 80
    • HTTPS (secure web): Port 443
    • Implicit Use: When accessing a website like google.com via HTTPS, you don’t need to type :443 because the protocol uses the default port implicitly.

🔒 Port Usage Rule and Sockets

  • Port Usage Rule: A specific port number on a specific IP address can only be bound by a single process at any given time. This means if one application’s process is actively listening on a port (e.g., localhost:4200), no other process can bind to and serve traffic on that identical address and port simultaneously.
  • Multiple Ports: A single process can, however, listen on multiple ports simultaneously by binding different sockets to different port numbers.
  • Underlying Mechanism: The operating system uses sockets (an endpoint for communication) to manage these connections. An established connection socket is uniquely identified by a 5-tuple combination of Protocol, Local IP Address, Local Port, Remote IP Address, and Remote Port.