Ex.
2 – HTTP Web Client using TCP Sockets
In [4]: import socket
s = [Link]()
[Link](("[Link]", 80))
[Link](b"GET / HTTP/1.1\r\nHost: [Link]\r\nConnection: close\r\n\r\n")
d = b""
while chunk := [Link](1024): d += chunk
[Link]()
b = [Link](b"\r\n\r\n",1)[1]
i = [Link](b"</body>")
b = b[:i] + b"<p>hello makkaley</p>" + b[i:] if i!=-1 else b + b"<p>hello m
open("[Link]","wb").write(b)
print("Saved [Link]")
Saved [Link]
EXP 3A Echo client and Echo Server
In [ ]: import socket
server_socket = [Link]()
server_socket.bind(("localhost", 1234))
server_socket.listen(1)
print("Server is listening on port 1234...")
conn, addr = server_socket.accept()
print(f"Connected by {addr}")
data = [Link](1024)
[Link](data)
[Link]()
print("Connection closed.")
In [ ]: import socket
client_socket = [Link]()
client_socket.connect(("localhost", 1234))
client_socket.sendall(b"Hello")
response = client_socket.recv(1024)
print("Received from server:", [Link]())
client_socket.close()
EXP 3B Program to Chat using TCP
In [ ]: import socket
s = [Link]()
[Link](('localhost', 5000))
[Link](1)
conn, _ = [Link]()
while True:
msg = [Link](1024).decode()
if [Link]() == "bye":
break
print("Client:", msg)
[Link](input("You: ").encode())
[Link]()
In [ ]: import socket
s = [Link]()
[Link](('localhost', 5000))
while True:
[Link](input("You: ").encode())
reply = [Link](1024).decode()
if [Link]() == "bye":
break
print("Server:", reply)
[Link]()
[Link]: 4 Simulation of DNS using UDP Sockets
In [1]: import socket
def dns_lookup(domain):
try:
ip = [Link](domain)
return f"Domain: {domain}\nIP Address: {ip}"
except [Link]:
return f"Could not resolve domain: {domain}"
# Example usage:
domain_name = input("Enter a domain name to lookup: ")
result = dns_lookup(domain_name)
print(result)
Enter a domain name to lookup: [Link]
Domain: [Link]
IP Address: [Link]
EXP 6A Write a code simulating ARP Protocols
In [ ]: import socket
arp_table = {"[Link]": "AA:BB:CC:DD:EE:FF"}
s = [Link]()
[Link](('localhost', 9999))
[Link](1)
print("ARP Server listening on port 9999...")
conn, addr = [Link]()
print(f"Connection from {addr}")
ip = [Link](1024).decode()
print(f"Received IP request: {ip}")
mac = arp_table.get(ip, "Not found")
[Link]([Link]())
print(f"Sent MAC address: {mac}")
[Link]()
print("Connection closed.")
In [ ]: import socket
s = [Link]()
[Link](('localhost', 9999))
# You can change this IP or make it user input
ip = input("Enter IP address to query MAC: ")
[Link]([Link]())
mac_address = [Link](1024).decode()
print("MAC Address:", mac_address)
[Link]()
EXP 6B Write a code simulating RARP Protocols
In [ ]: import socket
# Reverse ARP table: MAC -> IP
rarp_table = {
"AA:BB:CC:DD:EE:FF": "[Link]"
}
s = [Link]()
[Link](('localhost', 9998)) # Use different port to avoid conflict
[Link](1)
print("RARP Server listening on port 9998...")
conn, addr = [Link]()
print(f"Connected by {addr}")
mac = [Link](1024).decode()
print(f"Received MAC request: {mac}")
ip = rarp_table.get(mac, "Not found")
[Link]([Link]())
print(f"Sent IP address: {ip}")
[Link]()
print("Connection closed.")
In [ ]: import socket
s = [Link]()
[Link](('localhost', 9998))
mac = input("Enter MAC address to query IP: ")
[Link]([Link]())
ip_address = [Link](1024).decode()
print("IP Address:", ip_address)
[Link]()
Ex.9 – Distance Vector Routing
In [2]: graph = {
'A': {'B': 1, 'C': 3},
'B': {'A': 1, 'C': 1},
'C': {'A': 3, 'B': 1}
}
def distance_vector(node):
dist = graph[node]
print(f"Routing table for {node}:")
for dest, cost in [Link]():
print(f"To {dest}: cost = {cost}")
print()
for node in graph:
distance_vector(node)
Routing table for A:
To B: cost = 1
To C: cost = 3
Routing table for B:
To A: cost = 1
To C: cost = 1
Routing table for C:
To A: cost = 3
To B: cost = 1
Ex.10 – CRC Error Detection
In [3]: def crc(d, k):
d += '0'*(len(k)-1)
d = list(map(int, d))
k = list(map(int, k))
for i in range(len(d)-len(k)+1):
if d[i]: d[i:i+len(k)] = [d[i+j]^k[j] for j in range(len(k))]
return d[-(len(k)-1):]
data, key = '1101011011', '1011'
code = data + ''.join(map(str, crc(data, key)))
print("Codeword:", code)
# Flip last bit to simulate error
recv = code[:-1] + ('1' if code[-1]=='0' else '0')
print("Error detected:", "Yes" if any(crc(recv, key)) else "No")
Codeword: 1101011011100
Error detected: Yes
In [ ]: