gpt4 book ai didi

c++ - 为什么这个程序没有收到预期的 UDP 数据包?

转载 作者:可可西里 更新时间:2023-11-01 16:42:14 31 4
gpt4 key购买 nike

我正在尝试使用 Boost asio 接收 UDP 数据包。我的代码基于 this blocking UDP client example from the asio documentation .

我正在尝试从 C6655 TI DSP 接收类似 BOOTP 的 UDP 数据包,该数据包以 3 秒的间隔传输。我让 Wireshark 监视我的程序正在监听的同一接口(interface),它可以看到数据包到达(请参阅下面的确切数据包数据,从 Wireshark 导出)。数据包不是真的来自 DSP,我用 tcpdump 捕获了一个数据包,我用 packeth 从 Raspberry Pi 模拟它.

但是,我的程序没有收到数据包。它有 4 秒超时(因为 DSP 每 3 秒广播一次)。如果超时,它会打印一条消息,否则它应该打印接收到的字节数。该程序的完整(可编译)源代码如下(大约 100 行)。

使用参数 192.168.5.122 67 4000 调用该命令,这意味着在 192.168.5.122:67 上监听 4000 毫秒超时。

编辑:除了下面的代码,我还尝试将其作为我的端点:udp::endpoint listen_endpoint(boost::asio::ip::address_v4::any(), atoi(argv[2 ])); 以及某处搜索结果建议的 IP 地址 0.0.0.0

我还添加了以下内容无济于事:

boost::asio::socket_base::broadcast option(true);
socket_.set_option(option);

我确实有一个能够正确接收这个数据包的程序,它是使用 Berkeley 套接字编写的。除了绑定(bind)到 INADDR_ANY 之外,我看不出它有什么特别之处。

完整程序如下:

//
// blocking_udp_client.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~
//
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/udp.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <iostream>

using boost::asio::deadline_timer;
using boost::asio::ip::udp;

class listener
{
public:
listener(const udp::endpoint& listen_endpoint)
: socket_(io_service_, listen_endpoint)
, deadline_(io_service_)
{
deadline_.expires_at(boost::posix_time::pos_infin);
check_deadline();
}

std::size_t receive(const boost::asio::mutable_buffer& buffer, boost::posix_time::time_duration timeout, boost::system::error_code& ec)
{
deadline_.expires_from_now(timeout);
ec = boost::asio::error::would_block;
std::size_t length = 0;
socket_.async_receive(boost::asio::buffer(buffer), boost::bind(&listener::handle_receive, _1, _2, &ec, &length));

// TODO: The following do/while is hinky. Does run_one() need to happen before the comparison?
do io_service_.run_one();
while (ec == boost::asio::error::would_block);

return length;
}

private:
void check_deadline()
{
if (deadline_.expires_at() <= deadline_timer::traits_type::now())
{
// cancel() won't work on XP. Something about using close() instead... Look it up. I'm doing this on Win10.
socket_.cancel();
deadline_.expires_at(boost::posix_time::pos_infin);
}
deadline_.async_wait(boost::bind(&listener::check_deadline, this));
}

static void handle_receive(const boost::system::error_code& ec, std::size_t length, boost::system::error_code* out_ec, std::size_t* out_length)
{
*out_ec = ec;
*out_length = length;
}

private:
boost::asio::io_service io_service_;
udp::socket socket_;
deadline_timer deadline_;
};

int main(int argc, char* argv[])
{
try
{
if (argc != 4)
{
std::cerr << "Usage: blocking_udp_timeout <listen_addr> <listen_port> <timeout_ms>\n";
return 1;
}

udp::endpoint listen_endpoint(boost::asio::ip::address::from_string("0.0.0.0"), atoi(argv[2]));
std::cout << "Endpoint: " << listen_endpoint << std::endl;

auto timeout = atoi(argv[3]);
std::cout << "Timeout : " << timeout << std::endl;

listener c(listen_endpoint);

for (;;)
{
char data[1024];
boost::system::error_code ec;
auto n = c.receive(boost::asio::buffer(data), boost::posix_time::milliseconds{timeout}, ec);

if (ec)
{
std::cout << "Receive error: " << ec.message() << "\n";
}
else
{
std::cout << "Received " << n << " bytes." << std::endl;
}
}
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}

return 0;
}

这是我要接收的数据包。这包括以太网帧:

0000   ff ff ff ff ff ff c4 ed ba aa 28 35 08 00 45 00  ..........(5..E.
0010 01 48 00 01 00 00 10 11 a9 a5 00 00 00 00 00 00 .H..............
0020 00 00 00 44 00 43 01 34 00 00 01 01 06 00 12 34 ...D.C.4.......4
0030 56 78 00 01 00 00 00 00 00 00 00 00 00 00 00 00 Vx..............
0040 00 00 00 00 00 00 c4 ed ba aa 28 35 00 00 00 00 ..........(5....
0050 00 00 00 00 00 00 74 69 2d 62 6f 6f 74 2d 74 61 ......ti-boot-ta
0060 62 6c 65 2d 73 76 72 00 00 00 00 00 00 00 00 00 ble-svr.........
0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0090 00 00 00 00 00 00 74 69 2d 62 6f 6f 74 2d 74 61 ......ti-boot-ta
00a0 62 6c 65 2d 30 30 30 37 00 00 00 00 00 00 00 00 ble-0007........
00b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0150 00 00 00 00 00 00 ......

我确实有一个可以接收这个数据包的 Berkeley 套接字实现(我已经删除了错误处理和其他杂项代码):

{
struct sockaddr_in servaddr;
socklen_t len;
char mesg[RECV_BUFFER_LENGTH];

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(67);
bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
n = recvfrom(sockfd, mesg, RECV_BUFFER_LENGTH, 0, NULL, &len);
}

最佳答案

考虑在 socket_.cancel() 之后但在下一次调用 socket_.async_receive() 之前会发生什么。如果有任何数据到达,则没有分配给套接字的“接收处理程序”。当计时器到期时,它会导致 run_once() 被调用 3 次(因为每个 async_wait()expires_at() 和一些其他导致取消已分配的处理程序,并导致已分配的处理程序被发布到运行队列,错误代码为 operation_aborted)。

您没有提到您的超时设置是什么。您的代码所基于的示例(来自 Boost 文档的示例)将超时设置为 10 秒,但您将其设置为可配置。如果超时太紧,这将导致自旋(post->cancel previous->call previous handler->post->etc.)并可能在接收数据包时调用 socket_.cancel() .如果是这样的话,您就不必走得太远,用广播来测试它。您也可以通过点对点连接看到它。

编辑:在使用长超时(4000 毫秒)和您的确切代码时,我能够接收到已发送的广播。我必须安装“传统的”netcat,因为 BSD netcat 坏了。但是下面的行有效:

echo "hello world" | nc.traditional -b -u 192.168.XXX.255 1500

XXX.255 并非字面上的“XXX.255”。这是我的本地广播地址。 nc.bsd 中的 -b 选项已损坏(如果您使用上述选项对 nc.bsd 进行 strace,您就会明白为什么)。

unix stackexchange nc.traditional有一个很好的例子说明其他人如何弄清楚为什么 nc.bsd 不能进行 UDP 广播(-b 选项什么都不做)。

PackEth 无法发送广播或 p-t-p 流量,所以我不会用它来衡量您是否可以发送广播流量。诚然,我对它没有太多经验,所以我不知道它是坏了还是我没有正确配置它。

关于c++ - 为什么这个程序没有收到预期的 UDP 数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32917830/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com