-[NSNetService publish]
simply publishes the service; you're responsible for creating a socket and listening on the port you give to the NSNetService initializer. You won't receive netService:didAcceptConnectionWithInputStream:outputStream:
because the NSNetService knows nothing about the socket.
If you want the NSNetService to manage the socket for you—and send didAcceptConnection...
—use -[NSNetService publishWithOptions:]
:
[self.ns publishWithOptions:NSNetServiceListenForConnections];
In this case, you need to make sure that the port with which you initialize the NSNetService is unused. Your getPort
method never closes the socket (and leaks the CFSocketRef), so it will be in use when you publish the service, leading to an error.
I'd recommend that you remove getPort
and, as Apple suggests in NSNetServices.h, "[s]pecify a port number of zero to use a random port."