python udp线程聊天

import socket
import threading

def send_data(udp_socket):
    while True:
        send_data = input("input")
        udp_socket.sendto(send_data.encode("utf-8"), ("127.0.0.1", 7788))

def recv_msg(udp_socket):
    while True:
        recv_data = udp_socket.recvfrom(1024)
        print(recv_data)

def main():
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #绑定本地信息
    udp_socket.bind(("127.0.0.1", 7890))

    t1 = threading.Thread(target=recv_msg, args=(udp_socket,))
    t2 = threading.Thread(target=send_data, args=(udp_socket,))

    t1.start()
    t2.start()

if __name__ == '__main__':
    main()

你可能感兴趣的:(python udp线程聊天)