首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带XML的Boxee UDP广播发现API (远程)

带XML的Boxee UDP广播发现API (远程)
EN

Stack Overflow用户
提问于 2014-03-31 04:50:33
回答 1查看 129关注 0票数 1

我正在尝试使用Boxee Remote Control Interface发送UDP广播来发现设备。

当前使用AsyncUdpSocket,但在发送请求时,我只是将请求作为响应返回,而不是获得预期的响应。

这是我的代码,我是否遗漏了什么?:

代码语言:javascript
复制
- (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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-03-31 09:44:17

我认为您的问题在于您的代码只准备接收单个数据包。您正在发送一个广播数据包,这样本地网络上的所有设备都会收到该数据包,包括您自己的设备。此外,尽管我知道这只是测试代码,但网络上可能有多个Boxee盒子,因此您可以预期可能会有多个回复。

试试这样的东西-

代码语言:javascript
复制
 (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
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22749691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档