-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUDPHeartBeatServer.py
76 lines (68 loc) · 2.27 KB
/
UDPHeartBeatServer.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
# UDPHeartBeatServer.py
# We will need the following module to generate randomizedlost packets
from socket import *
import time
import random
# What's your IP address and witch port should we use?
recieve_host = '127.0.0.1'
recieve_port = 12000
# What's the remote host's IP address and witch port should we use?
remote_host = '127.0.0.1'
remote_port = 1024
# Create a UDP socket
# Notice the use of SOCK_DGRAM for UDP packets
serverSocket = socket(AF_INET, SOCK_DGRAM)
# Assign IP address and port number to socket
serverSocket.bind((recieve_host, recieve_port))
simulate_packet_loss = True
sleep_for_rand_response_times = True
sequence_number = 0
recieved_time = 0
def get_time():
return int(round(time.time() * 1000))
def send_message(message,wait=False):
serverSocket.sendto(message, (remote_host, remote_port))
if wait == False:
return
else:
return wait_for_response()
# Just respond to ping requests
while True:
# Receive the client packet along with the address it is coming from
message, address = serverSocket.recvfrom(remote_port)
print get_time() - recieved_time
if recieved_time !=0 and get_time() - recieved_time > 5000:
print 'Client disconnect (timeout)'
sequence_number = 0
recieved_time = 0
# Capitalize the message from the client
message = message.upper()
recieved_size = len(message)
recieved_array = message.split(' ')
recieved_type = recieved_array[0].upper()
#print recieved_type
recieved_seq = int(recieved_array[1])
recieved_time = int(recieved_array[2])
if recieved_seq != sequence_number+1:
if sequence_number != 0:
for i in range(sequence_number, recieved_seq):
print 'Dropped Packet:' + str(i)
if sequence_number == 0:
print 'Client connect.'
sequence_number = recieved_seq
print 'Recieve: ' + message
# If rand is less is than 4, we consider the packet lost and do not respond
if sleep_for_rand_response_times:
min_sleep = 0.2
max_sleep = 1.0
time.sleep(random.uniform(min_sleep, max_sleep))
if simulate_packet_loss:
if random.randint(0, 10) < 2:
print 'Dropped, lol'
continue
elif simulate_packet_loss:
if random.randint(0, 10) < 4:
print 'Dropped, lol'
continue
serverSocket.sendto(message, address)
print 'Send: ' + message