多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
#### 1. 任务要求: 使用多线程完成一个全双工的QQ聊天程序 #### 2. 运行现象如下 ![](https://box.kancloud.cn/2adab1d88535ced4f80728e9d5c9c9ef_1645x901.png) ~~~ from threading import Thread from socket import * #1. 收数据,然后打印 def recvData(): while True: recvInfo = udpSocket.recvfrom(1024) print(">>%s:%s"%(str(recvInfo[1]), recvInfo[0])) #2. 检测键盘,发数据 def sendData(): while True: sendInfo = input("<<") udpSocket.sendto(sendInfo.encode("gb2312"), (destIp, destPort)) udpSocket = None destIp = "" destPort = 0 def main(): global udpSocket global destIp global destPort destIp = input("对方的ip:") destPort = int(input("对方的ip:")) udpSocket = socket(AF_INET, SOCK_DGRAM) udpSocket.bind(("", 4567)) tr = Thread(target=recvData) ts = Thread(target=sendData) tr.start() ts.start() tr.join() ts.join() if __name__ == "__main__": main() ~~~