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
- A host runs many network services at once.
- Ports tell the OS which service gets the data.
- Port 443 reaches the web service.
- Port 25 reaches the mail service.
- 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
- Open the interactive lesson, with a mini quiz.
- Take the Computer Networks quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- 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?
Where do people use Ports and Sockets?
What is the most common mistake with Ports and Sockets?
What should I learn before this topic?
How can I practice it?
Is Ports and Sockets hard to learn?
How long does it take to learn Ports and Sockets?
Which topics are related to Ports and Sockets?
What should I study after Ports and Sockets?
Related topics
- HTTP (Computer Networks)
- Packet Switching (Computer Networks)
- DHCP (Computer Networks)
- Wi-Fi (Computer Networks)