1 import threading 2 import socket 3 4 def recv_msg(udp_socket): 5 recv_data = udp_socket.recvfrom(1024) 6 print(recv_data) 7 8 def send_msg(udp_socket): 9 while True: 10 send_data = input('请输入你要发送的内容:') 11 udp_socket.sendto(send_data.encode('utf-8'), ('192.168.0.106', 8080)) 12 13 def main(): 14 udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 15 udp_socket.bind(('192.168.0.106', 7878)) 16 t1 = threading.Thread(target=send_msg, args=(udp_socket,)) 17 t2 = threading.Thread(target=recv_msg, args=(udp_socket,)) 18 t1.start() 19 t2.start() 20 21 22 if __name__ == '__main__': 23 main()