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:
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:
ports range from 0 to 65535 and are divided into:
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:
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:
after this, the connection is established and data can be transferred.
features of tcp:
advantages:
disadvantages:
examples of tcp usage:
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:
udp simply sends packets (called datagrams) without checking if they arrive.
features of udp:
advantages:
disadvantages:
examples of udp usage:
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:
UDP:
How ports and protocols work together
when you visit a website:
when you play an online game:
so the full communication requires:
# =========================
# 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()