基於TCP協議簡單qq聊天
阿新 • • 發佈:2018-09-02
pre inf 基於 port byte while enc == con
#server端 import socket ip_port = (‘127.0.0.1‘,8080) sk = socket.socket() sk.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) sk.bind(ip_port) sk.listen() conn,addr = sk.accept() while True: msg = conn.recv(1024).decode(‘utf-8‘) if msg == ‘bye‘: conn.send(b‘bye‘) break print(msg) info = input(‘>>>‘) conn.send(info.encode(‘utf-8‘)) conn.close() sk.close()
#client端 import socket sk = socket.socket() sk.connect((‘127.0.0.1‘,8080)) while True: msg = input(‘>>>‘) sk.send(bytes(msg,encoding=‘utf-8‘)) ret = sk.recv(1024).decode(‘utf-8‘)if ret == ‘bye‘: break print(ret) sk.close()
基於TCP協議簡單qq聊天