iphone - 通过 NSNetService 连接并发送数据

标签 iphone objective-c macos nsnetservice

我正在尝试使用 NSNetService 在 iPhone 应用程序和 Mac 应用程序之间建立通信。

到目前为止,我已将 Mac 应用程序设置为发布 iPhone 应用程序可以发现的 NSNetService,但我无法弄清楚如何在它们之间发送数据。

在我的 Mac 应用程序中,我发布了我的 NSNetService:

self.netService = [[NSNetService alloc] initWithDomain:@"" type:@"_sputnik._tcp" name:@"" port:port];
if (self.netService) {
    [self.netService scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:@"PrivateMyMacServiceMode"];

    [self.netService setDelegate:self];
    [self.netService publish];
    [self.netService startMonitoring];
    NSLog(@"SUCCESS!");
} else {
    NSLog(@"FAIL!");
}

一切看起来都很好,委托(delegate)方法 - (void)netServiceDidPublish:(NSNetService *)sender (在 Mac 应用程序中)被触发。

在 iPhone 应用程序中,我创建了一个 NSNetServiceBrowser 实例,一两秒后它触发了 didFindService 委托(delegate)方法。

- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
    TRACE;
    NSLog(@"moreComing: %d", moreComing);
    self.connectedService = aNetService;
    if (!moreComing) {
        self.connectedService.delegate = self;
        [self.connectedService resolveWithTimeout:30];
    }
}

紧接着它触发下一个委托(delegate)方法 - (void)netServiceDidResolveAddress:(NSNetService *)sender

这里我不知道它是否已连接,在 sender 上找不到任何连接方法 och 连接状态。

所以我尝试写入输出流:

[self.connectedService getInputStream:&istream outputStream:&ostream];
NSString *test = @"test";
NSData *data = [test dataUsingEncoding:NSUTF8StringEncoding];
[ostream write:[data bytes] maxLength:[data length]];

同时使用 [sender setTXTRecordData:data]; 更新 TXTRecordData。我没有收到任何错误,我的 Mac 应用程序也没有任何反应。 Mac 应用程序具有委托(delegate)方法 - (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data

我不知道怎么办。我想我遗漏了什么,但我不知道它是在 Mac 应用程序还是 iPhone 应用程序中。我想我首先需要以某种方式从 iPhone 应用程序连接到 Mac 应用程序,然后在 Mac 应用程序中添加一些监听连接的东西。

最佳答案

不要相信苹果声称的一切,

使用 NSNetworkService 设置流有很多问题。

如果您执行以下操作,它将起作用:

首先获取一个端口来发布网络。不要自己选择端口。

然后您可以使用该端口来发布网络。

获取客户端流:

[nService qNetworkAdditions_getInputStream:&istream outputStream:&ostream];

很多程序员犯的错误是通过自己选择端口然后打开流来发布网络

[nService qNetworkAdditions_getInputStream:&istream outputStream:&ostream];

流不会打开....

结论: 通过首先获取一个端口然后使用类似的东西来发布:

self.netService = [[NSNetService alloc] initWithDomain:@"local" type:@"_xxx._tcp." name:serviceName port:(int) self.port];
open streams with
CFStreamCreatePairWithSocket(kCFAllocatorDefault, nativeSocketHandle, &readStream, &writeStream);
open streams with (you open a connection here)
EchoConnection * connection = [[EchoConnection alloc] initWithInputStream:( NSInputStream *)readStream outputStream:( NSOutputStream *)writeStream];
        [self.connections addObject:connection];

然后浏览并添加服务

然后从您浏览过的服务中打开所需服务的流

[nService qNetworkAdditions_getInputStream:&istream outputStream:&ostream];
(and open them with [istream open] and [ostream open])

关于iphone - 通过 NSNetService 连接并发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12055916/

相关文章:

iphone - 我的方法似乎总是返回 FALSE

Mojave 上的 Python 段错误 11

macos - 将 Xcode 5 中的 AppDelegate.scpt 设置为仅运行

iphone - 在我使用的 iPhone 上制作游戏

iphone - 新的 iPhone 应用程序作为旧应用程序的更新,相同的 bundle 标识符,Xcode 给出错误

iPhone SDK : Different instances of webviews

ios - 如何获得 Xcode 项目文件的较短路径

iphone - 让 iPad 应用程序变得通用?

ios - xcode 在 ios 中显示 3.5 英寸和 4 英寸屏幕的高度相同

ios - 多次调用 textFieldShouldEndEditing