

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()
