-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_server.py
45 lines (33 loc) · 996 Bytes
/
tcp_server.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
import sys
import socket
import time
import struct
HeaderStruct = struct.Struct("IId")
SERVER_IP = "0.0.0.0"
FRAME_SIZE_BYTES = int(sys.argv[2])
N = 0
def send_frame(sock, frame_data):
global N
header_data = HeaderStruct.pack(N, len(frame_data), time.time())
sock.sendall(header_data + frame_data)
print(f"Sent frame {N}")
N += 1
def frame_producer_server(port=8003):
frame_data = b"\0" * FRAME_SIZE_BYTES
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((SERVER_IP, port))
server_socket.listen()
print("Waiting for a connection...")
client_socket, addr = server_socket.accept()
print(f"Connected to {addr}")
try:
while True:
send_frame(client_socket, frame_data)
time.sleep(1 / 20) # 20 fps
except KeyboardInterrupt:
pass
finally:
client_socket.close()
server_socket.close()
if __name__ == "__main__":
frame_producer_server()