我正在从这里修改“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;
}