1

I'm kinda new to Objective-C / Cocoa, and I'm trying to write a bonjour server-client app. Because I don't have much experience, I would appreciate if your replies could also include explanations on how to do it, and not just the code.

I manage to establish a connection, and input-output streams, between the server and the client, using the server's netService:didAcceptConnectionWithInputStream:outputStream method.

The thing is, I want the server to have a list of all connected clients. I know that the client's NSNetServiceBrowserDelegate have a method that is revoked whenever the connection with the bonjour service is ending, But I can't find an equal method, with NSNetServiceDelegate, that I can use on the server side, to run the appropriate code when a client disconnects.

Imran Ali Khan
  • 8,469
  • 16
  • 52
  • 77
AMI289
  • 1,098
  • 9
  • 10
  • It's all about the streams at that point. – uchuugaka Sep 05 '14 at 09:41
  • Isn't there's a "live" way to know that? I mean, if I count on streams, the only time I'll know that the client has disconnected is when I try to use his stream. – AMI289 Sep 05 '14 at 14:35

1 Answers1

0

Check the Stream Programming Guide. Basically, you'll be using NSStream (or a subclass of it) and you'll need to create and assign a delegate to your streams. The stream delegate should handle one method – stream:handleEvent: That method is a callback that will give you one of the following indications of NSStreamStatus (a typedefed NSUInteger)

typedef enum {
   NSStreamStatusNotOpen = 0,
   NSStreamStatusOpening = 1,
   NSStreamStatusOpen = 2,
   NSStreamStatusReading = 3,
   NSStreamStatusWriting = 4,
   NSStreamStatusAtEnd = 5,
   NSStreamStatusClosed = 6,
   NSStreamStatusError = 7
};

In your implementation of the delegate method you basically create a switch-case statement that does whatever it is you need to do in response to the change.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
  • Thank you for your reply mate. I have tried that, on netService:didAcceptConnectionWithInputStream:outputStream: I set inputStream.delegate and outputStream.delegate to self, but non of the stream:handleEvent: methods gets revoked. maybe it has something to do with autoreleasing? – AMI289 Sep 06 '14 at 08:57
  • If I recall correctly, there might be a thing where the NSStream API needs to be on the same thread or runloop. I forget. CFStream is much harder but more flexible and thread safe. But you can break it down by finding some working sample code. It does work your just missing something somewhere. – uchuugaka Sep 06 '14 at 09:14
  • I've managed to get the delegate work if I create a new class which holds the streams data. But besides NSStreamStatusOpen and NSStreamStatusClosed no methods are being called. And on Xcode's documentations the typedef of NSStream is different than the one you've posted. – AMI289 Sep 14 '14 at 14:34
  • That's from the docs. It's possible the headers have updated and the docs have not. Another common but definitely more challenging approach is to use CFStream or other popular open source libraries like AFNetworking or AsyncSocket. – uchuugaka Sep 14 '14 at 15:06