boost :: asio UDP如何在Windows上接收消息?

时间:2011-07-06 01:41:04

标签: c++ udp boost-asio

我有两个类UDPSender和UDPReceiver Senarios:
(1)当我在QNX中运行UDPSender和在Linux中运行UDPReceiver时,它可以工作 (2)当我在QXX中运行UDPSender并在WindowsXP中运行UDPReceiver时,它不起作用。(但我看到UDP数据报使用Wireshark,意味着Sender在Senario(1)中工作)
(3)它部分适用于Windows     i)如果我使用Sender发送UDP消息到127.0.0.1,Receiver可以获得消息
    ii)如果我使用发送方发送UDP消息到192.168.1.137(网络中的本地地址),接收方无法接收。

我的问题是:

如何让Receiver在Windows上运行?


一些代码:

UDPReceiver::UDPReceiver( boost::asio::io_service& service, unsigned short port )
: m_service( service ),
  m_socket( service, udp::endpoint(udp::v4(), port)),
  m_peerEndpoint(),
  m_buffer(),
  m_messageReceipient()
{
}

void UDPReceiver::SetMessageReceipient( const boost::function<void(const char*, unsigned int)>& r )
{
     m_messageReceipient = r;
}

void UDPReceiver::Start()
{
    m_socket.async_receive_from(
        boost::asio::buffer(m_buffer, m_buffer.size()), m_peerEndpoint,
        boost::bind( &UDPReceiver::HandleReceived, shared_from_this(),
                          boost::asio::placeholders::error,
                          boost::asio::placeholders::bytes_transferred));
}

void UDPReceiver::Stop()
{
    m_socket.close();
}

void UDPReceiver::HandleReceived( const boost::system::error_code& error, size_t bytesReceived )
{
    assert( !m_messageReceipient.empty() );
    if( error ) {
       LOG( Comms, Error, "UDPReceiver:" << error.message() );
    }
    else {
        m_messageReceipient( &m_buffer.front(), bytesReceived );
        m_socket.async_receive_from(
            boost::asio::buffer(m_buffer, m_buffer.size()), m_peerEndpoint,
            boost::bind( &UDPReceiver::HandleReceived, shared_from_this(),
                          boost::asio::placeholders::error,
                          boost::asio::placeholders::bytes_transferred));
    }
}

1 个答案:

答案 0 :(得分:2)

它可能被Windows防火墙阻止。