Hi, I've constructed a NWListener closely based on the example provided here: https://developer.apple.com/forums/thread/653925?login=true&page=1#621284022
The problem is that the example specifically only handles a single connection request and then ignores all others. I've removed that logic. The problem is that for
code running on two simulators (tvOS and iOS) I get four accepts each time I connect from the client. They are coming in pairs of ipv4/ipv6. The second address might be the simulator,
.0.5 is the mac. I need to implement a server that accepts as many connections over time as needed. My questions are
b) if not how should I ignore the other connection? I don't seem to get information into newConnectionHandler, which is too late.
c) why am I getting a second pair of connections?
Thanks in advance for your help!
Cliff
Code Block func startListener() { do { let tcpOption = NWProtocolTCP.Options() tcpOption.enableKeepalive = true tcpOption.keepaliveIdle = 2 let params = NWParameters(tls: nil, tcp: tcpOption) params.includePeerToPeer = true let listener = try NWListener(using: params) networkListener = listener listener.service = NWListener.Service(name: NetworkConstants.serviceName, type: NetworkConstants.serviceType) listener.newConnectionLimit = NWListener.InfiniteConnectionLimit listener.stateUpdateHandler = { [weak self] newState in guard let strongSelf = self else { return } switch newState { case .ready: let listenerMessage = "listening on \(NetworkConstants.serviceName) \(NetworkConstants.serviceType) \(String(describing: strongSelf.networkListener!.port))" strongSelf.networkDelegate?.didReceiveListenerUpdate(listener: strongSelf, didUpdateMessage: listenerMessage) case .failed(let error): strongSelf.networkListener?.cancel() if strongSelf.didListenerFail { print("listener did fail") strongSelf.didListenerFail = true NetworkListener.shared.startListener() } else { let errorMessage = "Listener - failed with \(error.localizedDescription), restarting" strongSelf.networkDelegate?.didReceiveListenerUpdate(listener: strongSelf, didUpdateMessage: errorMessage) } default: print("listener: unhandled state \(newState)") break } } listener.newConnectionHandler = { [weak self] newConnection in guard let strongSelf = self else { return } strongSelf.networkDelegate?.didReceiveListenerUpdate(listener: strongSelf, didUpdateMessage: "Listener received a new connection") strongSelf.networkDelegate?.didReceiveNewConnection(listener: strongSelf, newConnection: newConnection) } listener.start(queue: .main) } catch { print("Can't make listener: \(error.localizedDescription)") } }
listener: ScreenshotServer.NetworkListener: Listener received a new connection
listener: ScreenshotServer.NetworkListener: new connection [C1 ::1.52396 tcp, local: ::1.52309, server, prohibit joining, path satisfied (Path is satisfied), interface: lo0]
listener: ScreenshotServer.NetworkListener: Listener received a new connection
listener: ScreenshotServer.NetworkListener: new connection [C2 192.168.0.5:52397 tcp, local: 192.168.0.5:52309, server, prohibit joining, path satisfied (Path is satisfied), interface: lo0]
listener: ScreenshotServer.NetworkListener: Listener received a new connection
listener: ScreenshotServer.NetworkListener: new connection [C3 192.168.0.2:52398 tcp, local: 192.168.0.2:52309, server, prohibit joining, path satisfied (Path is satisfied), interface: lo0]
listener: ScreenshotServer.NetworkListener: Listener received a new connection
listener: ScreenshotServer.NetworkListener: new connection [C4 169.254.240.25:52399 tcp, local: 169.254.240.25:52309, server, prohibit joining, path satisfied (Path is satisfied), interface: lo0]