My first question is how do I get the didAcceptConnectionWithInputStream:outputStream:
callback in NSNetServiceDelegate
to get called?
Follow up question: can I still establish a connection between a client and server, although I never get a callback saying that a connection was accepted (via didAcceptConnectionWithInputStream:outputStream:
)?
I understand that calling publishWithOptions
, while passing in the NSNetServiceListenForConnections
option is supposed to result in the NetServiceDelegate
callback ( didAcceptConnectionWithInputStream:outputStream:
) to be called. However, that callback is not getting called.
Here are the steps I am taking, to publish:
Create NSNetService
with
self.netService = [[NSNetService alloc] initWithDomain:@"" type:_serviceType name:(_name == nil) ? @"" : _name port:0];
Schedule service in current runloop, in default mode
publishWithOptions:NSNetServiceListenForConnections
Here are the steps I take, to browse services:
NSNetServiceBrowser
, and set its delegate to my client wrapper object searchForServicesOfType
for the same service type and domain as NSNetService
UITableView
for the UI, to allow a user to select a service getInputStream:outputSteam:
on the service didAcceptConnectionWithInputStream:outputStream:
to get called. However this does not occur. Thanks for your help!
The problem is that didAcceptConnectionWithInputStream:outputStream:
must be called from the side accepting the connection.
Once the service is available, you call get the streams
[service getInputStream:&istream outputStream:&ostream]
Once this happens on the side receiving the request the delegate method
- (void)netService:(NSNetService *)sender didAcceptConnectionWithInputStream:(NSInputStream *)inputStream outputStream:(NSOutputStream *)outputStream
will be called
In my experience, it is not the act of calling getInputStream:outputStream:
on the client that causes didAcceptConnectionWithInputStream:outputStream:
to be called on the server.
On the client, after calling getInputStream:outputStream:
, your client then needs to call [inputStream open]
and [outputStream open]
before the didAcceptConnectionWithInputStream:outputStream:
will be called.
It's all a part of lazy initialization.
Calling getInputStream:outputStream:
will give you back two perfectly good NSStreams ready to use. So, say, you want to write some data? First, open the write stream...
BAM! netService:didAcceptConnectionWithInputStream:outputStream:
is called.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.