0% found this document useful (0 votes)
3 views

748 CN Lab Assignment 9

Uploaded by

souvik05baidya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

748 CN Lab Assignment 9

Uploaded by

souvik05baidya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

COMPUTER Network lab

Assignment-9

Name: Souvik Baidya


Roll No: CSE/21088/748
1. Write a TCP/UDP socket program (in C/C++/Java/Python) to establish a connection
between client and server. The server should act as a network device maintaining an
ARP table. Implement ARP request and reply functionality. Display appropriate
messages indicating the ARP request and response. Test your program with multiple
clients requesting ARP resolution for different IP addresses.

Ans:

Server code:
import socket
port=50000
host="127.0.0.1"
server= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((host, port))
print("socket binded to %s" %(port))

server.listen(2)
print("Socket is listening...")

# Accepting/Establishing connection from client.


conn, addr = server.accept()
print('Got connection from', addr)

import subprocess

def get_arp_table():
arp_output = subprocess.check_output(['arp', '-a']).decode('utf-
8')
# print(arp_output)
arp_lines = arp_output.splitlines()
arptable={}
for line in arp_lines:
if 'dynamic' in line.lower() or 'static' in line.lower()
: # Filter out only dynamic entries
# print(lisne)
parts = line.split()
# print(parts)

ip_address = parts[0]
mac_address = parts[1]
arptable[ip_address]= mac_address
return arptable

while True:
recieved_data = conn.recv(2048)
formateddata=recieved_data.decode()
if formateddata=='Quit':
break
print("Message from client: ",formateddata)

arptable=get_arp_table()
if(formateddata in arptable ):
conn.send(f" corressponding mac is
{arptable[formateddata]}".encode('utf-8'))
else :
conn.send(f" mac address not found".encode('utf-8'))

print("Connection closed from client")


#Close the connection with the client
conn.close()

Client code:
import socket

port=50000
portClient=5004
host="127.0.0.1"

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


client.bind((host, portClient))
client.connect((host, port))
while True:
data = input("Enter an Ip: ")

client.send(data.encode())
if data=='Quit':break
print(client.recv(3089).decode())

print("Connection closed from server")


client.close()

Output.png

2. Write a TCP/UDP socket program (in C/C++/Java/Python) to establish a connection


between client and server. The server should act as a network device maintaining a RARP
table mapping MAC addresses to IP addresses. Implement RARP request and reply
functionality. Display appropriate messages indicating the RARP request and response.
Test your program with multiple clients requesting RARP resolution for different MAC
addresses.

Ans:

Server Code:

import socket
udpsock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
udp_host = socket.gethostname()
udp_port = 5000
udpsock.bind((udp_host,udp_port))
import subprocess

def get_rarp_table():
arp_output = subprocess.check_output(['arp', '-a']).decode('utf-
8')
# print(arp_output)
arp_lines = arp_output.splitlines()
rarptable={}
for line in arp_lines:
if 'dynamic' in line.lower() or 'static' in line.lower()
: # Filter out only dynamic entries
# print(lisne)
parts = line.split()
# print(parts)

ip_address = parts[0]
mac_address = parts[1]
rarptable[mac_address]= ip_address
return rarptable
while True:
recieved_data,addr = udpsock.recvfrom(1024)
datarecv=recieved_data.decode()
rarptable=get_rarp_table()
if(datarecv in rarptable ):
udpsock.sendto(f" corressponding ip is
{rarptable[datarecv]}".encode('utf-8'),addr)
else :

udpsock.sendto(f" Ip address not found".encode('utf-


8'),addr)

Client Code:

import socket

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # For


UDP

udp_host = socket.gethostname() # Host IP


udp_port = 5000 # specified port to connect
message=input("Enter Mac address: ")
while message!="Quit":

sock.sendto(message.encode('utf-8'),(udp_host,udp_port))
recieved_data,addr=sock.recvfrom(1024)

print(recieved_data.decode())
message=input("Enter Mac address:")

Output.png:

You might also like