使用boost asio

时间:2015-11-24 19:18:36

标签: c++ boost multicast

我在SO上看到了一些其他相关问题,但没有一个能帮我解决问题。

我有以下代码:

class MulticastReceiver
{
public:

    MulticastReceiver(const String& ip, const UInt16 port)  :
        m_endpoint(boost::asio::ip::udp::v4(), port),
        m_socket(m_service, m_endpoint)
    {
        boost::asio::ip::address addr = boost::asio::ip::address::from_string(ip);
        if (!addr.is_multicast()) throw std::exception("excpeted a multicast address");

        m_socket.set_option(boost::asio::ip::multicast::join_group(addr.to_v4()));
        m_socket.set_option(boost::asio::socket_base::receive_buffer_size(8 * 1024 * 1024));
        m_socket.set_option(boost::asio::socket_base::reuse_address(true));
    }

    size_t receive(void* msg, const size_t size)
    {
        boost::system::error_code error;
        const size_t received = m_socket.receive_from(boost::asio::buffer(msg, size), m_endpoint, 0, error);
        if (error && error != boost::asio::error::message_size)
        {
            throw boost::system::system_error(error);
            return 0;
        }
        return received;
    }

private:

    boost::asio::io_service m_service;
    boost::asio::ip::udp::endpoint m_endpoint;
    boost::asio::ip::udp::socket m_socket;

};

// test code
MulticastReceiver recv(ip, port);
char msg[2000];
while (true)
{
    const size_t size = recv.receive(&msg[0], 2000);
    cout << "Received a message sized: " << size << endl;
}

如果从同一服务器发送和接收,一切都很完美,但是,如果我从其他服务器发送,则不会收到数据包。使用Wireshark,我可以看到所有数据包都到达预期的接口,预期有多播地址和端口。

注意:接收服务器是Windows,如果重要的话,已经测试了从Windows和Linux发送的信息。

任何想法为什么会发生这种情况将不胜感激。

0 个答案:

没有答案