Python Udp多线程聊天器demo

import socket, threading

def udp_out(udp_socket):
    while True:
        my_conten = input('客户端:').encode('gbk')
        udp_socket.sendto(my_conten, ('192.168.191.1', 7080))


def udp_in(udp_socket):
    while True:
        my_concent, se_ip = udp_socket.recvfrom(1024)
        my_scvse = my_concent.decode('gbk')
        print(my_scvse)


#程序入口
if __name__ == '__main__':
    #创建socket
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #绑定信息
    udp_socket.bind(('', 6644))
    #创建线程
    udp_out_thread = threading.Thread(target=udp_out, args=(udp_socket, ))

    udp_in_thread = threading.Thread(target=udp_in, args=(udp_socket,))
    #启动线程
    udp_in_thread.start()
    udp_out_thread.start()

猜你喜欢

转载自blog.csdn.net/qq_38691608/article/details/80020824