What Is a Network Socket? Socket vs. Port Explained

Sockets and ports get confused constantly -- here's the difference, built up step by step.

  1. What a socket actually is

    A socket is the combination of an IP address, a port number, and a protocol (TCP or UDP) -- together they form one endpoint of a network connection. Using a phone analogy: the IP address is a building's street address, the port number is an extension inside that building, and the socket is the specific line that's actually connected for a call.

  2. How a port differs from a socket

    A port number is just a single number (0-65535) indicating which service to reach. A socket is more specific: it combines that port number with an actual IP address and protocol to form one concrete connection. The same port 80 produces a different socket for every client IP that connects, which is exactly how a web server can serve many people at once on the same port.

  3. Socket pairs identify each connection

    A single TCP connection is uniquely identified by a socket pair -- the combination of your own IP:port and the remote IP:port. That's how a server can accept thousands of simultaneous connections on the same port 80 and still know exactly which incoming data belongs to which client.

  4. TCP sockets vs UDP sockets

    A TCP socket establishes a connection first (a three-way handshake, like placing a phone call) and guarantees data arrives in order, while a UDP socket sends data immediately without setting up a connection first, more like dropping a postcard in the mail. Applications where accuracy matters, like loading a website, tend to use TCP; applications where speed matters more, like live gaming or video calls, often use UDP.

  5. Well-known ports vs. ephemeral ports

    Ports like 80 (HTTP) and 443 (HTTPS) are "well-known ports" that servers use by convention so clients know where to connect. The port your own device uses when connecting out to a server, by contrast, is an ephemeral port automatically assigned by the operating system (typically in the 49152-65535 range) -- not something you need to manage yourself.

  6. How a socket address is written

    A socket address is commonly written as "IP address:port number." For example, 192.168.0.10:8080 refers to port 8080 on the device at 192.168.0.10. You'll see this notation often in server configuration files and developer tools.

  7. Listening sockets vs. established sockets

    A server program waiting for new connections on a given port holds a listening socket, while a socket that has an active, ongoing exchange of data with a client is an established socket. Network diagnostic tools like netstat let you see directly which sockets are currently listening and which are established.

  8. What a firewall actually controls is sockets

    When a firewall rule "blocks a port," what it's really doing is preventing new socket connections from being made on that port. Once you understand sockets, concepts like port forwarding, firewall rules, and proxy configuration all click into place as variations on the same underlying idea.

WebSocket is a different thing, despite the similar name

Despite the name, "WebSocket" isn't the operating-system-level socket described on this page -- it's a separate application-layer protocol built specifically so a browser and a server can hold open a persistent, two-way connection, commonly used for chat apps and live updates. A WebSocket connection is actually built on top of an ordinary TCP socket underneath.

Sockets, firewalls, and port forwarding are the same idea from different angles

A firewall rule that blocks a port is really preventing new sockets from forming on that port, and port forwarding works by redirecting incoming socket connections on a router to a specific device inside the network. Once the socket concept clicks, these related networking topics stop feeling like separate rules to memorize.

Frequently Asked Questions

Do I ever need to choose my own socket number?

No. A server's well-known ports (like 80 or 443) are set by the program or an administrator, but the temporary port your device uses as a client is assigned automatically by the operating system, so there's nothing for you to configure.

Can a server run out of sockets?

Yes -- a server that opens and closes a very large number of connections in a short time can temporarily run out of available ephemeral ports, a condition sometimes called socket exhaustion. This is often mitigated by tuning connection reuse (keep-alive) settings on the server.