Python网络编程之UDP

UDP处理数据包,是一种无连接的协议

服务器端:

    server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    # 绑定ip
    server.bind(("192.168.1.104",221))
    while True:
        # 接收
        data,address = server.recvfrom(1024)
        print("客户端:"+data.decode("utf-8"))
        # 发送
        sendData = input("服务器:")
        server.sendto(sendData.encode("utf-8"),address)
        pass

客户端:

    # 创建客户端
    cliet = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    while True:
        # 发送
        sendData = input("客户端:")
        cliet.sendto(sendData.encode("utf-8"),("192.168.1.104",221))
        data,address = cliet.recvfrom(1024)
        print("服务器:"+data.decode("utf-8"))
测试通过


你可能感兴趣的:(Python网络编程之UDP)