PCa: import socket Sockin = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #新建socket Sockin.bind(('192.168.10.5',23000)) #socket绑定该主机的ip和端口 while True: #循环中执行收发功能 text = raw_input('> ') Sockin.sendto(text,(('192.168.10.8',23000))) #将 'text’ 发送给对方 if text == 'q': Scokin.close() #退出时关闭socket break msg,(addr,port) = Sockin.recvfrom(100) # 接受数据 if msg == 'q': Sockin.close() break print msg PCb: import socket Sockin = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) Sockin.bind(('192.168.10.8',23000)) while True: msg,(addr,port) = Sockin.recvfrom(100) if msg == 'q': Sockin.close() break else: print msg text = raw_input('> ') Sockin.sendto(text,(('192.168.10.5',23000))) if text =='q': Sockin.close() break
参考:
https://www.cnblogs.com/xiaokang01/p/9069048.html