Skip to content
BoKSA

04 – The Application Layer: Architectures, Sockets and Port Numbers

04 – The Application Layer: Architectures, Sockets and Port Numbers

Course: ICT Basic course · Level: introductory Book: Kurose & Ross, 9th ed., chapter 2.1 (principles of network applications) and 3.2 (multiplexing and demultiplexing)

Learning goals

After this article you can:

  • compare the client-server and peer-to-peer (P2P) architectures;
  • explain what an application-layer protocol defines;
  • explain how processes communicate through sockets, identified by IP address + port number;
  • name well-known port numbers and explain the port ranges;
  • explain which transport services (TCP, UDP, QUIC) an application can choose and why.

1. Network applications

A network application consists of programs running on different end systems that talk to each other over the network: a browser and a web server, a game client and a game server, two video-call apps.

You only write software for the end systems. Routers in the network core don't run your application. This is why new Internet applications (from the web to ChatGPT-style services) could be developed and spread so quickly.


2. Application architectures

2.1 Client-server

1
2
3
   [client] ──request──►  ┌────────┐
   [client] ──request──►  │ server │  always on, fixed IP address / domain name
   [client] ◄─response──  └────────┘  often in a data centre
  • Server: always on, permanent (well-known) address, often many servers in data centres for scale.
  • Clients: start the conversation, can have changing (dynamic) IP addresses, and don't talk directly to each other.
  • Examples: web (HTTP), e-mail, online banking, most cloud apps, streaming services.

Today: a big "server" is usually thousands of machines behind a load balancer, often in the cloud (AWS, Azure, Google Cloud) and with a CDN in front.

2.2 Peer-to-peer (P2P)

1
2
3
   [peer] ⇄ [peer]
     ⇅   ╲  ⇅
   [peer] ⇄ [peer]
  • No always-on central server: peers talk directly to each other.
  • Self-scalability: every new peer brings demand and capacity (upload bandwidth).
  • Harder to manage: peers come and go, have dynamic addresses and are often behind NAT.
  • Examples: BitTorrent, some blockchain networks, parts of WebRTC video calls, local file sharing (AirDrop-like). See article 07.

2.3 Hybrid

Many applications mix both. In a WebRTC video call, a server handles sign-in and finding the other person, but the audio/video stream goes directly between the peers if possible.


3. What does an application-layer protocol define?

Aspect Example (HTTP)
Types of messages request, response
Syntax – which fields, how they are separated GET /index.html HTTP/1.1 + header lines
Semantics – meaning of fields status code 404 means "not found"
Rules – when and how to send/respond client sends request first, server responds
  • Open protocols are published (e.g. in RFCs), so anyone can build compatible software: HTTP, SMTP, DNS, SSH.
  • Proprietary protocols are owned by a company: e.g. the internal protocols of WhatsApp, Zoom or many game platforms.

4. Processes, sockets and addressing

A process is a running program. Processes on different hosts communicate by exchanging messages through sockets.

1
2
3
4
5
6
   Host A (client)                                     Host B (server)
 ┌──────────────┐                                    ┌──────────────┐
 │ browser      │                                    │ web server   │
 │   socket ◄───┼── 145.92.10.7 : 51734  ⇄  93.184.215.14 : 443 ─►│   socket     │
 │ TCP/IP (OS)  │                                    │ TCP/IP (OS)  │
 └──────────────┘                                    └──────────────┘

To reach the right process you need two things:

  1. the IP address of the host (which computer?);
  2. the port number (which process on that computer?). A port number is 16 bits: 0–65535.

4.1 Port number ranges

Range Name Used for
0 – 1023 Well-known ports standard services (need admin/root rights to open on most OSes)
1024 – 49151 Registered ports registered with IANA by applications (e.g. 3306 MySQL)
49152 – 65535 Dynamic / ephemeral ports chosen randomly by the OS for the client side of a connection

4.2 Well-known ports you should know

Port Protocol Transport Note
20/21 FTP TCP legacy, unencrypted; use SFTP/HTTPS
22 SSH / SFTP TCP secure remote login
23 Telnet TCP legacy, unencrypted; only for testing
25 SMTP TCP mail between servers
53 DNS UDP + TCP
67/68 DHCP UDP server 67, client 68
80 HTTP TCP
110 / 143 POP3 / IMAP TCP unencrypted versions; use 995 / 993
123 NTP UDP time synchronisation
443 HTTPS TCP and UDP UDP 443 = HTTP/3 over QUIC
587 / 465 SMTP submission TCP mail from client to server (with TLS)
853 DNS over TLS TCP
993 / 995 IMAPS / POP3S TCP encrypted mailbox access
3389 RDP TCP/UDP Windows Remote Desktop
3306 / 5432 MySQL / PostgreSQL TCP should never be open to the Internet

Full list: IANA Service Name and Transport Protocol Port Number Registry.

4.3 Multiplexing and demultiplexing

  • Multiplexing (sender): the transport layer collects data from many sockets and adds a header with source and destination port.
  • Demultiplexing (receiver): the transport layer looks at the port numbers and hands the data to the right socket.
Transport A socket is identified by
UDP (connectionless) destination IP + destination port (2-tuple)
TCP (connection-oriented) source IP, source port, destination IP, destination port (4-tuple)

That's how one web server on port 443 can serve thousands of clients at the same time: every connection has a different 4-tuple.

Example: Host A (port 37) sends to Host B (port 61). In the replies from B to A, the ports are swapped: source port 61, destination port 37.


5. Which transport service does an application need?

Need Example
Reliable data transfer (no loss) file transfer, web pages, e-mail, banking
Low delay / timing gaming, video calls, VoIP
Throughput video streaming
Security (encryption, integrity) basically everything today
Service TCP UDP QUIC (on top of UDP)
Reliable delivery ✔ ✘ ✔
In-order delivery ✔ (one stream) ✘ ✔ per stream (multiple streams)
Flow and congestion control ✔ ✘ ✔
Connection setup 3-way handshake none 1 RTT including encryption (0-RTT on resume)
Built-in encryption ✘ (add TLS) ✘ (add DTLS) ✔ always TLS 1.3
Used by HTTP/1.1, HTTP/2, SMTP, SSH DNS, VoIP, games, streaming (RTP) HTTP/3, DNS over QUIC

Details: article 09 (TCP and UDP) and article 05 (HTTP/3).


6. Try it yourself

  1. On Linux: ss -tunp. Which connections are open, and which local (ephemeral) ports does your browser/system use? On Windows: netstat -ano.
  2. Open two terminals. In the first: nc -l 5000 (listen on port 5000). In the second: nc localhost 5000. Type text: you just built a client-server application!
  3. Check which ports are open on your own VM: ss -tulpn. Which processes are listening?

Check your understanding

  1. Give two differences between client-server and P2P.
  2. Why does a client usually get a random (ephemeral) port?
  3. Which four values identify a TCP connection?
  4. Which port numbers do HTTP, HTTPS, DNS and SSH use?
  5. Why would a game developer choose UDP instead of TCP?

References

Book

  • Kurose & Ross, 9th ed., chapter 2.1, chapter 2.7 (socket programming), chapter 3.2 (multiplexing/demultiplexing)

Videos

Articles and standards


Cyber Security

Why is this important for cyber security?

  • Every open port is a door. Attackers use port scanners (e.g. nmap, or Internet-wide scanners like Shodan and Censys) to find open ports and the software behind them. An exposed database (port 3306/5432/27017) or RDP (3389) is one of the most common causes of data breaches and ransomware.
  • Legacy, unencrypted protocols leak data. Telnet, FTP, HTTP, POP3 and IMAP without TLS send passwords in plain text. Anyone on the same network (or an ISP, or a malicious Wi-Fi hotspot) can read them with Wireshark.
  • Client-server = attractive central target. One server holds the data of millions of users, and if it goes down, the whole service is down (DDoS).
  • P2P = trust problem. You don't know who the other peers are; they may send malware or see your IP address.
  • Port numbers are not security. Moving SSH from port 22 to 2222 reduces noise in your logs, but it doesn't stop a real attacker ("security through obscurity").

What can you do to make it safe?

  • Minimise exposure: only open the ports you really need, close the rest with a firewall, and check with ss -tulpn (local) or nmap (from outside, only on your own systems).
  • Never expose management interfaces or databases (RDP, SSH, MySQL, admin panels) directly to the Internet. Put them behind a VPN or a zero-trust access gateway.
  • Replace legacy protocols with secure alternatives:
Insecure Secure alternative
Telnet (23) SSH (22)
FTP (21) SFTP (22) / HTTPS
HTTP (80) HTTPS (443)
POP3/IMAP (110/143) POP3S/IMAPS (995/993)
DNS (53, plain) DNS over HTTPS / TLS (443/853)
  • Validate all input in your own applications: a network application gets data from unknown, possibly malicious clients (see the OWASP Top 10).
  • Keep server software up to date: most attacks target known vulnerabilities in exposed services.

Extra references