No history yet

Introduction to Sockets

The Internet's Mail System

Think about sending a letter. To make sure it reaches the right person, you need two things: a street address to find the right building, and a name or apartment number to find the right person inside. The internet works in a very similar way.

When two computers want to talk to each other, they need a clear, defined endpoint for sending and receiving data. This endpoint is called a socket. A socket is like one end of a two-way phone conversation. It’s a combination of an IP address and a port number, creating a specific address for a particular application running on a specific machine.

Essentially, a socket is a doorway for information to flow between programs over a network. Without sockets, your web browser couldn't fetch a webpage, and your email client couldn't download new messages.

Anatomy of a Socket

Every socket is defined by two key pieces of information.

IP Address

noun

A unique string of numbers that identifies a specific computer on a network.

The IP address is the street address. It gets the data to the correct device, whether it's a massive web server or your personal laptop. It answers the question, "Which computer am I sending this to?"

Port Number

noun

A number that identifies a specific process or application on a computer.

The port number is the apartment number. A single computer can run many different network applications at once: a web browser, a mail app, a streaming service, and more. The port number tells the computer's operating system which specific application should receive the incoming data. It answers, "Which program on this computer gets this message?"

Together, the IP address and port number create a unique destination. For example, connecting to 172.217.14.238 on port 443 gets you to one of Google's web servers, specifically the process handling secure web traffic.

Sockets in Action

Let's walk through a common scenario: visiting a website. When you type a URL into your browser and hit Enter, a series of socket interactions happens behind the scenes.

  1. Your computer (the client) creates a socket for your web browser.
  2. It looks up the IP address of the website's server using the Domain Name System (DNS).
  3. Your browser then uses its socket to send a connection request to the server's IP address on a specific port, usually port 80 for standard web traffic (HTTP) or 443 for secure traffic (HTTPS).
  4. The web server, which is always listening for incoming connections on that port, accepts the request. This establishes a connection between the two sockets.
  5. Data can now flow back and forth. Your browser sends a request for the webpage, and the server sends the webpage's data back.

This client-server communication model is the foundation of how most of the internet works, from browsing and email to online gaming.

Once the communication is finished, the connection is closed, and the sockets are released. Every time you click a link or load a new page, this process repeats, creating new connections to fetch the necessary information.

Time for a quick check on what we've learned.

Quiz Questions 1/5

What two components are combined to create a network socket?

Quiz Questions 2/5

In the analogy of sending a letter, if an IP address is the street address of an apartment building, what is the port number?

Sockets are a fundamental concept in networking. By providing a clear endpoint for communication, they allow countless different applications to use the internet simultaneously and reliably.