简体   繁体   中英

How does didAcceptConnectionWithInputStream:outputStream: get called?

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:

  1. Create NSNetService with

    self.netService = [[NSNetService alloc] initWithDomain:@"" type:_serviceType name:(_name == nil) ? @"" : _name port:0];

  2. Schedule service in current runloop, in default mode

  3. Set the delegate to my Server wrapper object
  4. call publishWithOptions:NSNetServiceListenForConnections

Here are the steps I take, to browse services:

  1. Create an NSNetServiceBrowser , and set its delegate to my client wrapper object
  2. Call searchForServicesOfType for the same service type and domain as NSNetService
  3. List services in a UITableView for the UI, to allow a user to select a service
  4. When a user selects a service, set the service's delegate to my client object, and call getInputStream:outputSteam: on the service
  5. After getInputStream:outputSteam: returns success, I would expect 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM