-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_client.py
42 lines (29 loc) · 1002 Bytes
/
tcp_client.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
import sys
import socket
import time
import struct
HeaderStruct = struct.Struct("IId")
SERVER_IP = sys.argv[1]
def receive_frame(sock):
# Read message length
header_data = sock.recv(HeaderStruct.size)
seq_id, body_size, timestamp = HeaderStruct.unpack(header_data)
data = bytearray()
while len(data) < body_size:
packet = sock.recv(body_size - len(data))
if not packet:
return None
data.extend(packet)
delay = round((time.time() - timestamp) * 1000, 2)
print(f"Seq {seq_id}, data size = {body_size}, delay = {delay} ms")
def frame_processor_client(server_ip=SERVER_IP, server_port=8003):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((server_ip, server_port))
print("Connected to the server.")
try:
while True:
receive_frame(sock)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
frame_processor_client()