forked from sharunrajeev/YourFirstContribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhammingServer.py
66 lines (63 loc) · 1.71 KB
/
hammingServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Server code
import socket
import random
def calRedundantBits(m):
for i in range(m):
if(2**i >= m):
return i
def detectError(arr, nr):
n = len(arr)
res = 0
for i in range(nr):
val = 0
for j in range(1, n + 1):
if(j & (2**i) == (2**i)):
val = val ^ int(arr[-1 * j])
res = res + val*(10**i)
if (res==0):
return (int(str(res),2))
else:
return (n-int(str(res), 2)+1)
s=socket.socket()
port=12345
s.bind(('127.0.0.1',port))
print(f"Connected to port {port}")
s.listen(5)
while True:
k,a=s.accept()
print (f"Connected to {a}")
data=k.recv(1024).decode()
print("When no disturbance occured during transmission\nMessage from client:", data)
m=len(data)
nr=calRedundantBits(m)
correction = detectError(data, nr)
if (correction==0):
msg=f"Data received : {data} ; No error"
k.send(msg.encode())
else:
data1=''
if (data[correction-1]=='0'):
data1=data[:correction-1]+"1"+data[correction:]
else:
data1=data[:correction-1]+"0"+data[correction:]
msg=f"Data received is {data} ; Error found at position {correction} ; Corrected data is {data1}"
k.send(msg.encode())
rand=random.randint(1,m)
ndata=''
if (data[rand-1]=='0'):
ndata=data[:rand-1]+"1"+data[rand:]
else:
ndata=data[:rand-1]+"0"+data[rand:]
print(f"When disturbance occured during transmission\nMessage from client: {data}")
correction = detectError(ndata, nr)
if (correction==0):
msg=f"Data received is {ndata} ; No error"
k.send(msg.encode())
else:
data1=''
if (ndata[correction-1]=='0'):
data1=ndata[:correction-1]+"1"+ndata[correction:]
else:
data1=ndata[:correction-1]+"0"+ndata[correction:]
msg=f"Data received : {ndata} ; Error found at position {correction} ; Corrected data is {data1}"
k.send(msg.encode())