100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Network Socket vs a Port?

Learn the difference between a port and a socket, the 5-tuple that identifies a connection, and how servers handle many clients.

mediumQ179 of 224 in Computer Networks Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A port is just a 16-bit number (0-65535) identifying an application or service on a host, while a socket is the full combination of protocol, source IP, source port, destination IP, and destination port that uniquely identifies one specific end-to-end connection β€” a port names a door, a socket names a specific conversation happening through it.

A port number by itself only tells you which application on a host should receive traffic β€” port 443 conventionally means HTTPS, port 22 means SSH β€” but it says nothing about who the traffic is coming from or which specific ongoing exchange it belongs to. A socket, formally the 5-tuple (protocol, local IP, local port, remote IP, remote port), is what the operating system actually uses to demultiplex incoming packets to the correct process and, for TCP, to track connection state. This is why a server listening on port 443 can serve thousands of simultaneous clients: every client connects from a different source IP/port combination, so each one forms a distinct socket even though they all share the same destination port 443. In code, a program calls socket() to create the endpoint, then bind() to associate it with a local IP/port, and connect() or accept() to complete the other end of the 5-tuple β€” at that point the socket is fully specified and the OS can route bytes to and from that specific connection.

  • A port identifies which application/service on a host should get the traffic
  • A socket (5-tuple) uniquely identifies one specific end-to-end connection
  • One listening port can serve many simultaneous distinct client sockets
  • The OS uses the socket 5-tuple, not just the port, to demultiplex incoming packets

AI Mentor Explanation

A port is like the number painted on a dressing-room door at the ground β€” it tells you which room to knock on, say door 22 for the visiting team. A socket is the full booking record: this specific door, on this specific day, for this specific visiting team arriving from this specific city, matched against the home team already inside. Many different visiting teams can use door 22 on different days, and each visit is a distinct booking even though the door number never changes. That is exactly why one server port can serve thousands of separate client connections at once, each identified by its full socket, not just the shared port number.

Step-by-Step Explanation

  1. Step 1

    Port identifies a service

    A 16-bit port number (e.g., 443) tells the OS which application or listening process should receive traffic.

  2. Step 2

    Create the socket

    The application calls socket() to create an endpoint and bind() to attach it to a local IP and port.

  3. Step 3

    Complete the 5-tuple

    A client connect() or a server accept() fills in the remote IP/port, forming the full (protocol, local IP, local port, remote IP, remote port) socket.

  4. Step 4

    OS demultiplexes by socket

    Incoming packets are matched to the correct connection using the full 5-tuple, letting one port serve many simultaneous clients.

What Interviewer Expects

  • Clearly distinguishes a port (a number) from a socket (a 5-tuple identifying a connection)
  • Explains how one listening port serves many simultaneous clients via distinct sockets
  • Names the 5-tuple: protocol, local IP, local port, remote IP, remote port
  • Knows the socket()/bind()/connect()/accept() lifecycle at a high level

Common Mistakes

  • Using β€œport” and β€œsocket” interchangeably
  • Thinking a server can only handle one client per listening port
  • Forgetting protocol (TCP vs UDP) is part of what makes a socket unique
  • Confusing a socket file (Unix domain socket) with a network socket 5-tuple

Best Answer (HR Friendly)

β€œA port is just a number that tells a computer which application should handle incoming traffic, like door number 443 for secure web traffic. A socket is the complete, specific conversation happening through that door β€” it includes who you are, who they are, and both port numbers involved β€” which is why one web server on port 443 can talk to thousands of different visitors at the same time without mixing up their traffic.”

Code Example

Creating a TCP socket bound to a port (Python)
import socket

# Create a TCP socket and bind it to local port 8080
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.bind(("0.0.0.0", 8080))
srv.listen(5)

conn, addr = srv.accept()
# 'conn' is now a fully specified socket: local (host, 8080)
# paired with the remote (addr[0], addr[1]) -- the 5-tuple
print(f"Accepted connection from {addr}, socket fd={conn.fileno()}")

Follow-up Questions

  • Why can a server accept many clients on the same listening port?
  • What is the difference between a well-known port, a registered port, and an ephemeral port?
  • How do TCP and UDP sockets differ in connection state tracking?
  • What does the SO_REUSEADDR socket option actually control?

MCQ Practice

1. What is a port number by itself?

A port is just a 16-bit number identifying which service should receive traffic; it does not by itself identify a connection.

2. What uniquely identifies a specific network connection (a socket)?

A socket is defined by the full 5-tuple, which is why many clients can share the same destination port.

3. Why can a web server on port 443 serve thousands of clients simultaneously?

Even though all clients connect to the same destination port, each has a unique source IP/port, forming a distinct 5-tuple socket.

Flash Cards

What is a port? β€” A 16-bit number identifying an application or service on a host, e.g. 443 for HTTPS.

What is a socket? β€” The full 5-tuple (protocol, local IP, local port, remote IP, remote port) uniquely identifying one connection.

How does one port serve many clients? β€” Each client has a distinct source IP/port, so each forms a different socket even on the same destination port.

What socket calls set up a connection? β€” socket() creates the endpoint, bind() attaches a local address, then connect()/accept() completes the 5-tuple.

1 / 4

Continue Learning