开发者

boost::asio UDP how to receive messages on Windows?

I have two clas开发者_如何学Cses UDPSender and UDPReceiver.

Senarios:

(1)It works when I run UDPSender in QNX and UDPReceiver in Linux.

(2)It doesn't work when I run UDPSender in QNX and UDPReceiver in WindowsXP.(But I see UDP datagrams using Wireshark, meaning Sender is working as in Senario(1))

(3)It partially works in Windows.

i) if I use Sender sending UDP messages to 127.0.0.1, Receiver can get messages

ii) if I use Sender sending UDP messages to 192.168.1.137(local address in the network), Receiver can NOT receive.

My question is:

How can I get Receiver working on Windows?


some code:

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


It's probably being blocked by the Windows Firewall.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜