简体   繁体   English

使用boost库收听udp广播

[英]Listening to an udp broadcast with the boost library

Seems to be a problem that many people have, but all the answers I have found so far didn't help. 似乎是很多人都有的问题,但到目前为止我找到的所有答案都无济于事。

Problem: I'm trying to listen to a Velodyne HDL32 that sends its packets via UDP to my pc. 问题:我正在尝试收听通过UDP将数据包发送到我的电脑的Velodyne HDL32。 The OS is 32-bit Ubuntu and Boost library v1.46. 操作系统是32位Ubuntu和Boost库v1.46。

The data i get via Wireshark looks like this: 我通过Wireshark获得的数据如下所示:

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

But with this code, no data is shown to me (Port is correct): 但是使用此代码,没有向我显示数据(端口是正确的):

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));
}

Can anyone identify a problem so far or do you need more information? 到目前为止,任何人都可以识别问题,还是需要更多信息? I appreciate any help I can get. 我感谢任何帮助。

NOTE: I'm NOT running the program with root privileges! 注意:我没有以root权限运行程序!

Some thoughts: Could it be possible that boost::asio::ip::address_v4::any() won't listen to the IP . 一些想法: boost :: asio :: ip :: address_v4 :: any()是否可能不会监听IP .*.255 when having subnetmask 255.255.255.0? 。* .255有子网掩码255.255.255.0?

When using netcat, no data is shown as well. 使用netcat时,也不会显示任何数据。 When I use Windows netcat it works quite fine. 当我使用Windows netcat时它工作得很好。 Same with Wireshark on Linux and Windows - works fine. 与Linux和Windows上的Wireshark相同 - 工作正常。 Tried it with the as well, but with the same effect - no data. 尝试了同样的,但效果相同 - 没有数据。

Thank you all for your trying to help me. 谢谢大家帮助我。 The code was all right, but the problem was on side of the velodyne and the networksetting with it. 代码没问题,但问题出在velodyne和网络设置上。

Explanation for all others who try to work with a Velodyne: 尝试使用Velodyne的所有其他人的解释:

The velodyne has it's own subnetwork (192.168.17.x). velodyne拥有自己的子网(192.168.17.x)。 All recorded data is now send to the subnetwork 192.168.3.x by broadcast. 现在,所有记录的数据都通过广播发送到子网192.168.3.x. Under normal circumstances the data should be received on all IPs in this subnet, but this seems not to be possible. 在正常情况下,应该在该子网中的所有IP上接收数据,但这似乎是不可能的。 The only IP you can receive data is the IP 255 and that just if you use one of these two solutions. 您可以接收数据的唯一IP是IP 255,即使您使用这两种解决方案中的一种也是如此。 (Or use windows or dump a file with wireshark) (或使用windows或使用wireshark转储文件)

1. The stupid but working solution 1.愚蠢但有效的解决方案

Set an gateway to 192.168.3.1. 将网关设置为192.168.3.1。 Yes there is none, but it doesn't matter. 是没有,但没关系。 From now one you will receive data on the IP 255. 从现在开始,您将收到IP 255的数据。

2. The clean solution 2.干净的解决方案

Set a new route that leads all traffic from the subnet of the velodyne to the subnet 192.168.3.x. 设置一条新路由,引导从velodyne子网到子网192.168.3.x的所有流量。

I really don't know why it's so complicaded, but it took us quite some time to discover this "secret". 我真的不知道为什么它如此复杂,但我们花了很长时间才发现这个“秘密”。 Hopefully some of you will profit from our finicky job. 希望你们中的一些人能从我们挑剔的工作中获益。

Have you tried setting the broadcast option ? 您是否尝试过设置广播选项?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM