首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法接收使用GCDAsyncSocket发送的UDP数据包的响应

无法接收使用GCDAsyncSocket发送的UDP数据包的响应
EN

Stack Overflow用户
提问于 2015-04-18 14:02:13
回答 2查看 2.4K关注 0票数 20

我正在制作一个发送UDP数据包的应用程序,以便打开LED bulb。当我连接到由Wifi bridge创建的Ad-hoc时,我能够执行所有操作。

现在,我要配置Wifi bridge,使其可以连接到我的主路由器。我设置了AT命令来执行此过程,但不知何故,我无法从Wifi bridge接收到我发送给它的命令的响应。

有关程序如下:

  • Step 1 :向局域网广播IP地址"10.10.100.255“和端口48899 => "Link_Wi-Fi”发送UDP报文

LAN上的所有Wifi网桥都将使用其详细信息进行响应。响应是"10.10.100.254,ACCF232483E8"

  • Step 2 : (更改wifi网桥上的设置可选):然后向LimitlessLED Wifi网桥发送"+ok“。将UDP消息发送到从步骤1 "10.10.100.254“=> "+ok"

  • Step 3 : (更改wifi网桥上的设置时可选)返回的响应IP地址:之后,您可以向模块发送AT命令(以\r\n结尾)。

发送UDP数据包的代码如下

代码语言:javascript
复制
-(void)configureWifi{

    counter++;
    NSString *host = @"10.10.100.255";
    if ([host length] == 0)
    {
        [self logError:@"Address required"];
        return;
    }

    int port = 48899; //[portField.text intValue];
    if (port <= 0 || port > 65535)
    {
        [self logError:@"Valid port required"];
        return;
    }
    NSString *msg = @"Link_Wi-Fi";
    NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"the message sent is %@", data);
    [udpSocket sendData:data toHost:host port:port withTimeout:-1 tag:tag];

}

现在,为了设置套接字并接收数据,我使用了这两个委托方法:

代码语言:javascript
复制
 - (void)setupSocket
{
    // Setup our socket.
    // The socket will invoke our delegate methods using the usual delegate paradigm.
    // However, it will invoke the delegate methods on a specified GCD delegate dispatch queue.
    // 
    // Now we can configure the delegate dispatch queues however we want.
    // We could simply use the main dispatc queue, so the delegate methods are invoked on the main thread.
    // Or we could use a dedicated dispatch queue, which could be helpful if we were doing a lot of processing.
    // 
    // The best approach for your application will depend upon convenience, requirements and performance.
    // 
    // For this simple example, we're just going to use the main thread.

    udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    NSError *error = nil;

    if (![udpSocket bindToPort:0 error:&error])
    {
        [self logError:FORMAT(@"Error binding: %@", error)];
        return;
    }
    if (![udpSocket beginReceiving:&error])
    {
        [self logError:FORMAT(@"Error receiving: %@", error)];
        return;
    }

    [self logInfo:@"Ready"];
}

为了接收数据,这是在发送UDP数据包后调用的方法。这是GCDAsyncUdpSocket类的委托方法,我在项目中使用它来发送和接收UDP包。

代码语言:javascript
复制
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
                                               fromAddress:(NSData *)address
                                         withFilterContext:(id)filterContext
{
    NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (msg)
    {
        [self logMessage:FORMAT(@"RECV: %@", msg)];
    }
    else
    {
        NSString *host = nil;
        uint16_t port = 0;
        [GCDAsyncUdpSocket getHost:&host port:&port fromAddress:address];

        [self logInfo:FORMAT(@"RECV: Unknown message from: %@:%hu", host, port)];
    }
}

一旦我能够接收到响应,我将能够发送下一个AT命令,以便配置网桥。

谢谢。任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2015-04-24 13:43:36

以下是我建议您使用的故障排除步骤:

1-我假设您使用的是ARC,因此请确保您的udpSocket变量在整个异步通信过程中都有一个强引用。如果它正在被释放,那么这可以解释为什么没有回调。

2-确保交流真的按照你想象的方式进行。使用Wireshark之类的软件捕获网络上交换的数据包。这将允许您确认在调用sendData:时确实发送了数据包,并且还允许您确认是否收到回复。

3-确保正确使用GCDAsyncUdpSocket。考虑到您想广播一条消息,您不应该在setupSocket方法中调用bindToPort:error:。相反,您应该调用enableBroadcast:error:。考虑到您还希望在广播后接收数据包,您应该使用connectToHost:onPort:error:方法来更改套接字的状态,以允许双向通信。在此之后,您可以将sendData:toHost:port:withTimeout:tag:的用法替换为sendData:withTimeout:tag:。最后,您可以调用beginReceiving:,以便对任何传入的数据包调用该委托。

4-如果这仍然不能让你通过它,我建议你通读GCDAsyncUdpSocket的文档,它有很好的文档记录。

票数 2
EN

Stack Overflow用户

发布于 2015-05-07 05:34:18

您可以使用Wireshark或任何网络捕获工具排除故障。我们曾经在类似的项目中工作,我们广泛地使用了Wireshark。如果数据包已经到达设备(Z-Wave),它将发出某种Ack。这将有助于确保数据包正在传出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29713553

复制
相关文章

相似问题

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