Computer Networks Essentials / Transport Layer

Ports & Sockets

๐Ÿค” Why Do We Need Ports & Sockets? (Basic Idea)

A single computer can run many applications at the same time:

  • ๐ŸŒ Web browser

  • ๐Ÿ“ง Email client

  • ๐ŸŽฎ Game

  • ๐Ÿ“‚ File transfer

๐Ÿ“Œ All of them use the network simultaneously.

๐Ÿ‘‰ Question:

How does the OS know which data belongs to which application?

๐Ÿ’ก Answer: Ports & Sockets


๐Ÿง  Simple Definitions

๐Ÿ”ข Port

๐Ÿ“Œ A port is a logical number used by the Transport Layer to identify a specific application or service on a device.

๐Ÿ”Œ Socket

๐Ÿ“Œ A socket is a combination of IP address and port number that uniquely identifies an endpoint of communication.


๐Ÿง  Real-Life Analogy

๐Ÿข Apartment Building Example

  • Building address ๐Ÿ  โ†’ IP address

  • Flat number ๐Ÿšช โ†’ Port number

  • Full address (Building + Flat) โ†’ Socket

๐Ÿ‘‰ Parcel reaches right flat, not just the building.


๐ŸŒ Role of Ports in Transport Layer

Ports enable:

  • ๐Ÿ“ฆ Application-to-application communication

  • ๐Ÿ”€ Multiplexing (many apps sending data)

  • ๐Ÿ” Demultiplexing (delivering data to correct app)

๐Ÿ“Œ Without ports, Transport Layer cannot work.


๐Ÿ”ข Port Number Basics

  • Port numbers are 16-bit numbers

  • Range: 0 to 65535

๐Ÿ“Œ Total possible ports = 65,536


๐Ÿงฉ Types of Port Numbers

Ports are divided into three categories:

๐ŸŸข 1. Well-Known Ports (0 โ€“ 1023)

๐Ÿ“Œ Reserved for standard services

Service

Port

HTTP

80

HTTPS

443

FTP

21

SMTP

25

DNS

53

SSH

22

๐Ÿ“Œ Managed by IANA

๐Ÿ“Œ Cannot be freely used by normal applications


๐Ÿ”ต 2. Registered Ports (1024 โ€“ 49151)

๐Ÿ“Œ Used by specific applications but not system-critical

Examples:

  • MySQL โ†’ 3306

  • PostgreSQL โ†’ 5432

๐Ÿ“Œ Can be registered officially.


๐ŸŸก 3. Dynamic / Ephemeral Ports (49152 โ€“ 65535)

๐Ÿ“Œ Used temporarily by client applications

Example:

  • Browser opens a random port to connect to server

๐Ÿ“Œ Automatically assigned by OS.


๐Ÿง  Clientโ€“Server Communication Using Ports

๐Ÿง  Example: Web Browsing

  • Client:

    • IP: 192.168.1.10

    • Port: 52345 (dynamic)

  • Server:

    • IP: 142.250.195.46

    • Port: 80 (HTTP)

๐Ÿ‘‰ Communication happens smoothly because of ports.


๐Ÿ”€ Multiplexing & Demultiplexing

๐Ÿ“ค Multiplexing

  • Multiple applications send data

  • Transport Layer combines data streams

  • Uses different port numbers

๐Ÿ“ฅ Demultiplexing

  • Receiver gets data

  • Uses port number

  • Delivers data to correct application

๐Ÿ“Œ Ports make this possible.


๐Ÿ”Œ What is a Socket? (Deep Understanding)

๐Ÿง  Socket = IP Address + Port Number

๐Ÿ“Œ Example:

192.168.1.10 :8080

๐Ÿ‘‰ This combination uniquely identifies:

  • A device

  • A specific application on that device


๐Ÿง  Socket Pair (Very Important โญ)

A complete communication is identified by:

(Source IP,Source Port, Destination IP, Destination Port)

๐Ÿ“Œ This is called a Socket Pair or 4-tuple.


๐Ÿง  Example

Field

Value

Source IP

192.168.1.10

Source Port

52345

Destination IP

142.250.195.46

Destination Port

80

๐Ÿ‘‰ This uniquely identifies one TCP connection.


๐Ÿง  Types of Sockets

๐Ÿ”น TCP Socket

  • Reliable

  • Connection-oriented

  • Uses TCP protocol

๐Ÿ“Œ Used for:

  • Web

  • Email

  • File transfer

๐Ÿ”น UDP Socket

  • Fast

  • Connectionless

  • Uses UDP protocol

๐Ÿ“Œ Used for:

  • Streaming

  • Gaming

  • DNS


๐Ÿง  Ports in TCP vs UDP

Feature

TCP

UDP

Uses Ports

Yes

Yes

Connection

Yes

No

Reliability

Guaranteed

Not guaranteed

๐Ÿ“Œ Port numbers exist in both TCP & UDP headers.


๐Ÿ“ฆ Port Numbers in TCP Header (Preview)

TCP Header contains:

  • Source Port (16 bits)

  • Destination Port (16 bits)

๐Ÿ‘‰ Same for UDP (simpler header).


โš ๏ธ Why Port Numbers Are Important

Without ports:

  • Multiple applications cannot run simultaneously โŒ

  • Data reaches device but not application โŒ

  • Internet services fail โŒ

๐Ÿ‘‰ Ports are essential for Transport Layer.


โœ๏ธ Definitions

๐Ÿ“Œ Port:

A port is a logical address used by the Transport Layer to identify an application or service.

๐Ÿ“Œ Socket:

A socket is an endpoint of communication identified by an IP address and a port number.


โญ Memory Tricks

  • IP = House address

  • Port = Door number

  • Socket = Full address


โญ One-Line Summary

๐Ÿ”น Ports identify applications, and sockets uniquely identify communication endpoints in Transport Layer communication.