NSNetService发布,但didAcceptConnectionWithInputStream ...永远不会被调用

时间:2014-02-01 21:37:05

标签: ios objective-c sockets nsnetservice

我一直在尝试在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 post,{{3} }),它似乎连接(即我自己的实现通过NSStreamEventHasSpaceAvailable接收NSStreamEventOpenCompletedstream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode事件。但是,上述方法都不会在服务器上调用。

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

1 个答案:

答案 0 :(得分:4)

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

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

[self.ns publishWithOptions:NSNetServiceListenForConnections];

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

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