我不想使用GameKit。我正在尝试使用dns_sd.h的低级方法和DNSServiceRegister之类的方法来建立和维护连接。但是,我找到了一个名为HHServices的包装器......老实说,在这一点上,使用dns_sd的低级答案或使用HHServices的高级答案都是受欢迎的。
这是HHServices。这是GCDAsyncSocket。
这是我到目前为止的代码。我能够成功发布HHService和GCDAsyncSocket,并且还开始浏览其他设备上的HHServices,但没有建立连接。有帮助吗? :d
//....
NSUInteger serverPort = 19997;
HHServicePublisher *publisher = [[HHServicePublisher alloc] initWithName:@"MyDisplayName" type:@"_myexampleservice._tcp." domain:@"local." txtData:nil port:serverPort];
GCDAsyncSocket *listenSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
if (![listenSocket acceptOnPort:serverPort error:&error])
{
[self alert:@"Error starting GCDAsyncsocket"];
}
publisher.delegate = self;
if(![publisher beginPublish])
{
[self alert:@"Error publishing HHService"];
}
else
{
[self alert:@"Successfully published HHService"];
}
- (void)socket:(GCDAsyncSocket *)sender didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
[self alert:@"didAcceptNewSocket!"];
}
HHServiceBrowser *browser = [[HHServiceBrowser alloc] initWithType:@"_myexampleservice._tcp." domain:@"local."];
browser.delegate = self;
if(![browser beginBrowse])
{
[self alert:@"Couldn't start browsing"];
}
else
{
[self alert:@"Successfully started browsing"];
}
- (void) serviceBrowser:(HHServiceBrowser*)serviceBrowser didFindService:(HHService*)service moreComing:(BOOL)moreComing {
[self alert:@"Found a service! "];
}
有人能否解释我做错了什么?
答案 0 :(得分:0)
HHServices仅处理发现。由你来建立连接。您需要解析该服务,然后打开一个套接字,就像它在HHServices示例中所示。
答案 1 :(得分:0)
原来我没有保留我的browser
对象。