简体   繁体   中英

Connect to and send data via NSNetService

I'm trying to set upp a communication between a iPhone app and a Mac app using NSNetService.

I've come so far as to setup the Mac app to publish a NSNetService that the iPhone app can discover but I can't figure out how send data between them.

In my Mac app I publish my NSNetService with:

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!");
}

Everything looks fine and the delegate method - (void)netServiceDidPublish:(NSNetService *)sender (in the Mac app) is triggered.

In the iPhone app I make an instance of NSNetServiceBrowser and after a second or two it triggers the didFindService delegate method.

- (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];
    }
}

And directly after this it triggers the next delegate method - (void)netServiceDidResolveAddress:(NSNetService *)sender .

Here I don't know if it's connected of not, can't find any connect method och connect state on sender .

So I tried writing to the output-stream with:

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

And also updating the TXTRecordData with [sender setTXTRecordData:data]; . I don't get any errors and nothing happens in my Mac App. The Mac app has the delegate method - (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data .

I don't know how to proceed. I figure I'm missing something, but I don't know if it's in the Mac app or iPhone app. I would guess that I first need to connect somehow from the iPhone app to the Mac app, and in the Mac app add something that listens to a connection.

Don't believe everything apple claims,

There are a lot of problems with setting up streams with the NSNetworkService .

It will work if you do the following:

First obtain a port to publish the network on. DO NOT PICK A PORT YOURSELF.

Then you can use that port for publishing the network.

Get the client streams with:

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

The error a lot of programmers make is publishing a network by picking a port themselves and then opening the streams with

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

The streams will not open....

conclusion: publish by first obtaining a port and then use something like:

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];

then browse for services and add them

then open streams on the desired services from the one you browsed with

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

也许您需要首先打开输出流?

[ostream open];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM