Chat Application Using Python and TCP/IP Protocol and Hamming Code
Chat Application Using Python and TCP/IP Protocol and Hamming Code
s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")\n")
name = input(str("Enter your name: "))
s.listen(1)
print("\nWaiting for incoming connections...\n")
conn, addr = s.accept()
print("Received connection from ", addr[0], "(", addr[1], ")\n")
s_name = conn.recv(1024)
s_name = s_name.decode()
print(s_name, "has connected to the chat room\nEnter [e] to exit chat room\n")
conn.send(name.encode())
def BinaryToDecimal(binary):
return string
def calcRedundantBits(m):
for i in range(m):
if(2**i >= m + i + 1):
return i
# String Concatenation
# (0 to n - 2^r) + parity bit + (n - 2^r + 1 to n)
arr = arr[:n-(2**i)] + str(val) + arr[n-(2**i)+1:]
return arr
def detectAndCorrect(message):
data=list(message)
data.reverse()
c,ch,j,r,error,h,parity_list,h_copy=0,0,0,0,0,[],[],[]
for k in range(0,len(data)):
p=(2**c)
h.append(int(data[k]))
h_copy.append(data[k])
if(p==(k+1)):
c=c+1
startIndex=ph-1
i=startIndex
toXor=[]
while(i<len(h)):
block=h[i:i+ph]
toXor.extend(block)
i+=2*ph
for z in range(1,len(toXor)):
h[startIndex]=h[startIndex]^toXor[z]
parity_list.append(h[parity])
ch+=1
parity_list.reverse()
error=sum(int(parity_list) * (2 ** i) for i, parity_list in enumerate(parity_list[::-1]))
if((error)==0):
print('There is no error in the hamming code received')
temp_data = message[i:i + 7]
decimal_data = BinaryToDecimal(temp_data)
elif((error)>=len(h_copy)):
print('Error cannot be detected')
temp_data = message[i:i + 7]
decimal_data = BinaryToDecimal(temp_data)
else:
print('Error is in',error,'bit')
if(h_copy[error-1]=='0'):
h_copy[error-1]='1'
elif(h_copy[error-1]=='1'):
h_copy[error-1]='0'
print('After correction hamming code is:- ')
h_copy.reverse()
f=int(''.join(map(str, h_copy)))
temp_data = message[i:i + 7]
decimal_data = BinaryToDecimal(temp_data)
while True:
message = input(str("Me : "))
message2=message
data = ''.join(format(ord(i), 'b') for i in message)
m = len(data)
r = calcRedundantBits(m)
conn.send(arr.encode())
conn.send(message2.encode())
#conn.send(message.encode())
message = conn.recv(1024)
message2 = conn.recv(1024)
message=message.decode()
message2=message2.decode()
startIndex=ph-1
i=startIndex
toXor=[]
while(i<len(h)):
block=h[i:i+ph]
toXor.extend(block)
i+=2*ph
for z in range(1,len(toXor)):
h[startIndex]=h[startIndex]^toXor[z]
parity_list.append(h[parity])
ch+=1
parity_list.reverse()
error=sum(int(parity_list) * (2 ** i) for i, parity_list in enumerate(parity_list[::-1]))
if((error)==0):
print('There is no error in the hamming code received')
temp_data = message[i:i + 7]
decimal_data = BinaryToDecimal(temp_data)
elif((error)>=len(h_copy)):
print('Error cannot be detected')
temp_data = message[i:i + 7]
decimal_data = BinaryToDecimal(temp_data)
str_data = str_data + chr(decimal_data)
else:
print('Error is in',error,'bit')
if(h_copy[error-1]=='0'):
h_copy[error-1]='1'
elif(h_copy[error-1]=='1'):
h_copy[error-1]='0'
print('After correction hamming code is:- ')
h_copy.reverse()
f=int(''.join(map(str, h_copy)))
temp_data = message[i:i + 7]
decimal_data = BinaryToDecimal(temp_data)
str_data = str_data + chr(decimal_data)
#message = message.decode()
#print(s_name, ":", message)
def BinaryToDecimal(binary):
def calcRedundantBits(m):
for i in range(m):
if(2**i >= m + i + 1):
return i
# String Concatenation
# (0 to n - 2^r) + parity bit + (n - 2^r + 1 to n)
arr = arr[:n-(2**i)] + str(val) + arr[n-(2**i)+1:]
return arr
def detectError(arr, nr):
n = len(arr)
res = 0
def detectAndCorrect(message):
data=list(message)
data.reverse()
c,ch,j,r,error,h,parity_list,h_copy=0,0,0,0,0,[],[],[]
for k in range(0,len(data)):
p=(2**c)
h.append(int(data[k]))
h_copy.append(data[k])
if(p==(k+1)):
c=c+1
startIndex=ph-1
i=startIndex
toXor=[]
while(i<len(h)):
block=h[i:i+ph]
toXor.extend(block)
i+=2*ph
for z in range(1,len(toXor)):
h[startIndex]=h[startIndex]^toXor[z]
parity_list.append(h[parity])
ch+=1
parity_list.reverse()
error=sum(int(parity_list) * (2 ** i) for i, parity_list in enumerate(parity_list[::-1]))
if((error)==0):
print('There is no error in the hamming code received')
decimal_data = BinaryToDecimal(temp_data)
elif((error)>=len(h_copy)):
print('Error cannot be detected')
temp_data = message[i:i + 7]
decimal_data = BinaryToDecimal(temp_data)
else:
print('Error is in',error,'bit')
if(h_copy[error-1]=='0'):
h_copy[error-1]='1'
elif(h_copy[error-1]=='1'):
h_copy[error-1]='0'
print('After correction hamming code is:- ')
h_copy.reverse()
f=int(''.join(map(str, h_copy)))
temp_data = message[i:i + 7]
decimal_data = BinaryToDecimal(temp_data)
s = socket.socket()
shost = socket.gethostname()
ip = socket.gethostbyname(shost)
print(shost, "(", ip, ")\n")
host = input(str("Enter server address: "))
name = input(str("\nEnter your name: "))
port = 1234
print("\nTrying to connect to ", host, "(", port, ")\n")
time.sleep(1)
s.connect((host, port))
print("Connected...\n")
s.send(name.encode())
s_name = s.recv(1024)
s_name = s_name.decode()
print(s_name, "has joined the chat room\nEnter [e] to exit chat room\n")
while True:
message = s.recv(1024)
message2=s.recv(1024)
message2=message2.decode()
message = message.decode()
print(s_name,":",message2)
print(message," is the hamming code message received")
detectAndCorrect(message)
m = len(data)
r = calcRedundantBits(m)
if message2 == "[e]":
message2 = "Left chat room!"
s.send(message2.encode())
#s.send(arr)
print("\n")
break
s.send(arr.encode())
s.send(message2.encode())
Output 1(Client side) :