I need to implement a custom discovery protocol using UDP. It works like this:
You broadcast a beacon to a broadcast address (like 255.255.255.255).
You wait for clients that respond to your beacon.
Sending
I sent the beacon like this:Code Block let connection = NWConnection( to: .hostPort( host: .ipv4(.broadcast), port: .init(integerLiteral: discoveryPort) ), using: .udp ) connection.start(queue: .main) connection.send( content: beacon, completion: .contentProcessed(sendCompleted) )
Receiving
To receive the responses from the clients I useCode Block connection.receiveMessage { (message: Data?, context: NWConnection.ContentContext?, isComplete: Bool, optionalError: NWError?) in // Great I got a response... // But how do I know where this is coming from? }