title: How to pass a boost asio tcp socket to a thread for sending heartbeat to client or server
body: <p>I am writing a client/server program in boost TCP in which I want to send a HEARTBEAT message to the client every 2 seconds for which I am trying to create a new thread by which I can send it easily but unable to solve it. I am creating thread using <code>boost::thread t(hearbeatSender,sock);</code> this. but giving lots of errors. I also use bind to bind function name with the socket but not resolved the error.</p>
  
  <pre><code>void process(boost::asio::ip::tcp::socket &amp; sock);
  std::string read_data(boost::asio::ip::tcp::socket &amp; sock);
  void write_data(boost::asio::ip::tcp::socket &amp; sock,std::string);
  void hearbeatSender(boost::asio::ip::tcp::socket &amp; sock);
  int main()
  {
  
      unsigned short port_num = 3333;
      boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address_v4::any(), port_num);
      boost::asio::io_service io;
      try
      {
          boost::asio::ip::tcp::acceptor acceptor(io, ep.protocol());
          acceptor.bind(ep);
          acceptor.listen();
          boost::asio::ip::tcp::socket sock(io);
          acceptor.accept(sock);
          boost::thread t(hearbeatSender,sock); 
          process(sock);
          t.join();
  
      }
      catch (boost::system::system_error &amp;e)
      {
          std::cout &lt;&lt; "Error occured! Error code = " &lt;&lt; e.code()
          &lt;&lt; ". Message: " &lt;&lt; e.what();
  
          return e.code().value();
      }
    return 0;
  
  }
  void process(boost::asio::ip::tcp::socket &amp; sock)
  {
      while(1){
      std::string data = read_data(sock);
      std::cout&lt;&lt;"Client's request is: "&lt;&lt;data&lt;&lt;std::endl;
      write_data(sock,data);
      }
  }
  std::string read_data(boost::asio::ip::tcp::socket &amp; sock)
  {
      boost::asio::streambuf buf;
      boost::asio::read_until(sock, buf, "\n");
      std::string data = boost::asio::buffer_cast&lt;const char*&gt;(buf.data());
      return data;
  }
  void write_data(boost::asio::ip::tcp::socket &amp; sock,std::string data)
  {
      boost::system::error_code error;
      std::string msg;
      int ch = data[0]-'0';
      switch(ch)
      {
          case 1: msg = "Case 1\n"; break;
          case 2: msg = "Case 2\n"; break;
          case 3: msg = "Case 3\n"; break;
          case 4: msg = "Case 4\n"; break;
          default: msg  = "Case default\n"; break;
      }
      boost::asio::write( sock, boost::asio::buffer(msg+ "\n"), error );
       if( !error ) {
          std::cout &lt;&lt; "Server sent hello message!" &lt;&lt; std::endl;
       }
       else {
          std::cout &lt;&lt; "send failed: " &lt;&lt; error.message() &lt;&lt; std::endl;
       }
  }
  void hearbeatSender(boost::asio::ip::tcp::socket &amp; sock)
  {
      boost::system::error_code error;
      std::string msg = "HEARTBEAT";
      while(1)
      {
          sleep(2);
          std::cout&lt;&lt;msg&lt;&lt;std::endl;
          boost::asio::write( sock, boost::asio::buffer(msg+ "\n"), error );
          if( !error ) {
          std::cout &lt;&lt; "Server sent HEARTBEAT message!" &lt;&lt; std::endl;
          }
          else {
              std::cout &lt;&lt; "send failed: " &lt;&lt; error.message() &lt;&lt; std::endl;
          }
      }
  }
  
  </code></pre>
  
  <p>This is a server-side code for responding to the message of the client and sending heartbeat to the client. This is a synchronous TCP server.</p>