My program require me to keep writing while also need to be able to receive incoming data.
This is what I tried. I tried to put async_receive in separate thread that constantly receiving data. Also I add infinite while loop to keep sending data. However, I am not be able to receive anything.
class UDPAsyncServer {
public:
  UDPAsyncServer(asio::io_service& service, 
                 unsigned short port) 
     : socket(service, 
          asio::ip::udp::endpoint(asio::ip::udp::v4(), port))
  {  
     boost::thread receiveThread(boost::bind(&UDPAsyncServer::waitForReceive, this));
     receiveThread.join();
     while(1) {
         sendingData();
     }
  }
  void waitForReceive() {
    socket.async_receive_from(asio::buffer(buffer, MAXBUF),
          remote_peer,
          [this] (const sys::error_code& ec,
                  size_t sz) {
            const char *msg = "hello from server";
            std::cout << "Received: [" << buffer << "] "
                      << remote_peer << '\n';
            waitForReceive();
            socket.async_send_to(
                asio::buffer(msg, strlen(msg)),
                remote_peer,
                [this](const sys::error_code& ec,
                       size_t sz) {});
          });
  }
  void sendingData() {
          std::cout << "Sending" << "\n";
          //In this code, I will check the data need to be send,
          //If exists, call async_send
          boost::this_thread::sleep(boost::posix_time::seconds(2));
      }
private:
  asio::ip::udp::socket socket;
  asio::ip::udp::endpoint remote_peer;
  char buffer[MAXBUF];
};
If I commented out the while (1) { sendingData(); }; the receive function working fine.
Thanks in advance.
 
     
    