Boost logo

Boost Users :

Subject: [Boost-users] boost asio UDP issue...
From: The Novice Coder (boost_at_[hidden])
Date: 2010-11-12 08:36:44


I'm still new to boost, so forgive me for newbie mistakes...   I'm trying to get ASIO working on a project (TCP, UDP, and Serial).  So far, TCP and Serial work fine.  The problem is UDP. 

I've read all the example code I've found, but there's a core difference between how the examples work (reliance constructors to initialize) and how the project is laid out (Construct, configure, initialize).. so most of the examples have not given me enough to solve the issues.

Basically the problem is this - the UDP class can be initialized in two modes - Server (Listening - respond) and Client.  The problem I'm having is I'm 3/4 of the way there.  The server works fine, it will listen and then reply properly, but the client can only send, and never receive. Is there any advice on what I'm doing wrong? 

Here's the open function.

void UDPConnection::open()
{
   if (m_myPort > 0)
   {
      m_senderEndpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), m_myPort);
      m_socket.async_receive( boost::asio::buffer(in_, ASIO_BUFFER_SIZE),
         boost::bind(&UDPConnection::i_read_complete, this, boost::asio::placeholders::error,
         boost::asio::placeholders::bytes_transferred));
      m_socket.open(m_senderEndpoint.protocol());
      m_socket.bind(m_senderEndpoint);
   }
   if (m_foriegnHost.length() > 0)
   {
      boost::asio::ip::udp::resolver resolver(ioService_);
      boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), m_foriegnHost, m_foreignPort);
      m_iterator = resolver.resolve(query);
      m_senderEndpoint = *m_iterator;
      if (m_myPort == 0) // if myPort is set, we already opened.
         m_socket.connect(*m_iterator);
      // SPECIAL NOTE: The following code is .. Wrong.  It still doesn't help. :(
      if (m_myPort == 0)
      {
         boost::asio::ip::udp::endpoint endpoint = m_socket.local_endpoint();
         m_myPort = endpoint.port();
         m_socket.async_receive_from( boost::asio::buffer(in_,ASIO_BUFFER_SIZE),m_senderEndpoint,
            boost::bind(&UDPConnection::i_read_complete, this,boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
      }
   }
   if (m_inBufferSize < MIN_TCP_BUFFER)
      m_inBufferSize = MIN_TCP_BUFFER;
   m_socket.set_option(boost::asio::socket_base::receive_buffer_size(m_inBufferSize) );
   if (m_outBufferSize < MIN_TCP_BUFFER)
      m_outBufferSize = MIN_TCP_BUFFER;
   m_socket.set_option(boost::asio::socket_base::send_buffer_size(m_outBufferSize) );
   m_socket.set_option( boost::asio::socket_base::reuse_address(true)
);


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net