1

I do not want to use GameKit. I am trying to use a low-level approach using dns_sd.h and methods like DNSServiceRegister to establish and maintain a connection. However, I found a wrapper for this called HHServices... Honestly at this point either a low-level answer using dns_sd or a high-level answer using HHServices is welcome.

Here is HHServices. Here is GCDAsyncSocket.

This is the code I have so far. I am able to successfully publish the HHService and the GCDAsyncSocket, and also start browing for HHServices on the other device, but no connection is made. Any help? :D

Server Side

Here's where I publish the HHService:

//....
NSUInteger serverPort = 19997;
HHServicePublisher *publisher = [[HHServicePublisher alloc] initWithName:@"MyDisplayName" type:@"_myexampleservice._tcp." domain:@"local." txtData:nil port:serverPort];

GCDAsyncSocket *listenSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

NSError *error = nil;
if (![listenSocket acceptOnPort:serverPort error:&error])
{
    [self alert:@"Error starting GCDAsyncsocket"];
}


publisher.delegate = self;
if(![publisher beginPublish])
{
    [self alert:@"Error publishing HHService"];
}
else
{
    [self alert:@"Successfully published HHService"];
}

This is where I see if a client connected:

- (void)socket:(GCDAsyncSocket *)sender didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
    [self alert:@"didAcceptNewSocket!"];
}

Client-side

Where I start browsing

    HHServiceBrowser *browser = [[HHServiceBrowser alloc] initWithType:@"_myexampleservice._tcp." domain:@"local."];
    browser.delegate = self;
    if(![browser beginBrowse])
    {
        [self alert:@"Couldn't start browsing"];
    }
    else
    {
        [self alert:@"Successfully started browsing"];
    }

Callback for finding the published HHService:

- (void) serviceBrowser:(HHServiceBrowser*)serviceBrowser didFindService:(HHService*)service moreComing:(BOOL)moreComing {
    [self alert:@"Found a service! "];
}

Could someone shed some light as to what I'm doing wrong?

Community
  • 1
  • 1
rweichler
  • 103
  • 2
  • 7

2 Answers2

0

HHServices only handles discovery. It's up to you to make the connection. You need to resolve the service, then open a socket, like it shows in the HHServices example.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • The thing is, the code [self alert:@"Found a service! "]; doesn't even get called. – rweichler Jan 23 '13 at 11:24
  • So I basically don't even get the opportunity to resolve the service since it was never discovered. – rweichler Jan 23 '13 at 11:30
  • You don't post enough context to know for sure, but it looks like your `browser` object might be getting deallocated. I assume that you are using ARC, your `browser` object is a local variable in a method that return shortly after the code you posted, and that you don't assign that object to any strongly held variable such as an instance variable? – Jim Jan 23 '13 at 21:28
  • Yup... that was it. Forgot to retain. I feel stupid now. Thank you kindly! – rweichler Jan 24 '13 at 01:57
0

Turns out I did not retain my browser object.

rweichler
  • 103
  • 2
  • 7