LearnViz CompSci
Home / Learn / Computer Networks / Ports and Sockets

Beginner Topic 16 of 50 Computer Networks

Ports and Sockets

Endpoints identifying services and connections on hosts.

How it works

A port is a 16-bit number that identifies a specific service or conversation on a host, so many applications can share one IP address. Well-known ports include 80 for HTTP, 443 for HTTPS, 22 for SSH and 53 for DNS. A socket is the endpoint of a connection, identified by protocol, IP address and port; a TCP connection is uniquely defined by the pair of source and destination sockets. The sockets API provides socket, bind, listen, accept, connect, send and recv.

Step-by-step walkthrough

  1. A host runs many network services at once.
  2. Ports tell the OS which service gets the data.
  3. Port 443 reaches the web service.
  4. Port 25 reaches the mail service.
  5. A connection is identified by source IP and port plus destination IP and port, called a socket pair.

Where it is used

Servers listen on ports, clients choose ephemeral ports, and firewalls and NAT use port numbers to filter and map traffic.

Common pitfall

Binding to an in-use port fails with address-already-in-use, often from lingering TIME_WAIT. Ports below 1024 need privileges on Unix. Open unnecessary ports increase attack surface, so close them. Forgetting to read all bytes from a stream socket, which has no message boundaries, is a classic bug.

Where it fits in the course

Ports and Sockets is topic 16 of 50 in Computer Networks, rated Beginner. It follows HTTP. The next topic is Packet Switching.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Computer Networks quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Ports and Sockets?
Endpoints identifying services and connections on hosts. It belongs to Computer Networks and is a beginner-level topic.
Where do people use Ports and Sockets?
Servers listen on ports, clients choose ephemeral ports, and firewalls and NAT use port numbers to filter and map traffic.
What is the most common mistake with Ports and Sockets?
Binding to an in-use port fails with address-already-in-use, often from lingering TIME_WAIT.
What should I learn before this topic?
Start with HTTP, the topic just before it in Computer Networks.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Computer Networks quiz.
Is Ports and Sockets hard to learn?
It is a Beginner topic, so it works as an entry point. If the terms are new, read the explanation twice and try the visualizer before the quiz.
How long does it take to learn Ports and Sockets?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Ports and Sockets?
HTTP, Packet Switching, DHCP are closely connected. The full list is in the related topics section of this page.
What should I study after Ports and Sockets?
Continue with Packet Switching, the next topic in Computer Networks.

Related topics