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.
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
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.
Here's the code I'm using. sock is the socket I set up whose port number got passed to the NSNetService.
Code:
- (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
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.