客户端
import socket# 创建socket对象
socket_client = socket.socket()
# 链接服务器
socket_client.connect(("localhost", 19999))
发消息
while True:# 发消息msg = input("输入:")if msg == 'exit':breaksocket_client.send(msg.encode("UTF-8"))# 接收消息recv_data = socket_client.recv(1024).decode("UTF-8")print(f"收到消息:{recv_data}")
关闭链接
socket_client.close()
服务端
客户端