Ports and Protocols (TCP, UDP) | Cyber Security Tutorial - Learn with VOKS
Back Next

Ports and Protocols (TCP, UDP)


Introduction to ports and protocols (tcp and udp)

when computers communicate over a network (like the internet), they need rules and identifiers to make sure data goes to the correct place and is understood properly. these rules are called protocols, and the identifiers are called ports.

to understand this better, imagine sending a letter:

  • the ip address is like the street address of a house.
  • the port number is like the specific apartment number inside the building.
  • the protocol is the language and delivery method used to send the letter.

in networking, two of the most important protocols are tcp and udp.

What is a port?

a port is a number used by a computer to identify a specific program or service running on it.

when data arrives at a computer, the operating system looks at the port number to decide which application should receive the data.

for example:

  • port 80 is usually used for http (websites).
  • port 443 is used for https (secure websites).
  • port 22 is used for ssh.
  • port 25 is used for email (smtp).

ports range from 0 to 65535 and are divided into:

  1. well-known ports (0–1023) – used by common services.
  2. registered ports (1024–49151) – used by specific applications.
  3. dynamic/private ports (49152–65535) – used temporarily by clients.

What is a protocol?

a protocol is a set of rules that defines how data is sent and received over a network.

two of the main transport layer protocols (from the tcp/ip model) are:

  • transmission control protocol (tcp)
  • user datagram protocol (udp)

both work on top of ip (internet protocol), which handles addressing and routing.

TCP (transmission control protocol)

tcp is a connection-oriented protocol. this means:

before sending data, a connection must be established between the sender and receiver.

this happens using something called the three-way handshake:

  1. client sends: syn
  2. server replies: syn-ack
  3. client responds: ack

after this, the connection is established and data can be transferred.

features of tcp:

  • reliable: guarantees delivery.
  • ordered: packets arrive in the correct order.
  • error-checked: detects and retransmits lost data.
  • flow control: prevents overwhelming the receiver.

advantages:

  • safe and reliable.
  • good for important data.

disadvantages:

  • slower due to overhead and confirmation steps.

examples of tcp usage:

  • web browsing (http, https)
  • email
  • file transfers (ftp)
  • remote login (ssh)

example tcp server in python:

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("127.0.0.1", 5000))
server.listen(1)

print("server is listening...")

conn, addr = server.accept()
print("connected to", addr)

data = conn.recv(1024)
print("received:", data.decode())

conn.sendall(b"hello from server")
conn.close()

example tcp client in python:

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 5000))

client.sendall(b"hello from client")
data = client.recv(1024)

print("received:", data.decode())
client.close()

UDP (user datagram protocol)

udp is a connectionless protocol. this means:

  • no connection setup.
  • data is sent immediately.
  • no guarantee of delivery.
  • no ordering guarantee.
  • no retransmission of lost packets.

udp simply sends packets (called datagrams) without checking if they arrive.

features of udp:

  • faster than tcp.
  • low overhead.
  • no connection handshake.

advantages:

  • very fast.
  • good for real-time communication.

disadvantages:

  • unreliable.
  • packets may be lost or arrive out of order.

examples of udp usage:

  • online gaming
  • video streaming
  • voice calls (voip)
  • dns

example udp server in python:

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(("127.0.0.1", 6000))

print("udp server is listening...")

data, addr = server.recvfrom(1024)
print("received:", data.decode())

server.sendto(b"hello from udp server", addr)

example udp client in python:

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

client.sendto(b"hello from udp client", ("127.0.0.1", 6000))
data, addr = client.recvfrom(1024)

print("received:", data.decode())

Comparison between TCP and UDP

TCP:

  • connection-oriented
  • reliable
  • ordered
  • slower
  • used for important data

UDP:

  • connectionless
  • unreliable
  • unordered
  • faster
  • used for real-time data

How ports and protocols work together

when you visit a website:

  1. your browser creates a tcp connection.
  2. it connects to the server’s ip address.
  3. it uses port 80 (http) or 443 (https).
  4. tcp ensures the webpage data arrives correctly.

when you play an online game:

  1. the game often uses udp.
  2. it sends data to a specific port.
  3. speed is more important than perfect accuracy.

so the full communication requires:

  • ip address → identifies the device.
  • port number → identifies the application.
  • protocol (tcp/udp) → defines how the data is handled.


Example Code:
# =========================
# tcp server
# =========================
import socket

def tcp_server():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(("127.0.0.1", 5000))
    server.listen(1)
    print("tcp server is listening...")
    conn, addr = server.accept()
    print("connected to", addr)
    data = conn.recv(1024)
    print("received:", data.decode())
    conn.sendall(b"hello from tcp server")
    conn.close()

# =========================
# tcp client
# =========================
def tcp_client():
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(("127.0.0.1", 5000))
    client.sendall(b"hello from tcp client")
    data = client.recv(1024)
    print("received:", data.decode())
    client.close()

# =========================
# udp server
# =========================
def udp_server():
    server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server.bind(("127.0.0.1", 6000))
    print("udp server is listening...")
    data, addr = server.recvfrom(1024)
    print("received:", data.decode())
    server.sendto(b"hello from udp server", addr)

# =========================
# udp client
# =========================
def udp_client():
    client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    client.sendto(b"hello from udp client", ("127.0.0.1", 6000))
    data, addr = client.recvfrom(1024)
    print("received:", data.decode())

# choose which function to run manually
# tcp_server()
# tcp_client()
# udp_server()
# udp_client()
Cyber Security
Introduction Types of Cyber Threats Cyber Security Domains CIA Triad (Confidentiality Integrity Availability) Career paths in Cyber Security Certifications Ethics and Responsible Disclosure Laws and Regulation (e.g. GDPR, NDPR) What is an OS? Types: Window, Linus, macOS Command-line vs GUI OS Internals Overview (filesystems, processes, permissions) Windows command prompt basics Linux Bash Basics File System Navigation Basic Scripting IP Addressing DNS, DHCP Mac Address OSI VS TCP/IP Models Ports and Protocols (TCP, UDP) Common Protocols (HTTPS, FTP, SSH, etc.) Packet structure Firewalls, IDS/IPS, VPNs Common attacks: MITM, Sniffing Secure Network Practices How the Web works HTTP vs HTTPS URLs, Headers, Cookies Client-Server Architecture Introduction To Web Security OWASP Top 10 Overview Common Threats (XSS, SQLi, CSRF) Inpute validation and authentication flow Basic Exploitation demo (e.g. XSS) Burp Suite Introduction Using a Browser For Testing Password security MFA-Antivirus Cyber Hygeine Practice Intro To Tools: Nmap, Wireshark, Netstat
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL