title: Boost TCP server crash when client disconnects while server performs asio::write operation
body: <p>Just to test my server, I've created 100 requests using a for loop from my client side and while my server is writing response for the Nth request I deliberately pressed control+c from the client, Thats it. The server stops and has gone unresponsive although I try connecting it using a new connection, can any one advice me how to make my server stable and immune to such interrupt.
  here is my server:</p>
  
  <pre><code>class tcp_server
   {
   public:
  
   tcp_server(boost::asio::io_service&amp; io_service)
     : acceptor_(io_service, tcp::endpoint(tcp::v4(), 2020))
   {
      start_accept();
   }
  
   private:
  
   void start_accept()
   {
      tcp_connection::pointer new_connection =
          tcp_connection::create(acceptor_.get_io_service());
  
      acceptor_.async_accept(new_connection-&gt;socket(),
          boost::bind(&amp;tcp_server::handle_accept, this, new_connection,
          boost::asio::placeholders::error));
   }
  
   void handle_user_read(const boost::system::error_code&amp; err,
      std::size_t bytes_transferred)
   {
   }
  
  
   void handle_accept(tcp_connection::pointer new_connection,
      const boost::system::error_code&amp; error)
   {
      if (!error)
      {
         new_connection-&gt;start();
         start_accept();
      }
  
   }
   tcp::acceptor acceptor_;
   };
  </code></pre>
  
  <p>here is my tcp connection:</p>
  
  <pre><code>class tcp_connection : public boost::enable_shared_from_this&lt;tcp_connection&gt;
   {
     public:
  
     typedef boost::shared_ptr&lt;tcp_connection&gt; pointer;
  
     static pointer create(boost::asio::io_service&amp; io_service)
     {
        return pointer(new tcp_connection(io_service));
     }
  
     tcp::socket&amp; socket()
     {
        return socket_;
     }
  
     void start()
     {
        // Start reading messages from the server
        start_read();
  
     }
  
     public:
  
     tcp_connection(boost::asio::io_service&amp; io_service) : socket_(io_service),timer_(io_service), io(io_service),timer2_(io_service)
     {
            }
  
  
     // Reading messages from the server
     void start_read()
     {
            boost::asio::async_read(socket_, input_buffer_,
            boost::asio::transfer_at_least(1),
            boost::bind(&amp;tcp_connection::handle_read, shared_from_this(),
            boost::asio::placeholders::error));
            timer_.expires_from_now(boost::posix_time::seconds(120));
            timer_.async_wait(boost::bind(&amp;tcp_connection::close, shared_from_this()));
  
     }
     void close()
    {
      cout&lt;&lt;"I didn't hear the client yet:closing the socket......"&lt;&lt;endl;
      socket_.close();
    }
  
  
     // When stream is received, handle the message from the client
    int handle_read(const boost::system::error_code&amp; ec)
     {
  
        if (!ec)
        {
  
            std::istream is(&amp;input_buffer_);
            std::string line;
            std::getline(is, line);
            messageFromClient_+=line;
            messageFromClient_.erase(std::remove(messageFromClient_.begin(), messageFromClient_.end(), '\n'), messageFromClient_.end());
            std::size_t found = messageFromClient_.find('\0');
            if(found==std::string::npos)
            {
            boost::asio::async_read(socket_, input_buffer_,
            boost::asio::transfer_at_least(1),
            boost::bind(&amp;tcp_connection::handle_read, shared_from_this(),
            boost::asio::placeholders::error));
            }
            else{
              performmaj();--my logic never mind.
              std::cout &lt;&lt; "Request: "&lt;&lt;i&lt;&lt;" is on process......"&lt;&lt;"\n";--mylogic 
              boost::asio::ip::tcp::no_delay option(true);
              socket_.set_option(option);
              write();
              messageToClient_="";--my logic.
              boost::system::error_code tc;
              socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_send, tc);
              }
              std::cout &lt;&lt; "Request: "&lt;&lt;i&lt;&lt;" completed"&lt;&lt;"\n";
              ++i;
  (boost::asio::io_service io);
            }else
         {
          std::cout &lt;&lt; "Error on receive: " &lt;&lt; ec.message() &lt;&lt; "\n";
         }
  
     }
  
  void write()
  {
      try{
      boost::asio::write(socket_,boost::asio::buffer(messageToClient_), boost::asio::transfer_at_least(messageToClient_.size()));
      }catch(exception e)
      {
          cout&lt;&lt;e.what()&lt;&lt;endl;
          socket_.close();
          io.run();
      }
  
  }
  </code></pre>
  
  <p>please see my below code where i have used async_write; Note the string i have intende to write is of size:11279204. but using async_write in the below code let my client to recieve the meaasage that is more of  partial but not complete.</p>
  
  <pre><code>class tcp_connection : public boost::enable_shared_from_this&lt;tcp_connection&gt;
   {
     public:
  
     typedef boost::shared_ptr&lt;tcp_connection&gt; pointer;
  
     static pointer create(boost::asio::io_service&amp; io_service)
     {
        return pointer(new tcp_connection(io_service));
     }
  
     tcp::socket&amp; socket()
     {
        return socket_;
     }
  
     void start()
     {
        // Start reading messages from the server
        start_read();
  
     }
  
     public:
  
     tcp_connection(boost::asio::io_service&amp; io_service) : socket_(io_service),timer_(io_service), io(io_service),timer2_(io_service)
     {
         //io=io_service;
     }
  
  
     // Reading messages from the server
     void start_read()
     {
            boost::asio::async_read(socket_, input_buffer_,
            boost::asio::transfer_at_least(1),
            boost::bind(&amp;tcp_connection::handle_read, shared_from_this(),
            boost::asio::placeholders::error));
            timer_.expires_from_now(boost::posix_time::seconds(120));
            timer_.async_wait(boost::bind(&amp;tcp_connection::close, shared_from_this()));
  
     }
     void close()
    {
      cout&lt;&lt;"I didn't hear the client yet:closing the socket......"&lt;&lt;endl;
      socket_.close();
    }
  
  
     // When stream is received, handle the message from the client
    int handle_read(const boost::system::error_code&amp; ec)
     {
  
        if (!ec)
        {
  
            std::istream is(&amp;input_buffer_);
            std::string line;
            std::getline(is, line);
            messageFromClient_+=line;
            messageFromClient_.erase(std::remove(messageFromClient_.begin(), messageFromClient_.end(), '\n'), messageFromClient_.end());
            std::size_t found = messageFromClient_.find('\0');
            if(found==std::string::npos)
            {
            boost::asio::async_read(socket_, input_buffer_,
            boost::asio::transfer_at_least(1),
            boost::bind(&amp;tcp_connection::handle_read, shared_from_this(),
            boost::asio::placeholders::error));
            }
            else{
              performmaj();
              cout&lt;&lt;messageToClient_.size()&lt;&lt;endl;--11279204
                try{
              boost::asio::async_write(socket_, boost::asio::buffer(messageToClient_.data(),messageToClient_.size()),
             // boost::asio::transfer_at_least(messageToClient_.size()),
              boost::bind(&amp;tcp_connection::handle_write, shared_from_this(),
              boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
              }catch(exception e)
              {
                  Shutdown();
              }
              }
              std::cout &lt;&lt; "Request: "&lt;&lt;i&lt;&lt;" completed"&lt;&lt;"\n";
              ++i;
              return 0;
  
            }else
         {
          std::cout &lt;&lt; "Error on receive: " &lt;&lt; ec.message() &lt;&lt; "\n";
         }
  
     }
  
   void Shutdown()
    {
      try {
        socket_.shutdown(socket_.shutdown_both);
        socket_.close();
      } catch (std::exception &amp;e)
      {
        std::cout &lt;&lt; "Error Closing Socket" &lt;&lt; e.what() &lt;&lt; std::endl;
      }
    }
  
  void performmaj()
   {
      std::size_t found = messageFromClient_.find('\0');
      if (found!=std::string::npos)
      {
              std::cout &lt;&lt; "Request: "&lt;&lt;i&lt;&lt;" Recieved"&lt;&lt;"\n";
              std::cout &lt;&lt; "Request: "&lt;&lt;i&lt;&lt;" is on process......"&lt;&lt;"\n";
              if (messageFromClient_.size () &gt; 0) messageFromClient_.resize (messageFromClient_.size () - 1);
              messageToClient_=test(messageFromClient_);
              messageFromClient_="";
              messageToClient_.erase(std::remove(messageToClient_.begin(), messageToClient_.end(), '\n'), messageToClient_.end());
  
       }
  
   }
  
     void handle_write(const boost::system::error_code&amp; ec,
       size_t bytes_transferred)
     {
              boost::asio::async_write(socket_,boost::asio::buffer(messageToClient_.data(),bytes_transferred),
             // boost::asio::transfer_at_least(bytes_transferred),
              boost::bind(&amp;tcp_connection::handle_write, shared_from_this(),
              boost::asio::placeholders::error,
              bytes_transferred));
              boost::system::error_code tc;
              socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_send, tc);
  
  
     }
     tcp::socket socket_;
     std::string messageToClient_;
     boost::asio::streambuf input_buffer_;
     std::string messageFromClient_;
     boost::asio::io_service&amp; io;
     boost::asio::deadline_timer timer_,timer2_;
  
   };
  </code></pre>
  
  <p>the above unpredicted behaviour of async_write have caused me to use asio::write.</p>