Swift NWConnection - 接收失败,出现错误“流上没有可用消息”

buz*_*j99 5 networking tcp stream swift

我目前正在开发一个 iOS 应用程序,我需要连接到另一台设备。使用 TCP 成功建立了 NWConnection,我可以从远程设备接收一条(并且只有一条)消息并立即收到错误消息。连接被取消(我猜是由错误引起的)。控制台日志显示:

Hello World.
[connection] nw_flow_add_read_request [C1 <ip and port of remote device> ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, ipv6, dns)] already delivered final read, cannot accept read requests
[connection] nw_read_request_report [C1] Receive failed with error "No message available on STREAM"
error
Run Code Online (Sandbox Code Playgroud)

我的代码如下所示:

Hello World.
[connection] nw_flow_add_read_request [C1 <ip and port of remote device> ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, ipv6, dns)] already delivered final read, cannot accept read requests
[connection] nw_read_request_report [C1] Receive failed with error "No message available on STREAM"
error
Run Code Online (Sandbox Code Playgroud)

远程设备只发送一个包,为了接收更多包,该方法被再次调用。

编辑:我打印了错误消息并得到

error: Optional(POSIXErrorCode: No message available on STREAM)
Run Code Online (Sandbox Code Playgroud)

这似乎是指 POSIXError ENODATA。