title: C++ - Running one server thread and one client thread in the same program
body: <p>I've been trying to modify boost's daytime server/client example to run in 2 threads as below but it didn't work:</p>
  
  <pre><code>#include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  
  using boost::asio::ip::tcp;
  
  std::string make_daytime_string()
  {
    using namespace std; // For time_t, time and ctime;
    time_t now = time(0);
    return ctime(&amp;now);
  }
  
  void ServerThread()
  {
      boost::asio::io_service io_service;
      tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
  
      for (;;)
      {
          std::cout &lt;&lt; "why?";
          tcp::socket socket(io_service);
          acceptor.accept(socket);
          std::string message = make_daytime_string();
          boost::system::error_code ignored_error;
          boost::asio::write(socket, boost::asio::buffer(message),
          boost::asio::transfer_all(), ignored_error);
      }
  
  }
  
  void ClientThread()
  {
      try
    {
      boost::asio::io_service io_service;
  
      std::string hostString;
  
      std::cout &lt;&lt; "Please enter the host: ";
      std::cin &gt;&gt; hostString;
  
      tcp::resolver resolver(io_service);
      tcp::resolver::query query(hostString, "daytime");
      tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
  
      tcp::socket socket(io_service);
      boost::asio::connect(socket, endpoint_iterator);
    }
    catch (std::exception&amp; e)
    {
      std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
    }
  }
  
  int main()
  {
    boost::thread s(&amp;ServerThread);
    boost::thread c(&amp;ClientThread);
  
    c.join();
    s.join();
  
    return 0;
  }
  </code></pre>
  
  <p>The two threads run immediately one after another and will not process correctly. I guess I need to do something along the line of a fork? Please help telling me what's wrong with the code, thank you.</p>