cn record
cn record
THAVALAKUPPAM
PUDUCHERRY – 605 007
DEPARTMENT OF
COMPUTER APPLICATIONS
Bachelor of Computer Applications - Third Semester
2023-2024
Record Note Book
MAIN PRACTICAL
COMPUTER NETWORK LAB
Name :
Reg.No :
RAJIV GANDHI ARTS AND SCIENCE COLLEGE
THAVALAKUPPAM – PUDUCHERRY – 605 007
DEPARTMENT OF COMPUTER APPLICATIONS
BONAFIDE CERTIFICATE
Certified that this is a bonafide record of practical work
done by Mr./Ms..........................................p................
Reg.No......................
Puducherry.
01
02
03
04
05
06
07
GET IP USING SOCKET
PROGRAM
import socket
system=socket.gethostname()
ip_address=socket. gethostbyname(system)
print ("ip address is"+ip_address)
OUTPUT:
ONE WAY CONNECTION USING TCP
CLIENT PROGRAM
import socket
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
hostname=socket.gethostname()
host=socket.gethostbyname(hostname)
port=4444
sock_connect=sock.connect((host,port))
print("connected")
while True:
try:
data=sock.recv(1024)
print(data.decode('utf-8'))
except Exception as e:
break
sock.close()
OUTPUT:
SERVER PROGRAM
import socket
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
hostname=socket.gethostname()
host=socket.gethostbyname(hostname)
port=4444
sock.bind((host,port))
sock.listen(1)
conn,address=sock.accept()
print("connected to "+address[0])
while True:
try:
connection=conn.fileno()
if connection == -1:
print("client disconnected")
break
byte_data=data.encode("utf-8")
conn.send(byte_data)
except Exception as e:
print("error as occurd")
break
sock.close()
TWO WAY CONNECTION USING TCP
CLIENT PROGRAM
import socket
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
hostname=socket.gethostname()
host=socket.gethostname()
host=socket.gethostbyname(hostname)
port=4848
sock.connect((host,port))
sock.send(user_name.encode('utf-8'))
server_name=sock.recv(1024)
while True:
data=sock.recv(1024)
print(server_name.decode('utf-8')+">"+data.decode('utf-8'))
message=input(user_name+">")
sock.send(message.encode('utf-8'))
sock.close()
OUTPUT:
SERVER PROGRAM
import socket
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
hostname=socket.gethostname()
host=socket.gethostbyname(hostname)
port=4848
sock.bind((host,port))
sock.listen(2)
conn,addr=sock.accept()
conn.send(user_name.encode('utf-8'))
client_name=conn.recv(1024)
while True:
message=input(user_name+">")
conn.send(message.encode('utf-8'))
data=conn.recv(1024)
print(client_name.decode('utf-8')+">"+str(data.decode('utf-8')))
sock.close()
OUTPUT:
ONE WAY CONNECTION USING UDP
CLIENT PROGRAM:
import socket
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
hostname=socket.gethostname()
host=socket.gethostbyname(hostname)
port=4444
sock.sendto("connect".encode('utf-8'),(host,port))
print("connected")
while True:
data=sock.recv(1024)
print(data.decode('utf-8'))
if not data:
break
sock.close()
OUTPUT:
SERVER PROGRAM
import socket
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
hostname=socket.gethostname()
host=socket.gethostbyname(hostname)
port=4444
sock.bind((host,port))
data=sock.recvfrom(1024)
print(data)
print("connected")
while True:
try:
sock.sendto(message.encode('utf-8'),data[1])
except Exception as e:
print("Error occurded")
break
sock.close()
OUTPUT:
TWO WAY CONNECTION USING UDP
CLIENT PROGRAM
import socket
sock-socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
hostname-socket.gethostname()
host-socket.gethostbyname(hostname)
port-4444
sock.sendto("connect".encode('utf-8'), (host,port))
print("connected")
while True:
data=sock.recvfrom(1024)
print(data[0].decode('utf-8'))
sock.sendto(message.encode('utf-8'),data[1])
if not data:
break
sock.close()
SERVER PROGRAM
import socket
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
hostname=socket.gethostname()
host-socket.gethostbyname(hostname)
port=4444
sock.bind((host,port))
print("connected")
while True:
try:
data-sock.recvfrom(1024)
print(data[0].decode('utf-8'))
sock.sendto(message.encode('utf-8'),data[1])
except Exception as e:
break
sock.close()
OUTPUT:
URL PARSE
url-parse.urlparse('http://google.com:80/gmail')
scheme=url.scheme
port=url.port
location-url.netloc
neturl=url.geturl()
print(scheme)
print(port)
print(location)
print(neturl)
OUTPUT:
FILE SHARING
CLIENT PROGRAM
import socket
sock-socket.socket(socket.AF_INET, socket.SOCK_STREAM)
hostname socket.gethostname()
host-socket.gethostbyname(hostname)
port-4444
sock.connect((host,port))
print("connected")
while True:
data-sock.recv(1024)
f.write(data)
f.close()
print("File Saved")
sock.close()
OUTPUT:
SERVER PROGRAM
import socket
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
hostname=socket.gethostname()
host=socket.gethostbyname(hostname)
port=4444
sock.bind((host,port))
sock.listen(10)
conn,addr=sock.accept()
print("connected")
print("Sending Filess...")
file=f.read(860160)
conn.send(file)
f.close()
sock. close()
OUTPUT :