import socket import tcp_client_image SERVER_ADDRESS = ('172.16.0.249', 8888) client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(SERVER_ADDRESS) try: while True: message = "" choice = int(input("image > ")) if choice == 1: message = b"image" + bytes(tcp_client_image.image1) elif choice == 2: message = b"image" + bytes(tcp_client_image.image2) elif choice == 3: message = b"ping" else: message = b"something_invalid" # 4. 傳送資料 (需要編碼成 bytes) client_socket.sendall(message) # 5. 接收伺服器的回應 # 接收最多 1024 bytes data = client_socket.recv(1024) if data: print(f"收到伺服器訊息: {data.decode('utf-8')}") else: print("伺服器已關閉連線。") break except ConnectionResetError: print("連線被伺服器重置。") finally: client_socket.close() print("客戶端連線已關閉。")