-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
63 lines (50 loc) · 2.92 KB
/
Copy pathclient.py
File metadata and controls
63 lines (50 loc) · 2.92 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
"""UDP client: splits a text message into packets, sends them to the server,
and reads back the echoed message line by line."""
import math
import socket
import time
UDP_IP = "127.0.0.1"
UDP_PORT = 12321
PACKET_SIZE = 100
SEQ_NUM_LEN = 4
MESSAGE = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n Aenean commodo ligula eget dolor. Aenean massa. \nCum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.\n Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.\n Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.\n In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.\n Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi.\n Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.\n Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet.\n Etiam ultricies nisi vel augue.Curabitur ullamcorper ultricies nisi.Nam eget dui. Etiam rhoncus."
def build_packets(message):
"""Split the message into fixed-size packets.
Each packet is a header of SEQ_NUM_LEN zero-padded digits, followed by
'#', followed by up to PACKET_SIZE - SEQ_NUM_LEN - 1 characters of the
message. The sequence number counts down so that the final packet carries
sequence number 0, which the server uses to detect the end of the message.
"""
data_size = PACKET_SIZE - SEQ_NUM_LEN - 1
num_packets = math.ceil(len(message) / data_size)
packets = []
for i in range(0, len(message), data_size):
seq = num_packets - (i // data_size) - 1
packets.append(f"{seq:0{SEQ_NUM_LEN}d}#" + message[i:i + data_size])
return packets
def main():
print(f"UDP target: {UDP_IP}:{UDP_PORT}")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP over IPv4
packets = build_packets(MESSAGE)
num_lines = len(MESSAGE.split('\n'))
try:
while True:
print("\nclient starts sending the message to the server")
for packet in packets:
sock.sendto(packet.encode(), (UDP_IP, UDP_PORT))
print(f"\nclient sent packet no. {int(packet[:SEQ_NUM_LEN])}")
time.sleep(1)
print("\nclient finished sending the message to the server")
print("\nclient waits for the echoed lines from the server")
for _ in range(num_lines):
data, _ = sock.recvfrom(10000)
print(f"\nclient received line: {data.decode()}")
print("\nclient received all lines back from the server")
print("\nclient waits 3 seconds before sending again...")
time.sleep(3)
except KeyboardInterrupt:
print("\nclient shutting down")
finally:
sock.close()
if __name__ == "__main__":
main()