-
Notifications
You must be signed in to change notification settings - Fork 0
/
recv.py
76 lines (57 loc) · 2.14 KB
/
recv.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
67
68
69
70
71
72
73
74
75
76
import socket
class receiver:
# Initialization
def __init__(self, port):
self.socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
self.socket.settimeout(50)
self.socket.bind(('127.0.0.1', port))
self.last_seq = 1
# Receiving and checking the message
# If message is currpoted, ask for resend
def recv_msg(self):
i = 0
flag = True
while flag and i<5:
msg, addr = self.socket.recvfrom(1024)
if self.check_msg(msg) and addr == self.target_addr:
self.last_seq = self.last_seq + len(msg[3:])
print("Message Received: ", end="")
flag = False
else:
print("Curropt Message Received : Asking for resend")
self.socket.sendto(str(self.last_seq).encode(), addr)
i = i + 1
if flag:
return ""
# If the client want to close the connection
if msg[3:].decode('utf-8') == "Chal Beta Agla Laga":
self.close()
raise Exception("Connection Closed From the Sender")
return msg[3:].decode('utf-8')
# Check the integrity with the help of checksum
def check_msg(self, data):
check = 0
for byte in data[3:]:
check = check | byte
if check == data[2]:
return True
return False
# Vanilla Receive system
def recv(self):
return self.recv_msg()
# Listen and connect with the client
def listen(self):
msg, addr = self.socket.recvfrom(1024)
if self.check_msg(msg):
self.last_seq = self.last_seq + len(msg[3:])
self.target_addr = addr
print("Connection Established With : " + str(addr))
self.socket.sendto(str(self.last_seq).encode(), addr)
# Close the connection
def close(self):
self.socket.close()
# Infinite loop to receive the message
shit = receiver(12345)
shit.listen()
while True:
print(shit.recv())