title: Handling "reset by peer" scenario with boost::asio
body: <p>I have a server method that waits for new incoming <code>TCP</code> connections, for each connection I'm creating two threads (detached) for handling various tasks.</p>
  
  <pre><code>    void MyClass::startServer(boost::asio::io_service&amp; io_service, unsigned short port) {
  
          tcp::acceptor TCPAcceptor(io_service, tcp::endpoint(tcp::v4(), port));
  
          bool UARTToWiFiGatewayStarted = false;
  
          for (;;) {
  
              auto socket(std::shared_ptr&lt;tcp::socket&gt;(new tcp::socket(io_service)));
  
              /*!
               * Accept a new connected WiFi client.
               */
              TCPAcceptor.accept(*socket);
  
              socket-&gt;set_option( tcp::no_delay( true ) );
  
              MyClass::enableCommunicationSession();
  
              // start one worker thread.
              std::thread(WiFiToUARTWorkerSession, socket, this-&gt;LINport, this-&gt;LINbaud).detach();
  
              // only if this is the first connected client:
              if(false == UARTToWiFiGatewayStarted) {
  
                  std::thread(UARTToWifiWorkerSession, socket, this-&gt;UARTport, this-&gt;UARTbaud).detach();
  
                  UARTToWiFiGatewayStarted = true;
              }
          }
      }
  </code></pre>
  
  <p>This works fine for starting the communication, but the problem appears when the client disconnects and connects again (or at least tries to connect again).</p>
  
  <p>When the current client disconnects, I stop the communication (by stopping the internal infinite loops from both functions, then they'll return).</p>
  
  <pre><code>    void Gateway::WiFiToUARTWorkerSession(std::shared_ptr&lt;tcp::socket&gt; socket, ...) {
  
          /*!
           * various code here...
           */
  
          try {
              while(true == MyClass::communicationSessionStatus) {
  
                  /*!
                   * Buffer used for storing the UART-incoming data.
                   */
                  unsigned char WiFiDataBuffer[max_incoming_wifi_data_length];
  
                  boost::system::error_code error;
  
                  /*!
                   * Read the WiFi-available data.
                   */
                  size_t length = socket-&gt;read_some(boost::asio::buffer(WiFiDataBuffer), error);
  
                  /*!
                   * Handle possible read errors.
                   */
                  if (error == boost::asio::error::eof) {
                      break; // Connection closed cleanly by peer.
                  }
                  else if (error) {
                      // this will cause the infinite loops from the both worker functions to stop, and when they stop the functions will return.
                      MyClass::disableCommunicationSession();
                      sleep(1);
                      throw boost::system::system_error(error); // Some other error.
                  }
  
                  uart-&gt;write(WiFiDataBuffer, length);
              }
          }
          catch (std::exception &amp;exception) {
              std::cerr &lt;&lt; "[APP::exception] Exception in thread: " &lt;&lt; exception.what() &lt;&lt; std::endl;
          }
      }
  </code></pre>
  
  <p>I expect that when I reconnect the communication should work again (the <code>MyClass::startServer(...)</code> will create and <code>detach</code> again two worker threads that will do the same things.</p>
  
  <p>The problem is that when I connect the second time I get:</p>
  
  <pre><code>terminate called after throwing an instance of 'boost::exception_detail::clone_impl&lt;boost::exception_detail::error_info_injector&lt;boost::system::system_error&gt; &gt;'
    what():  write: Broken pipe
  </code></pre>
  
  <p>From what I found about this error it seems that the server (this application) sends something via <code>TCP</code> to a client that was disconnected.</p>
  
  <p>What I'm doing wrong?</p>
  
  <p>How can I solve this problem?</p>