title: UDP communication using c++ boost asio
body: <p>I need to communicate with a different device in a private network over UDP. I am new to using boost, but based on what I searched online and also the tutorials on Boost website, I came up with below code.. I am currently trying to send and receive data from my own device. Just to unit test and finalize the code.</p>
  
  <p>Question: I am unable to receive any message. What am I missing?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdio&gt;
  #include &lt;cstdlib&gt;
  #include &lt;string&gt;
  #include "boost/asio.hpp"
  #include &lt;thread&gt;
  #include &lt;boost/array.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  
  #define SRVR_UDP_PORT  10251
  #define CLNT_UDP_PORT 10252
  
  boost::array&lt;char, 1024&gt; recv_buffer;
  
  void Sender(std::string in)
  {
      boost::asio::io_service io_service;
      boost::asio::ip::udp::socket socket(io_service);
      boost::asio::ip::udp::endpoint remote_endpoint;
      socket.open(boost::asio::ip::udp::v4());
      remote_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address::from_string("192.168.1.64"), SRVR_UDP_PORT);
  
      boost::system::error_code err;
      socket.send_to(boost::asio::buffer(in.c_str(), in.size()), remote_endpoint, 0, err);
      socket.close();
      //int i =0;
      printf("Sending Payload --- \n");
  }
  
  void handle_receive(const boost::system::error_code&amp; error, size_t bytes_transferred)
  {
      std::cout &lt;&lt; "Received: '" &lt;&lt; std::string(recv_buffer.begin(), recv_buffer.begin()+bytes_transferred) &lt;&lt; "'\n";
  }
  
  
  void Receiver()
  {
      while(1)
      {
          boost::asio::io_service io_service;
          boost::asio::ip::udp::socket socket(io_service);
          boost::asio::ip::udp::endpoint remote_endpoint;
  
          //socket.open(boost::asio::ip::udp::v4());
          boost::system::error_code err;
          remote_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address::from_string("192.168.1.64"), CLNT_UDP_PORT);
  
          socket.open(boost::asio::ip::udp::v4());
          //https://stackoverflow.com/questions/26820215/boost-asio-udp-client-async-receive-from-calls-handler-even-when-there-are-no-in
          socket.async_receive_from(boost::asio::buffer(recv_buffer),
                                      remote_endpoint,
                                      boost::bind(handle_receive,
                                      boost::asio::placeholders::error,
                                      boost::asio::placeholders::bytes_transferred));                                    
                                      //socket.close();
      }
      int i = 0;
      printf("Received Payload --- %d", i);
  
  }
  
  int main(int argc, char *argv[])
  {
      //std::thread s(Sender);
      std::thread r(Receiver);
      //s.join();
      std::string input = argv[1];
      printf("Input is %s\nSending it to Sender Function...\n", input.c_str());
      Sender(input);
      r.join();
      return 0;
  }
  </code></pre>