1.
Echo Client-Server (UDP)
# Server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('localhost', 9999))
while True:
d, a = s.recvfrom(1024)
s.sendto(d, a)
# Client
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
msg = b"hello"
s.sendto(msg, ('localhost', 9999))
d, _ = s.recvfrom(1024)
print("Match" if d == msg else "Mismatch")
s.close()
2. Server-to-Client Message (UDP)
# Server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('localhost', 8888))
while True: s.sendto(input().encode(), ('localhost', 9999))
# Client
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('localhost', 9999))
while True: print(s.recvfrom(1024)[0].decode())
3. Chat using UDP
# Server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('localhost', 9000))
while True:
msg, a = s.recvfrom(1024)
print("Client:", msg.decode())
s.sendto(input("You: ").encode(), a)
# Client
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
s.sendto(input("You: ").encode(), ('localhost', 9000))
print("Server:", s.recvfrom(1024)[0].decode())
4. Hello Transfer (UDP)
# Server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('localhost', 8000))
while True:
m, a = s.recvfrom(1024)
if m == b"hello server": s.sendto(b"hello client", a)
# Client
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(b"hello server", ('localhost', 8000))
print("Server:", s.recvfrom(1024)[0].decode())
5. TCP File Request
# Server
import socket
s = socket.socket(); s.bind(('localhost', 9000)); s.listen(1)
c, _ = s.accept()
try: c.send(open(c.recv(1024).decode()).read().encode())
except: c.send(b"Not found")
c.close()
# Client
import socket
s = socket.socket(); s.connect(('localhost', 9000))
s.send(input("File: ").encode())
print(s.recv(4096).decode())
6. Framing Methods
def cc(d): return f"{len(d)}:{d}"
def cs(d): return d.replace("~", "^~")
def bs(d): return d.replace("11111", "111110")
print("Char Count:", cc("Hello~World"))
print("Char Stuff:", cs("Hello~World"))
print("Bit Stuff:", bs("111110111"))
7. Sliding Window
data = [f"Frame {i}" for i in range(10)]
i = 0
while i < len(data):
for j in range(i, min(i+5, len(data))): print("Sent:", data[j])
i = int(input("Last ACK: ")) + 1
8. UDP Broadcast
# Server
import socket, time
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
for _ in range(4):
s.sendto(b"Hello", ('<broadcast>', 5000))
time.sleep(1)
s.close()
# Client
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', 5000))
while True: print("Received:", s.recvfrom(1024)[0].decode())
9. IP Routing
import ipaddress
r = [("192.168.1.0/24", "192.168.1.1"), ("0.0.0.0/0", "192.168.100.1")]
ip = ipaddress.ip_address(input("IP: "))
m = max((x for x in r if ip in ipaddress.ip_network(x[0])),
key=lambda x: ipaddress.ip_network(x[0]).prefixlen, default=None)
print("Next Hop:", m[1] if m else "None")
10. File Transfer (UDP)
# Server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('localhost', 9000))
f, a = s.recvfrom(1024)
try:
with open(f.decode(), 'rb') as file:
for b in iter(lambda: file.read(1024), b''): s.sendto(b, a)
except: s.sendto(b"Not found", a)
s.close()
# Client
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
f = input("File: ")
s.sendto(f.encode(), ('localhost', 9000))
with open(f"recv_{f}", 'wb') as out:
while True:
d, _ = s.recvfrom(1024)
if d == b"Not found" or not d: break
out.write(d)
s.close()
print(f"Saved: recv_{f}")