使用 boost::asio 监听 UDP 端口失败

Fail to listen to UDP Port with boost::asio(使用 boost::asio 监听 UDP 端口失败)
本文介绍了使用 boost::asio 监听 UDP 端口失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器,它收集信息并通过本地网络广播一些消息.我正在使用 boost::asio 在端口 8079 上通过 UDP 广播这些数据包,我可以使用 WireShark 验证这些数据包实际上是按预期广播的.

I have a server that gathers information and broadcasts some messages across the local network. I'm using boost::asio to broadcast these via UDP on port 8079 and I can verify with WireShark that these packets are actually broadcasted as intended.

现在,很自然地,我想跟进一个可以对这些消息做出反应的听众,但我很难收到任何东西.我目前的做法是:

Now, naturally, I want to follow up with a listener that can react to these messages, but I am struggling to receive anything. My current approach is:

boost::asio::io_service io_service;
boost::asio::ip::udp::socket socket(io_service);
boost::asio::ip::udp::endpoint local(
    boost::asio::ip::address::from_string("192.168.2.102"),
    8079);
boost::system::error_code error;

std::cout << "Local bind: " << local << std::endl;

socket.open(boost::asio::ip::udp::v4(), error);
if(!error) {
    socket.bind(local);
    boost::array<char, 2048> buf;
    boost::asio::ip::udp::endpoint server;
    std::cout << "Listening..." << std::endl;
    while(true) {
        size_t len = socket.receive_from(boost::asio::buffer(buf), server);
        std::cout << "Received data:" << std::endl;
        std::cout.write(buf.data(), len);
        std::cout << std::endl;
    }
}

但我从来没有收到任何东西.使用调试器,我发现我只是永远卡在 receive_from 中,不知道为什么.

But I never receive anything. Using the debugger, I found that I'm just stuck in receive_from forever, and I don't know why.

我不确定是否会导致这些问题的一些进一步信息(主要来自 Wireshark):服务器和客户端在同一台机器上运行.服务器每两秒从端口 34050(源)向 8079(目标)发送一条发送 88 bytes 消息.192.168.2.102是本地网络中机器的ip.

Some further information (mostly from Wireshark) that I'm not sure about whether it could be causing these problems: Server and client are running on the same machine. The server is sending a sending an 88 bytes message every two seconds from port 34050 (source) to 8079 (destination). 192.168.2.102 is the ip of the machine within the local network.

推荐答案

IIRC,你必须绑定到 INADDR_ANY 才能接收广播包.Linux 消息列表 中有很多讨论这个问题.除此之外,请确保两台计算机上的网络掩码匹配.如果广播到 192.168.255.255 并且您的客户端网络掩码是 255.255.255.0,您将不会收到数据包.

IIRC, you have to bind to INADDR_ANY to receive broadcast packets. There are quite a few discussions in Linux message lists discussing this issue. Beyond this, make sure that the netmask matches on both computers. If the broadcast is going to 192.168.255.255 and your client netmask is 255.255.255.0, you will not receive the packets.

这篇关于使用 boost::asio 监听 UDP 端口失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)