gpt4 book ai didi

swift - 将 NWConnection 用于长时间运行的 TCP 套接字的正确方法

转载 作者:行者123 更新时间:2023-12-01 11:06:21 25 4
gpt4 key购买 nike

我一整天都在与 NWConnection 斗争以在长时间运行的 TCP 套接字上接收数据。由于缺乏文档,在对自己造成以下错误后,我终于让它工作了:

  • 数据不完整(因为只调用了一次接收)
  • 乱序获取 TCP 数据(由于“轮询”从计时器接收...导致多个同时关闭以等待获取数据)。
  • 遭受无限循环(由于在接收后重新启动接收而不检查“isComplete”Bool——一旦套接字从另一端终止,这是......坏......非常糟糕)。

  • 我所学到的总结:
  • 一旦您处于 .ready 状态,您就可以调用接收...一次且仅一次
  • 一旦您收到一些数据,您就可以再次调用接收...但前提是您仍处于 .ready 状态并且 isComplete 为 false。

  • 这是我的代码。我认为这是对的。但如果有误,请告诉我:
        queue = DispatchQueue(label: "hostname", attributes: .concurrent)
    let serverEndpoint = NWEndpoint.Host(hostname)
    guard let portEndpoint = NWEndpoint.Port(rawValue: port) else { return nil }
    connection = NWConnection(host: serverEndpoint, port: portEndpoint, using: .tcp)
    connection.stateUpdateHandler = { [weak self] (newState) in
    switch newState {
    case .ready:
    debugPrint("TcpReader.ready to send")
    self?.receive()
    case .failed(let error):
    debugPrint("TcpReader.client failed with error \(error)")
    case .setup:
    debugPrint("TcpReader.setup")
    case .waiting(_):
    debugPrint("TcpReader.waiting")
    case .preparing:
    debugPrint("TcpReader.preparing")
    case .cancelled:
    debugPrint("TcpReader.cancelled")
    }
    }

    func receive() {
    connection.receive(minimumIncompleteLength: 1, maximumLength: 8192) { (content, context, isComplete, error) in
    debugPrint("\(Date()) TcpReader: got a message \(String(describing: content?.count)) bytes")
    if let content = content {
    self.delegate.gotData(data: content, from: self.hostname, port: self.port)
    }
    if self.connection.state == .ready && isComplete == false {
    self.receive()
    }
    }
    }

    最佳答案

    我认为您可以多次使用短时间连接。例如,客户端连接到主机并要求主机做某事,然后告诉主机关闭连接。主机切换到等待模式以准备新的连接。见下图。

    当客户端在特定时间内未向主机发送关闭连接或应答事件时,您应该使用连接计时器来关闭打开的连接。

    关于swift - 将 NWConnection 用于长时间运行的 TCP 套接字的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54976513/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com