How can one computer Send and Receive on Bonjour, but not receive it's own messages?

Discussion in 'Mac Programming' started by ArtOfWarfare, May 7, 2012.

  1. ArtOfWarfare, May 7, 2012
    Last edited: May 16, 2012

    ArtOfWarfare macrumors G3


    Nov 26, 2007
    Edit: Sorry about the double post, but it's been over a week since I last posted and I'm still on the same topic. My issue is in my next post.

    Hi, I'd like to do something that I think will be pretty simple...

    I'd like my Cocoa app to be able to bundle up some data in an NSDictionary and broadcast it on the local network. Any other instances of the app on the local network (say, a school or office or home network,) should be able to receive the data and access the keys/objects for the dictionary. Is Bonjour the best means of doing this?

    Is this class reference the correct place to be looking, or am I heading in the wrong direction?


    (Oh, I'd also like it if I left myself with the option of creating a PC version sometime in the future that could communicate with the Mac version... if that makes it much more difficult, I won't bother as I probably won't ever make a PC version, but just incase, I don't want to close the door completely on that possibility...)
  2. ArtOfWarfare thread starter macrumors G3


    Nov 26, 2007
    Alright, so I started with Apple's PictureSharing demonstration code.

    I modified it a lot and moved the broadcasting and receiving classes both into Battery Status.

    Here's my issue:

    If my app is set to both broadcast and receive data, it'll receive the data that it broadcasts. How can I, when receiving data, filter out what it sent out?

    I attempted to filter based on domain, but I found that all of my computers see each other and themselves as "local.", so that didn't work.

    I'm not sure if this is relevant or not, but I think I should note that my iMac is plugged into ethernet and set up with internet sharing turned on. The other computers are connecting to the internet through the iMac's shared connection.

    Anyways, here's what a bit of my code looks like:
    (In my broadcasting class, at the point when it's registering itself with Bonjour.)
            self.netService = [[[NSNetService alloc] initWithDomain:@"" type:@"_tsmbatterystatus._tcp." name:self.serviceName port:chosenPort] autorelease];
            if (self.netService != nil) {
                [self.netService setDelegate:self];
                [self.netService publishWithOptions:0];
    (In my receiving class)
    - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
        // An NSNetService delegate callback that's called when we discover a service. 
        // We add this service to our set of pending services to add and, if there are 
        // no more services coming, we add that set to our services set, triggering the 
        // necessary KVO notification.
        assert(aNetServiceBrowser == self.browser);
        #pragma unused(aNetServiceBrowser)
        NSLog(@"Found a service, its domain is: %@, its address is: %@, and its host name is: %@", [aNetService domain], [[aNetService addresses] description], [aNetService hostName]); 
        if (![[aNetService domain] isEqualToString:@"local."])
            [self startReceiveFromService:aNetService];
            [self.pendingServicesToAdd addObject:aNetService];
        if ( ! moreComing ) {
            NSSet * setToAdd;
            setToAdd = [[self.pendingServicesToAdd copy] autorelease];
            assert(setToAdd != nil);
            [self.pendingServicesToAdd removeAllObjects];
            [self willChangeValueForKey:@"services" withSetMutation:NSKeyValueUnionSetMutation usingObjects:setToAdd];
            [ unionSet:setToAdd];
            [self  didChangeValueForKey:@"services" withSetMutation:NSKeyValueUnionSetMutation usingObjects:setToAdd];

Share This Page