Hi,
I'm starting a project that needs to detect some hardware on the network using Bonjour and then connect and send messages.
I started using the TicTacToe example from WWDC as my basis and i'm able to start a browser and detect my devices on the network.
In my browseResultsChangedHandler i can see new service endpoints being added using the following code block.
I'm using the array of NWBrowser.Result to populate a tableview and then segue to a new view when they select a row. As part of this I pass the browser result corresponding to the selected row to the new view.
When I reach the new view I try to start a connection:
However when attempting to start the connection it fails with a socket error:
nw_socket_handle_socket_event [C1.1.1:1] Socket SO_ERROR [61: Connection refused]
After some detective work using Xcode's network activity report, i discovered that this is because it is attempting to connect on port 80 rather than the correct port of 3134. It does however get the IP address correct.
If I manually start an NWConnection using hardcoded values in place of the detected endpoint the connection works and I can send/receive data:
I'm new to both networking and swift so fully expect i've done something wrong. Any advice would be greatly appreciated.
I'm starting a project that needs to detect some hardware on the network using Bonjour and then connect and send messages.
I started using the TicTacToe example from WWDC as my basis and i'm able to start a browser and detect my devices on the network.
In my browseResultsChangedHandler i can see new service endpoints being added using the following code block.
Code Block swift case .added(let browseResult): switch browseResult.endpoint { case .hostPort(let host, let port): print("added hostPort \(host) \(port)") case .service(let name, let type, let domain, _): print("added service: \(name)\n with type: \(type)\n on domain: \(domain)") default: print("fail") }
I'm using the array of NWBrowser.Result to populate a tableview and then segue to a new view when they select a row. As part of this I pass the browser result corresponding to the selected row to the new view.
When I reach the new view I try to start a connection:
Code Block swift var browserResult: NWBrowser.Result? override func viewDidLoad() { super.viewDidLoad() /* Do any additional setup after loading the view. */ if (browserResult != nil) { let endpoint = browserResult!.endpoint if case let NWEndpoint.service(name: name, type: _, domain: _, interface: _) = endpoint { connectedDeviceLabel.text = name } } ConnectToDevice() } func ConnectToDevice() { if (browserResult != nil) { connection = NetworkConnection(endpoint: browserResult!.endpoint, interface: browserResult!.interfaces.first) connection?.delegate = self } } /* NetworkConnection */ init(endpoint: NWEndpoint, interface: NWInterface?) { /* Create parameters, and allow browsing over peer-to-peer link. */ let tcpOptions = NWProtocolTCP.Options() tcpOptions.enableKeepalive = true tcpOptions.keepaliveIdle = 2 let parameters = NWParameters(tls: nil, tcp: tcpOptions) parameters.includePeerToPeer = true parameters.allowLocalEndpointReuse = true let connection = NWConnection(to: endpoint, using: parameters) self.connection = connection startConnection_B() } func startConnection_B() { guard let connection = connection else { return } connection.stateUpdateHandler = { newState in switch newState { case .ready: print("\(connection) established") /* When the connection is ready, start receiving messages. */ self.receiveNextMessage() /* Notify delegate that the connection is ready. */ if let delegate = self.delegate { delegate.connectionReady() } case .failed(let error): print("\(connection) failed with \(error)") /* Cancel the connection upon a failure. */ connection.cancel() default: break } } /* Start the connection establishment. */ connection.start(queue: .main) }
However when attempting to start the connection it fails with a socket error:
nw_socket_handle_socket_event [C1.1.1:1] Socket SO_ERROR [61: Connection refused]
After some detective work using Xcode's network activity report, i discovered that this is because it is attempting to connect on port 80 rather than the correct port of 3134. It does however get the IP address correct.
If I manually start an NWConnection using hardcoded values in place of the detected endpoint the connection works and I can send/receive data:
Code Block swift let connection = NWConnection(host: "192.168.1.93", port: 3134, using: .tcp)
I'm new to both networking and swift so fully expect i've done something wrong. Any advice would be greatly appreciated.