【问题标题】:Listening to an udp broadcast with the boost library使用 boost 库收听 udp 广播
【发布时间】:2012-06-01 18:13:14
【问题描述】:

似乎是很多人都有的问题,但到目前为止我找到的所有答案都没有帮助。

问题: 我正在尝试收听通过 UDP 将其数据包发送到我的电脑的 Velodyne HDL32。 操作系统是 32 位 Ubuntu 和 Boost 库 v1.46。

我通过 Wireshark 获得的数据如下所示:

Time     | Source         | Destination   | Protocol | Length | Source Port | Destination Port
0.000000 | 192.168.17.212 | 192.168.3.255 | UDP      | 1248   | https       | opentable

但是使用此代码,没有数据显示给我(端口是正确的):

receiver(boost::asio::io_service& io_service,
  const boost::asio::ip::address& listen_address)
: m_socket(io_service)
{

boost::asio::ip::address ipAddr = boost::asio::ip::address_v4::any();
boost::asio::ip::udp::endpoint listen_endpoint(
         ipAddr, 2368);

m_socket.open(listen_endpoint.protocol());
m_socket.bind(listen_endpoint);

m_socket.async_receive_from(
    boost::asio::buffer(m_data, max_length), m_sender_endpoint,
    boost::bind(&receiver::handle_receive_from, this,
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));
}

void handle_receive_from(const boost::system::error_code& error,
  size_t bytes_recvd)
{
std::cout << "receive" << bytes_recvd << std::endl;


  m_socket.async_receive_from(
      boost::asio::buffer(m_data, max_length), m_sender_endpoint,
      boost::bind(&receiver::handle_receive_from, this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));
}

到目前为止,任何人都可以发现问题吗?或者您需要更多信息吗? 我很感激能得到任何帮助。

注意: 我没有以 root 权限运行程序!

一些想法: 当子网掩码为 255.255.255.0 时, boost::asio::ip::address_v4::any() 会不会监听 IP ..*.255?

使用 netcat 时,也不会显示任何数据。当我使用 Windows netcat 时,它工作得很好。与 Linux 和 Windows 上的 Wireshark 相同 - 工作正常。 也试过了,但效果相同 - 没有数据。

【问题讨论】:

  • 应用程序是否正在向您发送多播数据?如果是这样,请尝试不绑定到侦听端点。
  • 你调用 io_service::run() 了吗?你只展示了你的代码片段,所以我不得不问。
  • @Sean io_service.run() 在主函数中被调用。在特定 IP 和特定端口上接收数据时,该程序运行良好。但是当我尝试收听广播时没有收到数据
  • @chad 也尝试了您的选择。似乎没有任何作用。我将我的一些想法添加到主要问题中
  • 如果您同时运行 Wireshark,您的应用程序是否正常工作? Wireshark 可能会将 NIC 本身置于 intp 混杂模式。

标签: c++ sockets boost udp boost-asio


【解决方案1】:

感谢大家努力帮助我。代码没问题,但问题出在 velodyne 和网络设置方面。

对尝试使用 Velodyne 的所有其他人的说明:

velodyne 有自己的子网 (192.168.17.x)。现在所有记录的数据都通过广播发送到子网 192.168.3.x。在正常情况下,应该在该子网中的所有 IP 上接收数据,但这似乎是不可能的。您可以接收数据的唯一 IP 是 IP 255,只要您使用这两种解决方案之一即可。 (或使用 windows 或使用 wireshark 转储文件)

1.愚蠢但有效的解决方案

将网关设置为 192.168.3.1。是的,没有,但没关系。从现在开始,您将收到 IP 255 上的数据。

2。干净的解决方案

设置一条新路由,将所有流量从 velodyne 的子网引向子网 192.168.3.x。

我真的不知道它为什么这么复杂,但是我们花了很长时间才发现这个“秘密”。希望你们中的一些人能从我们挑剔的工作中获益。

【讨论】:

    【解决方案2】:

    您是否尝试过设置广播选项?

    // do this before binding
    boost::asio::socket_base::broadcast option(true);
    m_socket.set_option(option);
    

    【讨论】:

    • thx BatchyX 的答案,但是当我查看这个选项时,它说:“允许发送广播消息的套接字选项。”但我需要接收数据而不发送任何数据。
    • 对我来说,源代码说它启用了 SO_BROADCAST,这是在 linux 上接收广播数据所必需的。
    猜你喜欢
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 2018-05-29
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多