1 #模拟QQ聊天-多线程 2 from socket import * 3 from threading import Thread 4 5 def recveData(soc1): 6 while True: 7 recevinfoa=soc1.recvfrom(1024) 8 print(recevinfoa[0].decode('utf-8')) 9 10 def sendData(soc1): 11 while True: 12 info=input('<<') 13 soc1.sendto(info.encode('utf-8'),("192.168.253.1",8080)) 14 15 def main(): 16 soc1 = socket(AF_INET, SOCK_DGRAM) 17 soc1.bind(('',2323)) #这句话不加会报上面第一个截图那个错,我也不知道为啥。而且端口号必须要使用之前没用过的,不然会报上面图二那个错 18 t1=Thread(target=recveData,args=(soc1,)) 19 t2=Thread(target=sendData,args=(soc1,)) 20 21 t1.start() 22 t2.start() 23 24 t1.join() 25 t2.join() 26 27 if __name__ == '__main__': 28 main()