-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.py
64 lines (58 loc) · 1.21 KB
/
receiver.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
#!/usr/bin/python3
from socket import *
from _thread import *
import threading
import sys
import random
import time
import random
expectedseqnum = 0
serverName = '127.0.0.1'
serverPort = 12008
rsock = socket(AF_INET, SOCK_STREAM)
rsock.connect((serverName,serverPort))
rcvpkt = ""
sndpkt = "-1"
recv = []
lasttime = time.time()
f = open("test.txt", "w")
def hasseqnum(rcvpkt):
global recv
recv = rcvpkt.split('\n')
return str(recv[0])
def randomiser():
r = random.randint(0,6)
if r == 0:
return False
else:
return True
def rdt_rcv():
global rcv_sock, recv, expectedseqnum, sndpkt, lasttime, f
print('Data Received:')
while True:
try :
rcvpkt = rsock.recv(1024).decode()
except BrokenPipeError:
sys.exit()
seqnum = hasseqnum(rcvpkt)
if(seqnum == str(expectedseqnum)):
print(recv[1])
sndpkt = str(expectedseqnum)
t = time.time() - lasttime
lasttime = time.time()
f.write(seqnum + " , " + str(t) + " \n")
if randomiser():
try:
rsock.send(sndpkt.encode())
except BrokenPipeError:
sys.exit()
expectedseqnum += 1
else:
if randomiser():
try:
rsock.send(sndpkt.encode())
except BrokenPipeError:
sys.exit()
f.close()
if __name__ == '__main__':
rdt_rcv()