title: asynchronous UDP client/server communication (BOOST) - server does not read
body: <p>Hello I ve been trying to implement a simple server/client app to communicate through UDP socket and understand how UDP works using boost library, my problem is that async_receive is not being invoked or is not getting complete in order to jump on the handler</p>
  
  <p>UDP server:</p>
  
  <pre><code>#include "udp_server.h"
  
  
  udp_server::udp_server(boost::asio::io_service&amp; io_service, string bind_address, uint16_t bind_port)
      : socket_(io_service)
  {
      cout &lt;&lt; "udp_server constructor start" &lt;&lt; endl;
  
  
      boost::shared_ptr&lt;boost::asio::io_service::work&gt; work(
          new boost::asio::io_service::work(io_service));
  
      for(int x=0; x&lt;5; ++x)
      {
          worker_threads.create_thread(boost::bind(&amp;udp_server::WorkerThread, this , boost::ref(io_service)));
      }
  
      boost::system::error_code myError;
  
      boost::asio::ip::address IP;
      IP = boost::asio::ip::address::from_string(bind_address, myError); 
  
      local_udppoint_.address(IP);
      cout &lt;&lt; "IP Address: " &lt;&lt; local_udppoint_.address().to_string() &lt;&lt; endl;
      local_udppoint_.port(bind_port);
      cout &lt;&lt; "Port: " &lt;&lt; local_udppoint_.port() &lt;&lt; endl;
  
      socket_.open(local_udppoint_.protocol(), myError);
      std::cout &lt;&lt; "Open - " &lt;&lt; myError.message() &lt;&lt; std::endl;
      socket_.bind( local_udppoint_, myError );
      std::cout &lt;&lt; "Bind - " &lt;&lt; myError.message() &lt;&lt; std::endl;
  
      udp::endpoint sender_endpoint_;
  
      struct test *request = (struct test *) malloc (sizeof(struct test));
  
      socket_.async_receive_from(
          boost::asio::buffer(&amp;request, sizeof(request-&gt;type)), sender_endpoint_,
          boost::bind(&amp;udp_server::handle_receive_from, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
  
      cout &lt;&lt; "udp_server constructor end" &lt;&lt; endl;
  }
  
  void udp_server::WorkerThread(io_service &amp;io_service_)
  {
      std::cout &lt;&lt; "Thread Start\n";
      io_service_.run();
      std::cout &lt;&lt; "Thread Finish\n";
  }
  
  void udp_server::handle_receive_from(const boost::system::error_code&amp; err, size_t bytes_recvd)
  {
      cout &lt;&lt; "udp_server::handle_receive_from enters?" &lt;&lt; endl;
      if(!err)
      {
          cout &lt;&lt; "no message" &lt;&lt; endl;
      }
      else
      {
          cout &lt;&lt; err.message() &lt;&lt; endl;
      }
  
      if (!err &amp;&amp; bytes_recvd &gt; 0)
      {
          cout &lt;&lt; "All good" &lt;&lt; endl;
      }
      else
      {
  
          cout &lt;&lt; err.message() &lt;&lt; "2" &lt;&lt; endl;
      }
  }
  
  udp_server::~udp_server(void)
  {
      //io_service.stop();
  
      worker_threads.join_all();
  
      cout &lt;&lt; "udp_server::destructor"  &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Server's Main:</p>
  
  <pre><code>#include "udp_server.h"
  
  int main()
  {
    try
    {
      boost::asio::io_service io_service;
      //boost::asio::io_service::work work( io_service);
      udp_server s(io_service, "127.0.0.1", 4000);
  
      //io_service.run();
    }
    catch (std::exception&amp; e)
    {
      std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n";
    }
  
    string a;
    cin &gt;&gt; a;
    return 0;
  
  }
  </code></pre>
  
  <p>UDP Client:</p>
  
  <pre><code>#include "udp_client.h"
  
  
  udp_client::udp_client(boost::asio::io_service&amp; io_service, string send_address, uint16_t send_port)
      : io_service_(io_service), socket_(io_service)
  {
      cout &lt;&lt; "udp_client::constructor_start" &lt;&lt; endl;
  
      boost::system::error_code myError;
  
      boost::asio::ip::address IP;
      IP = boost::asio::ip::address::from_string(send_address, myError); 
  
      remote_endpoint_.address(IP);
      cout &lt;&lt; "IP Address: " &lt;&lt; remote_endpoint_.address().to_string() &lt;&lt; endl;
      remote_endpoint_.port(send_port);
      cout &lt;&lt; "Port: " &lt;&lt; remote_endpoint_.port() &lt;&lt; endl;
  
      struct test *request = (struct test *) malloc (sizeof(struct test));
  
      request-&gt;type = START_STORAGE;
  
      socket_.async_send_to(boost::asio::buffer(&amp;request, sizeof(request-&gt;type)), remote_endpoint_,
          boost::bind(&amp;udp_client::start_handler, this, boost::asio::placeholders::error,
                          boost::asio::placeholders::bytes_transferred));
  
      cout &lt;&lt; "udp_client::constructor_end" &lt;&lt; endl;
  }
  
  void
  udp_client::start_handler(const boost::system::error_code&amp;, std::size_t)
  {
      cout &lt;&lt; "udp_client::start_handler()" &lt;&lt; endl;
  }
  
  udp_client::~udp_client(void)
  {
  }
  </code></pre>
  
  <p>Client's main:</p>
  
  <pre><code>#include "udp_client.h"
  
  int main(int argc, char* argv[])
  {
    try
    {
      boost::asio::io_service io_service;
  
      udp_client client(io_service, "127.0.0.1", 4000);
  
      io_service.run ();
    }
    catch (std::exception&amp; e)
    {
      std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
    }
  
    string a;
    cin &gt;&gt; a;
    return 0;
  }
  </code></pre>
  
  <p>As you can see in the outputs below client invoked async_send_to and the message on the handler is being printed but on the server side nothing happens</p>
  
  <p>UDP Server Console output:</p>
  
  <pre><code>udp_server constructor star
  Thread Start
  Thread Start
  Thread Start
  Thread Start
  Thread Start
  IP Address: 127.0.0.1
  Port: 4000
  Open - The operation completed successfully
  Bind - The operation completed successfullyudp_server constructor end
  _
  </code></pre>
  
  <p>UDP Client Console:</p>
  
  <pre><code>udp_client::constructor_start
  IP Address: 127.0.0.1
  Port: 4000
  udp_client::constructor_end
  udp_client::start_handler()
  </code></pre>
  
  <p>Any ideas why async_receive_from is not completed or invoked?</p>