Boxee UDP Broadcast Discover API with XML (Remote)

152 Views Asked by At

I'm trying to use the Boxee Remote Control Interface to send a UDP Broadcast to discover devices.

Currently using AsyncUdpSocket but when sending the request, i just get the request back as the response, instead of getting the expected response.

Here's my code, Am I missing anything? :

- (void)viewDidLoad
{
    [super viewDidLoad];

    AsyncUdpSocket *socket  = [[AsyncUdpSocket alloc] initWithDelegate:self];
    [socket enableBroadcast:YES error:nil];
    [socket bindToPort:2562 error:nil];

    NSString *xml           = @"<?xml version=\"1.0\"?><BDP1 cmd=\"discover\" application=\"iphone_remote\" challenge=\"shittycitttyy123\" signature=\"cdddac43fdbce83d24b7c1ca5149c697\"/>";


    NSData *data            = [xml dataUsingEncoding:NSUTF8StringEncoding];

    if([socket sendData:data toHost:@"10.0.0.255" port:2562 withTimeout:3 tag:0]){
        [socket receiveWithTimeout:2 tag:0];
    }
}

-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{
    NSLog(@"Got data %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

    return YES;
}
1

There are 1 best solutions below

5
On

I think your problem is that your code is only prepared to receive a single packet. You are sending a broadcast packet, so that will be received by all devices on the local network - including your own, which is what you are seeing. Additionally, although I understand that this is just test code, there may be multiple Boxee boxes on the network, so you could expect the possibility of multiple replies.

try something like this -

 (void)viewDidLoad
{
    [super viewDidLoad];

    AsyncUdpSocket *socket  = [[AsyncUdpSocket alloc] initWithDelegate:self];
    [socket enableBroadcast:YES error:nil];
    [socket bindToPort:2562 error:nil];

    NSString *xml           = @"<?xml version=\"1.0\"?><BDP1 cmd=\"discover\" application=\"iphone_remote\" challenge=\"shittycitttyy123\" signature=\"cdddac43fdbce83d24b7c1ca5149c697\"/>";


    NSData *data            = [xml dataUsingEncoding:NSUTF8StringEncoding];

    if([socket sendData:data toHost:@"10.0.0.255" port:2562 withTimeout:3 tag:0]){
        [socket receiveWithTimeout:2 tag:0];
    }
}

-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{
    NSLog(@"Got data %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

    //TODO - process incoming packet and determine if it is a Boxee response

    [socket receiveWithTimeout:2 tag:tag+1];  //Look for more data
    return YES;
}

- (void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error
{
   NSLog(@"Did not receive data");  
   //TODO check error and take appropriate action
}