Monday, November 30, 2026 Independent journalism
MediaChannel

technology

What is a socket and how does it connect your apps to the internet?

A socket is the low-level building block that lets every networked app, from your email client to your browser, actually exchange data with another machine. Here's how they work and why they matter.

Numerous wires and cables mounted into server patch panel in modern data center

Photo by Brett Sayles on Pexels

When you send a message in a chat app or load a webpage, your device doesn't just fire data into the air and hope for the best. It opens a socket. A socket is the programming interface that connects one endpoint to another across a network, giving your application a channel to send and receive data. Without sockets, almost nothing on the modern internet works.

The concept sounds abstract, but it's genuinely simple. Think of a socket as a telephone handset. Before a call, the handset exists but nothing moves. Once you dial and the other end picks up, you have a live two-way channel. A socket works the same way: it's the endpoint that opens the channel, not the data itself.

What a socket actually is

A socket is a software object created by an application and managed by the operating system's kernel. It sits at the intersection of your application code and the networking hardware underneath. When you create a socket, the OS assigns it a file descriptor — a number your program uses to refer to it, just like a file handle when reading a document from disk.

Every socket has three identifying properties: an IP address, a port number, and a transport protocol. The IP address says which machine is involved. The port says which application or service on that machine is listening. The transport protocol, most commonly TCP or UDP, defines the rules for how data moves across the connection.

A web server, for instance, typically listens on port 443 for HTTPS traffic. When your browser wants to load a page, it creates a socket on its own end, picks an unused high-numbered port (say, 54,322), and connects to the server's socket at port 443. Those two sockets, one on your machine and one on the server, form a connected pair. Data flows between them until either side closes the connection.

TCP sockets vs UDP sockets

TCP sockets and UDP sockets behave very differently, and choosing between them has real consequences for how an application performs.

A TCP socket establishes a formal connection before any data moves. The client and server perform a three-step handshake: the client sends a SYN packet, the server replies with SYN-ACK, and the client confirms with ACK. Only then does data flow. TCP also guarantees delivery and order: if a packet is lost, TCP detects it and retransmits. Your browser uses TCP sockets because a web page with a missing chunk is broken.

A UDP socket skips the handshake entirely. It fires packets and doesn't check whether they arrived. That sounds worse, but for some applications it's the right choice. Online games and live video calls use UDP sockets because a dropped frame is less disruptive than the half-second stall TCP introduces while it waits for a retransmit. Speed beats completeness.

How a server socket works differently from a client socket

Servers and clients use sockets in different modes, and understanding the distinction clears up a lot of confusion.

A server creates a "listening socket" and binds it to a specific port. The OS puts it into listen mode and queues incoming connection requests. When a client connects, the OS creates a brand-new socket specifically for that conversation. The original listening socket stays open, ready for the next client. This is why a single web server can handle thousands of simultaneous connections: each one gets its own dedicated socket pair, while the listening socket keeps accepting new arrivals.

A client socket works in the opposite direction. The client calls connect(), targeting the server's IP and port. If the server accepts, the client's socket transitions from "unconnected" to "established" and data can flow. This understanding is what powers technologies like WebSockets, which keep that established connection open permanently rather than closing it after each request.

Blocking vs non-blocking sockets

By default, a socket is blocking. That means if your program calls recv() to read incoming data and nothing has arrived yet, the entire program pauses and waits. For simple scripts, that's fine. For a server handling thousands of clients, it's catastrophic: one slow client would freeze everything.

Non-blocking sockets solve this. Set to non-blocking mode, a recv() call returns immediately if no data is available, letting the program check other sockets instead of stalling. Modern servers use an event loop — a mechanism that monitors hundreds or thousands of sockets simultaneously and fires a callback only when data actually arrives. Node.js and Python's asyncio are built on exactly this pattern.

The kernel-level tool that makes this efficient is called epoll on Linux (or kqueue on macOS). Instead of the program polling every socket in a loop, it registers interest with the kernel. The kernel signals the program only when something needs attention. It's the same principle as the interrupt mechanism your CPU uses to avoid busy-waiting on hardware: rather than constantly checking, you wait for a signal.

Unix domain sockets: the sockets you don't see

Not every socket crosses a network. Unix domain sockets connect processes on the same machine, using the filesystem as an address instead of an IP and port. They're faster than TCP sockets for local communication because no network stack is involved: data moves directly through kernel memory.

PostgreSQL, Redis, and Nginx all use Unix domain sockets by default when a client and server share the same host. If you've ever connected a web app to a local database without specifying a hostname, you were probably using a Unix domain socket without realising it.

What happens when a socket closes

Closing a socket isn't always clean. TCP connections go through a four-step teardown: each side sends a FIN packet and waits for the other to acknowledge. This means both sides have to agree the conversation is over before the connection fully releases. A socket in the TIME_WAIT state is a closed connection that hasn't yet released its port, because the OS holds it briefly to catch any late-arriving packets. Under heavy load, running out of available ports due to TIME_WAIT sockets is a real bottleneck that systems engineers actively tune for.

UDP sockets just close. No teardown, no handshake in reverse. The application says it's done and the socket disappears.

Sockets in the broader stack

Sockets sit directly above the transport layer in the network model. Everything higher, including HTTP, TLS, WebRTC, and DNS, is built on top of them. When your browser fetches a page over HTTPS, it opens a TCP socket, negotiates a TLS session on top of it, then sends HTTP requests through that encrypted channel. The socket is the foundation every other protocol stands on.

Understanding sockets makes the rest of the networking stack far easier to follow. Once you see that a TCP/IP stack is really just a set of rules layered on top of these basic read/write endpoints, the abstractions above start to make intuitive sense. A socket isn't a complex piece of infrastructure. It's a file your program opens. Everything else is built from there.