nssocketport

How to find proper port for NSNetService on iOS?

混江龙づ霸主 提交于 2019-12-10 18:26:54
问题 I am making an app with two components, an iPhone component and a Mac component. They are supposed to communicate with each other via bonjour. I use the following code on the Mac end to find a port for the service: NSSocketPort *socket = [[NSSocketPort alloc] init]; struct sockaddr *addr = (struct sockaddr *)[[socket address] bytes]; int port = 9876; if(addr->sa_family == AF_INET) { port = ntohs(((struct sockaddr_in *)addr)->sin_port); } else if(addr->sa_family == AF_INET6) { port = ntohs((