Server and client development
parent
72bfab99a1
commit
d7812173fb
|
@ -0,0 +1,20 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
import socket
|
||||||
|
import struct
|
||||||
|
import time
|
||||||
|
|
||||||
|
HOST = '127.0.0.1'
|
||||||
|
PORT = 5273
|
||||||
|
|
||||||
|
def send_message(sock, data: bytes):
|
||||||
|
header = struct.pack('!I', len(data))
|
||||||
|
sock.sendall(header + data)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
with socket.create_connection((HOST, PORT)) as s:
|
||||||
|
send_message(s, b'hello binary \x00\x01')
|
||||||
|
time.sleep(0.2)
|
||||||
|
send_message(s, b'\xff'*1024) # 发送 1KB 二进制
|
||||||
|
time.sleep(0.5)
|
||||||
|
send_message(s, b'last msg')
|
||||||
|
# 可关闭连接或继续发送更多消息
|
|
@ -0,0 +1,65 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
import struct
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
|
HOST = '0.0.0.0'
|
||||||
|
|
||||||
|
def recv_exact(conn, n):
|
||||||
|
"""Keep reading until exactly n bytes are received, or return None on EOF"""
|
||||||
|
buf = bytearray()
|
||||||
|
while len(buf) < n:
|
||||||
|
chunk = conn.recv(n - len(buf))
|
||||||
|
if not chunk:
|
||||||
|
return None
|
||||||
|
buf.extend(chunk)
|
||||||
|
return bytes(buf)
|
||||||
|
|
||||||
|
def handle_client(conn, addr):
|
||||||
|
print(f'Connected: {addr}')
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
header = recv_exact(conn, 4)
|
||||||
|
if header is None:
|
||||||
|
print(f'Client {addr} disconnected')
|
||||||
|
break
|
||||||
|
length = struct.unpack('!I', header)[0] # Network byte order (big-endian)
|
||||||
|
if length == 0:
|
||||||
|
# Optional: length 0 means heartbeat or empty message
|
||||||
|
print(f'Received an empty message from {addr}')
|
||||||
|
continue
|
||||||
|
data = recv_exact(conn, length)
|
||||||
|
if data is None:
|
||||||
|
print(f'Client {addr} disconnected prematurely while reading data')
|
||||||
|
break
|
||||||
|
# Handle binary data here (may take time)
|
||||||
|
print(f'Received {len(data)} bytes of data from {addr}')
|
||||||
|
print(data)
|
||||||
|
# Simulate time-consuming processing (replace with real logic)
|
||||||
|
# time.sleep(1) # Even if you have long tasks, other connections are still accepted
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Exception occurred while handling client {addr}: {e}')
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
print(f'Closed connection {addr}')
|
||||||
|
|
||||||
|
def run_server(PORT):
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
sock.bind((HOST, PORT))
|
||||||
|
sock.listen()
|
||||||
|
print(f'Server listening on {HOST}:{PORT}...')
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
conn, addr = sock.accept()
|
||||||
|
t = threading.Thread(target=handle_client, args=(conn, addr), daemon=True)
|
||||||
|
t.start()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print('Server shutting down...')
|
||||||
|
finally:
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run_server(int(sys.argv[1]))
|
Loading…
Reference in New Issue