-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathudp_client.py
More file actions
executable file
·91 lines (74 loc) · 2.05 KB
/
udp_client.py
File metadata and controls
executable file
·91 lines (74 loc) · 2.05 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
import os
import sys
import socket
import time
if len(sys.argv) < 5:
print("Usage: {} <dest> <pkt-len> <rate> <time> <csv> <count>".format(sys.argv[0]))
sys.exit(0)
udp_port = 6349
dest = sys.argv[1]
length = int(sys.argv[2])
rate = int(sys.argv[3])
timeout = int(sys.argv[4])
csv = int(sys.argv[5])
count = int(sys.argv[6])
ack = "ACK"
stop = "STOP"
interval = 1/(1024*rate/length/8)
message = os.urandom(length)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(.2)
i = 0
if not csv:
print("Sending datagrams of length {} bytes to {}".format(length, dest))
t0 = time.time()
s = interval
end = False
while True:
try:
for j in range(count):
s0 = time.time()
sock.sendto(message, (dest, udp_port))
i += 1
time.sleep(max(0, interval + s))
s = interval - (time.time() - s0)
if timeout and time.time() > t0 + timeout:
end = True
break
if end:
break
try:
d,a = sock.recvfrom(length)
except socket.timeout as e:
pass
else:
if d != bytes(ack, 'UTF-8'):
raise Exception("unexpected ack: '{}'".format(d))
except KeyboardInterrupt:
break
except socket.gaierror as e:
print(e, file=sys.stderr)
sys.exit(1)
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
t1 = time.time()
for j in range(20):
time.sleep(.2)
sock.sendto(bytes(stop, 'UTF-8'), (dest, udp_port))
t = t1 - t0
b = i*length
r = round(i*length*8/1014/t)
if t > 60*60:
time_str = "{}h {:2}m".format(int(t/60/60), int((t/60)%60))
else:
time_str = "{}m {:2}s".format(int(t/60), int(t%60))
if csv:
print("time: {}".format(round(t, 2)))
print("bytes: {}".format(b))
print("packets: {}".format(i))
print("rate: {}".format(r))
else:
print("Sent {} datagrams of total length {} kB in {}".format(i, round(b/1024), time_str))
print("Rate: {} kbit/s".format(r))