服务器
import socket
if __name__ == '__main__':
client = socket.socket()
client.connect(('10.7.181.88', 8081))
while True:
# 接收消息并且打印出来
print(client.recv(1024).decode(encoding='utf-8'))
message = input('>>>')
client.send(message.encode())
客户端
import socket
from threading import Thread
class CoversationThread(Thread):
def __init__(self, conversation: socket.socket, addr):
super().__init__()
self.conversation = conversation
self.addr = addr
def run(self):
while True:
message = input('...')
conversation.send(message.encode())
print(self.addr, self.conversation.recv(1024).decode(encoding='utf=8'))
if __name__ == '__main__':
server = socket.socket()
server.bind(('10.7.181.88', 8081))
server.listen(50)
# 让服务器不会关闭
while True:
conversation, addr = server.accept()
t = CoversationThread(conversation, addr)
t.start()
...nihaoma
('10.7.181.88', 51473) haihao
...
nihaoma
>>>haihao
现在只实现了多人可以向服务器发送信息,不会阻塞,但是弹幕还没有做。回去慢慢搞。