import socket
Server = socket.socket()
Server.bind(("127.0.0.1",9099))
Server.listen(2)
conn,address = Server.accept()
while True:
data = conn.recv(1024)
print(data.decode(encoding="utf-8"))
msg = input("请输入你想给客户端发送的消息:")
if msg == "exit":
break
conn.sendall(msg.encode("utf-8"))
Server.close()
import socket
Client = socket.socket()
Client.connect(("127.0.0.1",9099))
while True:
msg = input("请输入你想给客户端发送的消息:")
if msg == "exit":
break
Client.sendall(msg.encode("utf-8"))
data = Client.recv(1024)
print(data.decode(encoding="utf-8"))
Client.close()
分别同时进行可以进行聊天,也可以跟自己小伙伴聊天。