无法通过boost :: asio监听UDP端口 [英] Fail to listen to UDP Port with boost::asio

查看:514
本文介绍了无法通过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端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆