iPhone iOS如何发布UDP服务?

时间:2013-05-03 19:57:37

标签: iphone ios objective-c udp nsnetservice

我正在修改“Chatty”应用程序:http://mobileorchard.com/tutorial-networking-and-bonjour-on-iphone/

我正在尝试创建快速接收来自多个客户端的信息的服务器,但实际上并没有发回任何内容。既有Wifi和蓝牙同时使用,所以我想最大化网络吞吐量。我认为UDP协议可能更快,因为没有握手/数据包序列验证。

正在更改“_chatty._tcp”。到“_chatty._udp。”在这个例子中足够了吗?我检查了NSNetService的文档,但没有看到有关UDP的任何内容。

- (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;
}

1 个答案:

答案 0 :(得分:0)

在更改“_chatty._tcp”后,应用程序似乎正常运行。到“_chatty._udp。”