1

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!

Sheamus
  • 6,506
  • 3
  • 35
  • 61

3 Answers3

1

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

Boris
  • 11,373
  • 2
  • 33
  • 35
  • Yes, I understand that `didAcceptConnectionWithInputStream:outputStream:` must be called from the side accepting the connection. In my case, the server also acts as the client, and still that callback method, `didAcceptConnectionWithInputStream:outputStream:` was never called. – Sheamus Feb 25 '15 at 22:36
1

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.

PKCLsoft
  • 1,359
  • 2
  • 26
  • 35
0

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.

Peter
  • 1