iphoneiosobjective-cudpnsnetservice

iPhone iOS how to publish a UDP service?


I'm modifying the "Chatty" app from here: http://mobileorchard.com/tutorial-networking-and-bonjour-on-iphone/

I'm trying to create a server which quickly receives information from multiple clients, but does not really send anything back. There are both Wifi and Bluetooth used at the same time, so I would like to maximize the network throughput. I think that UDP protocol might be faster, because there's no handshaking/packet sequence verification.

Is changing the "_chatty._tcp." to "_chatty._udp." enough in this example? I checked the documentation for NSNetService, but don't see anything about UDP there.

- (BOOL) publishService {
  // come up with a name for our chat room
  NSString* chatRoomName = [NSString stringWithFormat:@"%@'chat room", [[AppConfig getInstance] name]];

  // create new instance of netService
    self.netService = [[NSNetService alloc]
      initWithDomain:@"" type:@"_chatty._tcp."
      name:chatRoomName port:self.port];
    if (self.netService == nil)
        return NO;

  // Add service to current run loop
    [self.netService scheduleInRunLoop:[NSRunLoop currentRunLoop]
      forMode:NSRunLoopCommonModes];

  // NetService will let us know about what's happening via delegate methods
    [self.netService setDelegate:self];

  // Publish the service
    [self.netService publish];

  return YES;
}

// Start browsing for servers
- (BOOL)start {
  // Restarting?
  if ( netServiceBrowser != nil ) {
    [self stop];
  }

    netServiceBrowser = [[NSNetServiceBrowser alloc] init];
    if( !netServiceBrowser ) {
        return NO;
    }

    netServiceBrowser.delegate = self;
    [netServiceBrowser searchForServicesOfType:@"_chatty._tcp." inDomain:@""];

  return YES;
}

Solution

  • It appears that the app is working fine after changing "_chatty._tcp." to "_chatty._udp."