|
Boost : |
Subject: [boost] how to receive UDP in Windows using boost::asio@boost1.46.0
From: Jinqiang Zhang (jasonzha_at_[hidden])
Date: 2011-07-05 21:42:06
I have two classes 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));
}
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk