1

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;
}
Shai Mishali
  • 9,224
  • 4
  • 56
  • 83

1 Answers1

2

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
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • I'm not sure I understand the answer... Should I just increase the tag? for how long? About error handling, I have it already i just didn't post it here... – Shai Mishali Mar 31 '14 at 09:04
  • The tag doesn't matter. I just incremented it so that you would have an idea of how many response had been received. The key is to call `receiveWithTimeout:` again after you have received a packet. Once your `didNotRecieveDataWithTag` has been called you have got all of the responses because the timeout was reached without a packet being received. – Paulw11 Mar 31 '14 at 09:09
  • It doesn't help ... It just brings the same "wrong" response twice. (after adding receiveWithTimeout:tag: inside the didReceiveData: delegate) ... – Shai Mishali Mar 31 '14 at 20:00
  • I found this https://github.com/knorby/boxeeremotelib/blob/master/boxeeremotelib/discoverer.py and your code looks equivalent now that it has the multiple receive. Are you testing this on the simulator or a device? – Paulw11 Mar 31 '14 at 21:27
  • Some other answers here on SO suggest putting the receiveWithTimeout before the sendData so that you are ready to receive the reply – Paulw11 Mar 31 '14 at 21:36