Just try Bonjour in fast 3
Here is my code, I can get a delegate
func netServiceDidResolveAddress(_ sender: NetService) { print("netServiceDidResolveAddress service name \(sender.name) of type \(sender.type)," + "port \(sender.port), addresses \(sender.addresses)") }
And here is my result
netServiceDidResolveAddress Webber service name Mac mini type _myapp._tcp., port 5678, addresses Optional ([<<1002162e c0a80205 00000000 00000000>, <1c1e162e 00000000 fe800000 00000000 00bce7ad 24b4b7e8 08000000>])
c0a80205 is the IP address I'm looking for => 192.168.2.5
And the address is [Data], Apple says
Service Addresses These are NSArray instances of NSData, each of which contains one structural sockaddr, suitable for use with connection (2). If the addresses are not allowed for the service or the service is not yet resolved, an empty NSArray is returned.
I'm still confused why data cannot use .btyes? As Apple says: "This is an NSArray of NSData instances" But I can't use it like NSData p>
And how to resolve the address as a readable IP string?
I have tried this before, but am not getting the result as I rule out ...
let thedata = NSData(bytes: sender.addresses, length: (sender.addresses?.count)!) var storage = sockaddr_storage() thedata.getBytes(&storage, length: sizeof(sockaddr_storage)) if Int32(storage.ss_family) == AF_INET { let addr4 = withUnsafePointer(&storage) {UnsafePointer<sockaddr_in>($0).pointee } print(inet_ntoa(addr4.sin_addr)); }
Any suggestion will help, thanks