Ports and Protocols (TCP, UDP) | Cyber Security Tutorial - Learn with VOKS
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
Back Next

Ports and Protocols (TCP, UDP)

Learn this topic step-by-step with VOKS Tutorials.

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.


# =========================
# 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()
QUICK KNOWLEDGE CHECK

Test Your Understanding

Answer 5 questions generated from this lesson. Your result is calculated instantly and is not saved.

0 / 5 answered
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
Course contents
Cyber Security
01 Introduction 02 Types of Cyber Threats 03 Cyber Security Domains 04 CIA Triad (Confidentiality Integrity Availability) 05 Career paths in Cyber Security 06 Certifications 07 Ethics and Responsible Disclosure 08 Laws and Regulation (e.g. GDPR, NDPR) 09 What is an OS? 10 Types: Window, Linus, macOS 11 Command-line vs GUI 12 OS Internals Overview (filesystems, processes, permissions) 13 Windows command prompt basics 14 Linux Bash Basics 15 File System Navigation 16 Basic Scripting 17 IP Addressing 18 DNS, DHCP 19 Mac Address 20 OSI VS TCP/IP Models 21 Ports and Protocols (TCP, UDP) 22 Common Protocols (HTTPS, FTP, SSH, etc.) 23 Packet structure 24 Firewalls, IDS/IPS, VPNs 25 Common attacks: MITM, Sniffing 26 Secure Network Practices 27 How the Web works 28 HTTP vs HTTPS 29 URLs, Headers, Cookies 30 Client-Server Architecture 31 Introduction To Web Security 32 OWASP Top 10 Overview 33 Common Threats (XSS, SQLi, CSRF) 34 Inpute validation and authentication flow 35 Basic Exploitation demo (e.g. XSS) 36 Burp Suite Introduction 37 Using a Browser For Testing 38 Password security 39 MFA-Antivirus 40 Cyber Hygeine Practice 41 Intro To Tools: Nmap, Wireshark, Netstat