Connect and send data through NSNetService

I am trying to set up a connection between an iPhone application and a Mac application using NSNetService.

I went as far as setting up a Mac application to publish NSNetService, which can open an iPhone application, but I cannot figure out how to send data between them.

In my Mac application, 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 starts - (void)netServiceDidPublish:(NSNetService *)sender (on a Mac application).

In an iPhone application, I make an instance of NSNetServiceBrowser , and after a second or two it calls the didFindService delegate didFindService .

 - (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 right after that, it starts the following delegate method - (void)netServiceDidResolveAddress:(NSNetService *)sender .

Here I do not know if this is due to not, cannot find any och connect method on sender .

So, I tried to write to the output stream using

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

As well as updating TXTRecordData using [sender setTXTRecordData:data]; . I am not getting any errors and nothing is happening in my Mac application. The Mac application has a delegate method - (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data .

I do not know how to act. I suppose I am missing something, but I don’t know if it was in a Mac or iPhone application. I would suggest that I first need to somehow connect from the iPhone application to the Mac, and in the Mac application add something that listens for the connection.

+4
source share
2 answers

Do not believe everything that the apple claims

There are many problems setting up flows using NSNetworkService .

It will work if you do the following:

First get the port for publishing the network. DO NOT SHOW PORT YOURSELF.

Then you can use this port to publish the network.

Get client threads using:

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

The mistake that many programmers create is to publish the network by selecting the port itself and then opening the threads with

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

Streams do not open ....

conclusion: publish, first getting the 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 services and add them

then open the streams of the required services from the one you were viewing with

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

Perhaps you need to open the output stream first?

 [ostream open]; 
+2
source

All Articles