使用Network.framework检测与UDP断开连接的客户端

时间:2019-01-26 03:35:42

标签: swift server-side-swift network.framework

我正在尝试确定使用Network.framework时UDP客户端何时停止向服务器发送数据包

我建立了一个小示例,演示了取消客户端连接后服务器无法将状态更改为.cancelled

示例客户端:

import Foundation
import Network

func sendMessage(on connection: NWConnection) {
    connection.send(content: "hello".data(using: .utf8), completion: .contentProcessed({error in
        if let error = error {
            print("error while sending hello: \(error)")
            return

        }

        connection.receiveMessage {data, context, isComplete, error in
            if let error = error {
                print("error while receiving reply: \(error)")
                return

            }

            connection.cancel()

        }

    }))
}

var connection: NWConnection = {
    let connection = NWConnection(
        to: .service(
            name: "Hello",
            type: "_test._udp",
            domain: "local",
            interface: nil
        ),
        using: .udp
    )

    connection.stateUpdateHandler = {newState in
        switch newState {
        case .ready:
            sendMessage(on: connection)
        case .failed(let error):
            print("client failed with error: \(error)")
        case .cancelled:
            print("Cancelled connection")
        default:
            break
        }
    }

    return connection
}()

connection.start(queue: DispatchQueue(label: "test"))

RunLoop.main.run()

示例服务器:

import Foundation
import Network

func receive(on connection: NWConnection) {
    connection.receiveMessage { (data, context, isComplete, error) in
        if let error = error {
            print(error)
            return

        }

        connection.send(content: "world".data(using: .utf8), completion: .contentProcessed({error in
            if let error = error {
                print("error while sending data: \(error)")
                return

            }

        }))

        receive(on: connection)

    }

}

var listener: NWListener = {
    let listener = try! NWListener(using: .udp)

    listener.service = NWListener.Service(name: "Hello", type: "_test._udp", domain: nil, txtRecord: nil)
    listener.newConnectionHandler = {newConnection in
        newConnection.stateUpdateHandler = {newState in
            switch newState {
            case .ready:
                receive(on: newConnection)
            case .failed(let error):
                print("client failed with error: \(error)")
            case .cancelled:
                print("Cancelled connection")
            default:
                break
            }
        }

        newConnection.start(queue: DispatchQueue(label: "new client"))


    }
    return listener
}()

listener.start(queue: DispatchQueue(label: "test"))

RunLoop.main.run()

在服务器运行时运行客户端时,客户端发送和接收一个数据包,然后被取消。客户端打印Connection cancelled。但是,服务器上NWConnection的状态不会更改,并且connection.receiveMessage会在没有任何数据要从客户端读取时静默失败。

我希望服务器连接的状态发生改变,或者receiveMessage调用其完成处理程序,尽管不存在任何数据(毕竟dataData?

因此,我不确定在Network.framework上使用UDP服务器时如何检测客户端何时停止发送数据包。我应该如何检测“断开连接”的客户端?

1 个答案:

答案 0 :(得分:1)

UDP服务器无法通过网络获得有关UDP客户端是否已断开连接或消失的信息,除非客户端可能明确地(通过UDP,TCP或其他辅助通道)发送某种有关其断开连接状态的附加消息。因此,没有什么可以更改NWConnection状态的(服务器本身可能存在某种问题除外)。

也许在经过某种协​​商或协商的超时时间之后,服务器可以断线,而没有进行任何活动。或数据包数量,数据字节。等等,然后关闭连接本身。