gpt4 book ai didi

ios - NSNetService 发布,但从未调用过 didAcceptConnectionWithInputStream...

转载 作者:行者123 更新时间:2023-11-28 18:34:39 24 4
gpt4 key购买 nike

我一直在尝试在 iOS 上制作一个简单的应用程序,它将接收消息,然后根据该消息执行操作(目前我只想通过 NSLog 显示它) .我可以连接到该服务,但该服务似乎从未收到任何东西。这是我的 .h 文件:

#import <Foundation/Foundation.h>
#import <arpa/inet.h>

@interface PalServiceController : NSObject <NSNetServiceDelegate>

@property (nonatomic, strong) NSNetService *ns;
@property (nonatomic, strong) NSOutputStream *ostream;

- (void)start;
+ (int)getPort;
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode;

@end

还有我的.m:

#import "PalServiceController.h"

@implementation PalServiceController

- (void)start
{
// Start a net service
int port = [PalServiceController getPort];
NSLog(@"Opening on port %i", port);
self.ns = [[NSNetService alloc] initWithDomain:@""
type:@"_TestingProtocol._tcp."
name:@"Test Name For iPhone"
port:port];
if (self.ns) {
[self.ns setDelegate:self];
[self.ns publish];
self.ns.delegate = self;
} else {
NSLog(@"Error starting service");
}

}

/*
* Code from: http://stackoverflow.com/a/11723158/657676
*/
+ (int) getPort
{
...
}

- (void)netServiceWillResolve:(NSNetService *)sender
{
NSLog(@"Resolving");
}

- (void)netServiceDidResolveAddress:(NSNetService *)sender
{
NSLog(@"Resolved Address");
}

- (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary *)errorDict
{
NSLog(@"Error publishing");
}

- (void)netService:(NSNetService *)sender didAcceptConnectionWithInputStream:(NSInputStream *)inputStream outputStream:(NSOutputStream *)outputStream
{
NSLog(@"Got a connection! (server)");
// Close self down
[self.ns stopMonitoring];
[self.ns stop];
}

- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
{
NSLog(@"Error resolving");
}
- (void)netServiceDidPublish:(NSNetService *)sender
{
NSLog(@"Published server");
}

- (void)netServiceDidStop:(NSNetService *)sender
{
NSLog(@"Server stopped");
}

- (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data
{
NSLog(@"Updated TXT Record");
}

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
{
NSLog(@"Event on server");
switch(eventCode) {
...
}
}

@end

这是通过以下方式调用的:

self.controller = [[PalServiceController alloc] init];
[self.controller start];

当我使用 Bonjour Browser 时,我可以看到该服务,当我使用我自己的 NSNetServiceBrowser 实现或 Bill Dudney 的示例(blog postcode)时,它似乎连接(即我自己的实现通过 stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode 接收 NSStreamEventHasSpaceAvailableNSStreamEventOpenCompleted 事件。但是,上述方法均未在服务器上调用。

我还是 iOS 的新手,所以我希望这只是一些可以轻松修复的愚蠢错误。

最佳答案

-[NSNetService publish] 只是发布服务;您负责创建一个套接字并监听您提供给 NSNetService 初始值设定项的端口。您不会收到 netService:didAcceptConnectionWithInputStream:outputStream:,因为 NSNetService 对套接字一无所知。

如果您希望 NSNetService 为您管理套接字——并发送 didAcceptConnection...——使用 -[NSNetService publishWithOptions:]:

[self.ns publishWithOptions:NSNetServiceListenForConnections];

在这种情况下,您需要确保用于初始化 NSNetService 的端口未被使用。您的 getPort 方法永远不会关闭套接字(并泄漏 CFSocketRef),因此它会在您发布服务时被使用,从而导致错误。

我建议您删除 getPort,并且正如 Apple 在 NSNetServices.h 中建议的那样,“[s]指定端口号为零以使用随机端口。”

关于ios - NSNetService 发布,但从未调用过 didAcceptConnectionWithInputStream...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21503970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com