服務(wù)器
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)
# 讓服務(wù)器不會關(guān)閉
while True:
conversation, addr = server.accept()
t = CoversationThread(conversation, addr)
t.start()
...nihaoma
('10.7.181.88', 51473) haihao
...
nihaoma
>>>haihao
現(xiàn)在只實現(xiàn)了多人可以向服務(wù)器發(fā)送信息临庇,不會阻塞反璃,但是彈幕還沒有做〖俣幔回去慢慢搞淮蜈。