import threading
from _socket import AF_INET, SOCK_DGRAM
from socket import socket
def send_data(socket, ):
while True:
print('开启发送数据线程--------')
data = input('发送数据:')
socket.sendto(data.encode('gbk'), ('192.168.1.24', 8080))
def recv_data(socket, ):
while True:
print('开启接受数据线程--------')
data = socket.recvfrom(1024)
print('{} {}'.format(data[0].decode('gbk'), data[1]))
def main():
'''完成udp聊天器的整体控制'''
# 创建套接字
udp_socket = socket(AF_INET, SOCK_DGRAM)
# 绑定ip
udp_socket.bind(("", 8081))
# 发送数据线程和接收数据线程
# send_data(udp_socket)
# recv_data(udp_socket)
thread_send = threading.Thread(target=send_data, args=(udp_socket, ))
thread_recv = threading.Thread(target=recv_data, args=(udp_socket, ))
thread_send.start()
thread_recv.start()
# udp_socket.close()
if __name__ == "__main__":
main()