title: Server and Client at same time with Boost-Asio
body: <p>I am an AspNet programmer with 57 years of age. Because I was the only one who worked a little, back in the beginning, with C ++, my bosses asked me to serve a customer who needs a communication agent with very specific characteristics. It can run as a daemon on multiple platforms and be both client and server at times. I do not know enough but I have to solve the problem and found a chance in the Boost / Asio library.</p>
  
  <p>I am new to Boost-Asio and reading the documentation I created a server and a TCP socket client that exchanges messages perfectly and two-way, full duplex.</p>
  
  <p>I read several posts where they asked for the same things I want, but all the answers suggested full duplex as if that meant having a client and a server in the same program. And it's not. The definition of full duplex refers to the ability to write and read from the same connection and every TCP connection is full duplex by default.</p>
  
  <p>I need to make two programs can accept connections initiated by the other. There will be no permanent connection between the two programs. Sometimes one of them will ask for a connection and at other times the other will make this request and both need to be listening, accepting the connection, exchanging some messages and terminating the connection until new request is made.</p>
  
  <p>The server I did seems to get stuck in the process of listening to the port to see if a connection is coming in and I can not continue with the process to be able to create a socket and request a connection with the other program. I need threads but I do not know enough about them.</p>
  
  <p>It'is possible? </p>
  
  <p>As I said I'm new to Boost / Asio and I tried to follow some documents of threads and Coroutines. Then I put the client codes in one method and the server in another.:</p>
  
  <pre><code>int main(int argc, char* argv[])
  {
      try
      {
          boost::thread t1(&amp;server_agent);
          boost::thread t2(&amp;client_agent);
  
          // wait
          t1.join();
          t2.join();   
          return 0;       
      }
      catch (std::exception&amp; e)
      {
          std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n";
      }    
      return 0;
  }
  </code></pre>
  
  <p>and two Coroutines:</p>
  
  <pre><code>void client_agent() {
      parameters param;
      param.load();
  
      boost::asio::io_service io_service1;
      tcp::resolver resolver(io_service1);
      char port[5];
      _itoa(param.getNrPortaServComunic(), port, 10);
      auto endpoint_iterator = resolver.resolve({ param.getIPServComunicPrincipal(), port });
      std::list&lt;client&gt; clients;
      client c(io_service1, endpoint_iterator, param);
  
      while (true)
      {
          BOOL enviada = FALSE;
          while (true) {
              if (!enviada) {
                  std::cout &lt;&lt; "sending a message\n";
                  int nr = 110;
                  message msg(nr, param);
                  c.write(msg);
                  enviada = TRUE;
              }
          }
      }
  
      c.close();
  }
  
  void server_agent() {
  
      parameters param;
      param.load();
  
      boost::asio::io_service io_service1;
      std::list&lt;server&gt; servers;
      tcp::endpoint endpoint(tcp::v4(), param.getNrPortaAgenteServ());
      servers.emplace_back(io_service1, endpoint);
      io_service1.run();
  }
  </code></pre>
  
  <p>I used one port to client endpoint and other port to server endpoint. Is it correct? Required?</p>
  
  <p>It starts looking like it's going to work. Each of the methods runs concurrently but then I get a thread allocation error at the io_service1.run (last line of the server_agent method):</p>
  
  <p>boost::exception_detail::clone_impl > at memory location 0x0118C61C.</p>
  
  <p>Any suggestion?</p>