You are on page 1of 10

Name: Mohammad Modi

Reg no: 19BCE2215


Course Network and communication
Lab fat
1.
rm -r dirname

Reg no: 19BCE2215


2.
Netstat

Reg no: 19BCE2215


3.
CODE
def calcRedundantBits(m):
for i in range(m):
if(2**i >= m + i + 1):
return i

def DecodeProcessing(aarr):
del aarr[0]
temp=(reverseArray(aarr))
return temp

def reverseArray(aarr):
temp=['']*len(aarr)
for i in range(len(aarr)):
temp[i]=aarr[-(i+1)]
return temp

def getParity( n ):
parity = 0
while n:
parity = ~parity
n = n & (n - 1)
return parity

def intToBinary(var):
return (bin(var).split("0b")[1])

def binToInteger(var):
return (int(var,2))

def listToString(aarr):
string = ""
for element in aarr:
string += element
return string

def arrRedundantPosition(r):
arrRedundantPos=[]
for i in range(r):
arrRedundantPos.append(2**i-1)
return arrRedundantPos

Reg no: 19BCE2215


def getParitybit(aarr, r):
temp=''
for i in range(len(aarr)):
if (i & r == r) :
temp += aarr[i]
return "1" if getParity(binToInteger(temp)) else "0"

def Hamming(message):
message=intToBinary(message)
print("Message in binary = ",message)
m=len(message)
r=calcRedundantBits(m)
print("Number of redundant bits = ",r)
n=m+r
aarr=["0"]*n
arrRedundantPos=arrRedundantPosition(r)
count=0
for i in range(n):
if i not in arrRedundantPos:
count= count+1
aarr[i]=message[-count ]
aarr.insert(0,'START')
for i in range(len(arrRedundantPos)):
arrRedundantPos[i] = arrRedundantPos[i]+1
for i in arrRedundantPos:
aarr[i]=getParitybit(aarr,i)
for i in range(len(aarr)-1):
aarr[i]=aarr[i+1]
aarr.pop(-1)
AArr=reverseArray(aarr)
print("The Hamming Code encoded message =",listToString(AArr))

def checkArrayRedundantBits(aarr):
n=len(aarr)
aarr=[]
for i in range(n):
if 2**i < n:
aarr.append(2**i)
return aarr

#Encoding part
n=int(input("Enter the number in decimal : "))
Hamming(n)

#Decoding part

Reg no: 19BCE2215


n=input("Enter the Encoded Message: ")

aarr=['']*(len(n))
for i in range(len(n)):
aarr[i]=n[i]
aarr=reverseArray(aarr)
dArr=checkArrayRedundantBits(aarr)
aarr.insert(0,'START')
ErrorPos=[]
for i in dArr:
ErrorPos.append(getParitybit(aarr, i))
if binToInteger(listToString(reverseArray(ErrorPos)))==0:
print("No Error Found")
aarr=DecodeProcessing(aarr)
else:
errordet=binToInteger(listToString(reverseArray(ErrorPos)))
print("Error Found at",errordet, "position from the left")
aarr=DecodeProcessing(aarr)
if (aarr[-(errordet)]=='1'):
aarr[-errordet]='0'
else:
aarr[-errordet]='1'
print("The Corrected Encoded Message =",listToString(aarr))
cArr=[]
for i in range(len(dArr)):
dArr[i]=-dArr[i]
for i in range(len(aarr)):
if -(i+1) not in dArr:
cArr.append(aarr[-(i+1)])
cArr=reverseArray(cArr)
print("The Decoded Message =", listToString(cArr))
print("The Message Recieved =", binToInteger(listToString(cArr)))

Reg no: 19BCE2215


4.
CODE
SERVER
import socket
import random
sock = socket.socket()
host = socket.gethostname()

Reg no: 19BCE2215


ip = socket.gethostbyname(host)
port = 5001
sock.bind((host, port))
sock.listen(2)
print("Searching for Connection")
connection, address = sock.accept()
print("Connected to :", address)
no1 = connection.recv(1024).decode()
no2 = connection.recv(1024).decode()
no3 = connection.recv(1024).decode()
print("\nRecieved First Integer from Client : ",no1)
print("Recieved Second Integer from Client : ",no2)
print("Recieved Third Integer from Client : ",no3)
print("\nCalculating Average of these 3 Integers")
response=(int(no1)+int(no2)+int(no3))/3
connection.send(str(response).encode())
print("\nSent the Average to Client")
sock.close()

client
import socket
import random
sock = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 5001
sock.connect((host, port))
print("Enter first Integer")
num_1 = input()
sock.send(str(num_1).encode())
print("Enter Second Integer")
num_2 = input()
sock.send(str(num_2).encode())
print("Enter Third Integer")
num_3 = input()
sock.send(str(num_3).encode())
print("\nSent all the Three Integers to the Server")
server = sock.recv(1024).decode()
print("\nRecieved the Average of those three numbers from the Server")
print("\nThe Average of the numbers is : ", server)
sock.close()

What happens if you run client first??


If you run TCPClient first, then the client will
attempt to make a TCP connection with a nonexistent server
process. A TCP connection will not
Reg no: 19BCE2215
be made.
As shown below

Reg no: 19BCE2215


What happens when we start server after client??
It shows it keeps on searching the connection

Reg no: 19BCE2215


Starting client after server is shown below
This works perfectly as shown in the below picture …..it
functions perfectly and find the average of three numbers

Reg no: 19BCE2215

You might also like