Bonjour, NSStream and Multicast

Discussion in 'Mac Programming' started by gormster, Jan 21, 2010.

  1. gormster macrumors newbie

    Jan 21, 2010
    I need to send the same data to a whole heap of iPhones at once (potentially thousands) from a single Mac. These iPhones are all on the same wireless network. The obvious way to set up the connection is with Bonjour, which was fairly simple - but now I'm trying to send data from the Mac using NS/CFStreams and it just doesn't want to work.

    Here's the code I'm using. sock is the socket I set up whose port number got passed to the NSNetService.

    - (void)netServiceDidPublish:(NSNetService *)sender
    	NSAssert(sender==service,@"Abnormal service detected, dying");
    	NSLog(@"Service was published.");
    	CFSocketNativeHandle fd = CFSocketGetNative(sock);
    	CFReadStreamRef readstream;
    	CFWriteStreamRef writestream;
    	CFStreamCreatePairWithSocket(NULL, fd, &readstream, &writestream);
    	iStream = (NSInputStream*)readstream;
    	oStream = (NSOutputStream*)writestream;
    	[iStream setDelegate:self];
    	[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    	[iStream open];
    	[oStream setDelegate:self];
    	[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    	[oStream open];
    Right now, I get the message that the streams have opened and never get the all important NSStreamEventHasSpaceAvailable event. If I try to write to the stream, I get an application crash. A quick test in the write method revealed that the stream has no space available, so it's not just a case of not getting the event, it really can't be written to.

    If I try to [sender getInputStream:eek:utputStream:] I get CFNetworkError -72003, which is apparently kCFNetServiceErrorInProgress. I'm guessing this is because this method is designed to be used by the client (and in the iPhone client program I am using this method.)

    So my question is: how do I get the streams associated with a published NSNetService? Or even the socket associated with it? Is it the same socket as the one I set up? After all, you never pass the actual socket descriptor to NSNetService, just the port number.
  2. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Have you tried just sending some data down the raw socket without the Objective-C stuff? It'll make debugging easier.

    Also what properties did you assign to the socket? Is it non-blocking? I assume you have used a UDP socket as you can't multicast over TCP.

Share This Page