2013-05-03 61 views
1

我從這裏修改「健談的」應用程序:http://mobileorchard.com/tutorial-networking-and-bonjour-on-iphone/iPhone iOS如何發佈UDP服務?

我試圖創建並迅速從多個客戶端接收信息的服務器,但並沒有真正發任何東西。同時使用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; 
} 

回答

0

看來,應用程序工作正常後,更改「_chatty._tcp」。到「_chatty._udp」。