SIMPLE COMMUNICATION USING UDP SERVER
AIM:
To write a python program for simple client-server using UDP.
PROCEDURE:
SERVER SIDE:
Step1:Create UDP[User Datagram Protocol] socket.
Step2:Specify the communication domain and type of socket to be created.
Step3:Bind the socket to the server address.
Step4:Wait until datagram packet arrives from client.
Step5:Process the datagram packet and send a reply to the client.
CLIENT SIDE:
Step1:Create UDP socket.
Step2:Encode the message to be sent in bytes.
Step3:Specify the communication domain and type.
Step4:Wait until response from server is received.
PROGRAM:
SERVER SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',7100))
data,addr=s.recvfrom(1024)
print(data)
CLIENT SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto(str.encode("HII SERVER"),('127.0.0.1',7100))
OUTPUT:
SERVER SIDE:
>>>exec(open(“R:\\New folder\\server.py”).read())
B’HII SERVER’
CLIENT SIDE:
>>>exec(open(“R:\\New folder\\client.py”).read())
RESULT:
Thus the program for client-server using UDP is implemented.
ECHO CLIENT SERVER IMPLEMENTATION USING UDP
AIM:
To write a python program for Echo client-server communication using UDP.
PROCEDURE:
SERVER SIDE:
Step1:Import socket module.
Step2:Create a datagram socket.
Step3:Bind the socket to the server id and port.
Step4:Wait until the datagram arrives from client.
Step5:Receive the data packets from the client along with its address.
Step6:Print the address and data
CLIENT SIDE:
Step1:Import socket module.
Step2:Create UDP socket.
Step3:Encode the message to be sent in bytes.
Step4:Send the data to server.
Step5:Receive the message.
Step6:Print the data and address received from server.
PROGRAM:
SERVER SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',6011))
print("server is waiting")
data,addr=s.recvfrom(1024)
print(data,addr)
s.sendto(data,addr)
CLIENT SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto(str.encode("hii"),('127.0.0.1',6011))
data,addr=s.recvfrom(1024)
print(data,addr)
OUTPUT:
SERVER SIDE:
>>>exec(open(“R:\\New folder\\server.py”).read())
server is waiting
b’hii’(‘127.0.0.1’,49161)
CLIENT SIDE:
>>>exec(open(“R:\\New folder\\client.py”).read())
b’hii’(‘127.0.0.1’,6011)
RESULT:
Thus the program for Echo client-server communication using UDP is implemented.
HALF DUPLEX COMMUNICATION USING TCP
AIM:
To write a python program for half duplex communication using TCP.
PROCEDURE:
SERVER SIDE:
Step1:Import socket module.
Step2:Create a TCP[Transmission Control Protocol]socket object for communication.
Step3:Bind the socket to the server.
Step4:Wait until connection is established between client and server.
Step5:Receive the data packets from client.
Step6:Send the reply to the client.
Step7.If the reply is disconnect, then the conncection will be closed.
CLIENT SIDE:
Step1:Import socket module.
Step2:Create a TCP socket object for communication.
Step3:Connect the socket to the server.
Step4:Send a message to the server.
Step5:Receive the message sent by server.
Step6:Print the message.
Step7:If the message is disconnect, then close the socket connection.
PROGRAM:
SERVER SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('127.0.0.1',6012))
s.listen(1)
client,addr=s.accept()
while True:
data=client.recv(1024)
print(data)
sentence=input(">>")
client.send(str.encode(sentence))
if "quit" in str(sentence):
break
client.close()
CLIENT SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('127.0.0.1',6012))
while True:
sen=input(">>")
s.send(str.encode(sen))
data=s.recv(1024)
print(data)
if "quit" in str(data):
break
OUTPUT:
SERVER SIDE:
>>>exec(open(“R:\\New folder\\server.py”).read())
b’hii’
>>hello
b’how are you’
>>fine
CLIENT SIDE:
>>>exec(open(“R:\\New folder\\client.py”).read())
>>hii
b’hello’
>>how are you
b’fine’
RESULT:
Thus the half duplex communication using TCP is implemented.
FULL DUPLEX COMMUNICATION USING UDP
AIM:
To write a python program for full duplex communication.
PROCEDURE:
SERVER SIDE:
Step1:Import socket module.
Step2:Create a UDP socket object for communication.
Step3:Bind the socket to the server.
Step4:Create an array list of clients.
Step5:Connection will be established between client and server.
Step6:Communication between clients and the server will record the date and time transmitted
between clients.
CLIENT SIDE:
Step1:Import socket module and threading.
Step2:Create an UDP socket object for communication.
Step3:Establish connection between server and client.
Step4:Get data as input and send to server.
Step5:Server again retransmits the data to other clients connected to server using threading.
Step6:Print the data in clients, such that there is full duplex communication.
Step7:If the message is q, then client stops sending data to the server.
PROGRAM:
SERVER SIDE:
import socket
import time
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',7000))
clients=[]
s.setblocking(0)
quitting=False
while not quitting:
try:
data,addr=s.recvfrom(1024)
if 'q' in str(data):
quitting=True
if addr not in clients:
clients.append(addr)
print(time.ctime(time.time())+str(addr)+":"+str(data))
for client in clients:
s.sendto(data,client)
except:
pass
s.close()
CLIENT SIDE:
import socket
import time
import threading
shutdown=False
tlock=threading.Lock()
def receiving(name,sock):
while not shutdown:
tlock.acquire()
try:
while True:
data,addr=sock.recvfrom(1024)
print(data)
except:
pass
finally:
tlock.release()
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',0))
s.setblocking(0)
rT=threading.Thread(target=receiving,args=("Recvthread",s))
rT.start()
alias=input("Name:")
ali=str.encode(alias+":")
message=input(alias+"->")
msg=str.encode(message)
while 'q' not in str(msg):
if "''" not in str(msg):
d=(ali+msg)
s.sendto(d,('127.0.0.1',7000))
tlock.acquire()
message=input(alias+"->")
msg=str.encode(message)
tlock.release()
shutdown=True
rT.join()
s.close()
OUTPUT:
SERVER SIDE:
>>>exec(open(“R:\\New folder\\server.py”).read())
Mon Dec 17 18:41:23 2018(‘127.0.0.1’,49168):b’janu:hii’
Mon Dec 17 18:41:28 2018(‘127.0.0.1’,49169):b’jansi:hello’
Mon Dec 17 18:41:32 2018(‘127.0.0.1’,49168):b’janu:Who is this?’
CLIENT SIDE:
>>>exec(open(“R:\\New folder\\client.py”).read())
Name:janu
Janu->hii
Janu->
b’janu:hii’
b’jansi:hello’
janu->who is this
>>>exec(open(“R:\\New folder\\client.py”).read())
Name:jansi
Jansi->
b’hii
jansi->hello
b’janu:hii’
b’jansi:hello’
b’janu:who is this?
RESULT:
Thus the full duplex communication is established.
IMPLEMENTATION OF SIMPLE STOP AND WAIT PROTOCOL
AIM:
To write a python program to send and receive frame using Stop and Wait protocol.
PROCEDURE:
SENDER:
Step1:Import socket module.
Step2:Create a UDP socket object for communication.
Step3:Bind to the receiver.
Step4:Send a frame.
Step5:After transmitting one packet, the sender waits for an acknowledgement before transmitting next
one.
Step6:Close the socket after sending data.
RECEIVER:
Step1:Import socket module.
Step2:Create a UDP socket object for communication.
Step3:Bind the socket to loop back address.
Step4:Receive the frame.
Step5:After receiving, send the acknowledgement(ie.the next expecting frame).
Step6:Close the socket.
PROGRAM:
SENDER:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',6009))
while True:
data=input(">>")
s.sendto(str.encode(data),('127.0.0.1',6014))
m,a=s.recvfrom(1024)
print("EXPECTING FRAME:"+str(m))
s.close()
RECEIVER:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',6014))
count='0'
while True:
acknowledge=False
while not acknowledge:
ack,ad=s.recvfrom(1024)
print("FRAME RECEIVED:"+str(ack))
if "0" in str(ack):
count='1'
elif "1" in str(ack):
count='0'
else:
break
s.sendto(str.encode(count),ad)
acknowledge=True
s.close()
OUTPUT:
SENDER:
>>>exec(open(“R:\\New folder\\sender.py”).read())
>>1
EXPECTING FRAME:b’0’
>>0
EXPECTING FRAME:b’1’
>>1
RECEIVER:
>>>exec(open(“R:\\New folder\\sender.py”).read())
FRAME RECEIVED:b’1’
FRAME RECEIVED:b’0’
RESULT:
Thus the frame has been sent and received using stop and wait protocol.
IMPLEMENTATION OF ARP PROTOCOL
AIM:
To write a python program to simulate Address Resolution Protocol.
PROCEDURE:
Step1:Create an array to store the IP and its corresponding MAC address.
Step2:Get the input address and MAC address as input.
Step3:Print the IP and MAC.
Step4:Search the MAC for your corresponding ip in the stored table.
Step5:Print the MAC address which is a hexadecimal number.
PROGRAM:
d={}
for i in range(2):
ip=input("Enter the ip:")
mac=input("Enter the mac:")
d[ip]=mac
print()
print(d)
s=input("Enter the search item:")
print(d[s])
OUTPUT:
>>>exec(open(“R:\\New folder\\arp.py”).read())
Enter the ip:10.1.24.121
Enter the mac:E0-CA-94-CB-A4-D8
Enter the ip:10.1.24.122
Enter the mac:E0-C7-94-CB-A4-D7
{’10.1.24.121’:’E0-CA-94-CB-A4-D8’,’10.1.24.122’:’E0-C7-94-CB-A4-D7’}
Enter the search item: 10.1.24.122
E0-C7-94-CB-A4-D7
RESULT:
Thus the Address Resolution Protocol is simulated.
FIND THE ADDRESS SPACE,FIRST ADDRESS,LAST ADDRESS USING
SUBNETTING IN CIDR
AIM:
To write a python program to calculate first address, last address and address space of given IP.
PROCEDURE:
Step1:Import ipaddress module and math module.
Step2:Get the IP address and prefix number.
Step3:Split the IP address and prefix number.
Step4:Find the Address space using the formula 2^(32-prefix number).
Step5:Using IPV4 network, list of all addresses from first to last are stored in array.
Step6:Print n[0] and n[-1] to get the first and last address.
PROGRAM:
import ipaddress
import math
ip=input("Enter the ip address:")
i=ip.split("/")
p=i[1]
l=32-int(p)
val=math.pow(2,int(l))
n=ipaddress.IPv4Network(ip)
first,last=n[1],n[-1]
print("ADDRESS BLOCK",int(val))
print("FIRST ADDRESS",first)
print("LAST ADDRESS",last)
OUTPUT:
>>>exec(open(“R:\\New folder\\subnet.py”).read())
Enter the ip address:10.1.24.0/26
ADDRESS BLOCK 64
FIRST ADDRESS 10.1.24.1
LAST ADDRESS 10.1.24.63
RESULT:
Thus the address space,last address and first addresses are calculated from the given ip.
IMPLEMENTATION OF HTTP SOCKET
AIM:
To write a python program for webpage upload and download using http socket.
PROCEDURE:
UPLOADING:
Step1:Import HTTPServer and BaseHTTPRequest from http.server.
Step2:Create a class Serv.
Step3:Handle the HTTP requests that arrive at the server.
Step4:If specified path is ‘/’ the upload the html file.
Step5:Send response 200.
Step6:Else send response 404 and ‘FILE NOT FOUND’.
Step 7:Write the file content on the screen.
Step8:Close the HTTP server.
DOWNLOADING:
Step1:Import urllib.request module for fetching URLs.
Step2:Urllib.request.urlretrieve the files from the specified link.
PROGRAM:
UPLOADING:
from http.server import HTTPServer,BaseHTTPRequestHandler
class Serv(BaseHTTPRequestHandler):
def do_GET(self):
if self.path==(r'/'):
self.path=(r'C:/new\file.html')
try:
file_to_open=open(self.path[2:]).read()
self.send_response(200)
except:
file_to_open="FILE ERROR"
self.send_response(404)
self.end_headers()
self.wfile.write(bytes(file_to_open,'utf-8'))
httpd=HTTPServer(('127.0.0.1',8000),Serv)
httpd.serve_forever()
HTML FILE:
<html>
<head>
<title>welcome</title>
</head>
<body>
<h3>WELCOME</h3>
</body>
</html>
DOWNLOADING:
import urllib.requesturllib.request.urlretrieve("https://www.govtrack.us/congress/votes/115-
2017/h200/diagram","angl.jpg")
OUTPUT:
UPLOAD:
>>>exec(open(“R:\\New folder\\upload.py”).read())
DOWNLOAD:
>>>exec(open(“R:\\New folder\\download.py”).read())
RESULT:
Thus the webpage upload and download are done using http socket.