title: How to run boost client websocket when the client is disconnected in C++?
body: <p>I am writing a web socket for sending and receiving data to the server and below is the code, I want this code to run automatically if the server stops and starts again or if the client-side has internet issues then it should re-establish the connection on its own if the connection closed abruptly due to any issue from the server and client-side. Also, this code always goes into the catch block since the closing gives the error <code> End of File</code>, I am a noob techie and so couldn't understand the previously answered questions.</p>
  <pre><code>
  #include &lt;boost/beast/core.hpp&gt;
  #include &lt;boost/beast/websocket.hpp&gt;
  #include &lt;boost/asio/connect.hpp&gt;
  #include &lt;boost/asio/ip/tcp.hpp&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  namespace beast = boost::beast;         // from &lt;boost/beast.hpp&gt;
  namespace http = beast::http;           // from &lt;boost/beast/http.hpp&gt;
  namespace websocket = beast::websocket; // from &lt;boost/beast/websocket.hpp&gt;
  namespace net = boost::asio;            // from &lt;boost/asio.hpp&gt;
  using tcp = boost::asio::ip::tcp;       // from &lt;boost/asio/ip/tcp.hpp&gt;
  
  // Sends a WebSocket message and prints the response
  
  
  
  
  int main(int argc, char** argv)
  {
      try
      {
          // Check command line arguments.
          if(argc != 4)
          {
              std::cerr &lt;&lt;
                  &quot;Usage: websocket &lt;host&gt; &lt;port&gt; &lt;text&gt;\n&quot; &lt;&lt;
                  &quot;Example:\n&quot; &lt;&lt;
                  &quot;    websocket echo.websocket.org 80 \&quot;Hello, world!\&quot;\n&quot;;
              return EXIT_FAILURE;
          }
          auto const host = argv[1];
          auto const port = argv[2];
          auto const text = argv[3];
  
          // The io_context is required for all I/O
          net::io_context ioc;
  
          // These objects perform our I/O
          tcp::resolver resolver{ioc};
          websocket::stream&lt;tcp::socket&gt; ws{ioc};
          // Look up the domain name
          auto const results = resolver.resolve(host, port,boost::asio::ip::resolver_query_base::numeric_service);
  
          // Make the connection on the IP address we get from a lookup
          boost::system::error_code ec;
          net::connect(ws.next_layer(), results.begin(), results.end(),ec);
          // Set a decorator to change the User-Agent of the handshake
          ws.set_option(websocket::stream_base::decorator(
              [](websocket::request_type&amp; req)
              {
                  req.set(http::field::user_agent,
                      std::string(&quot;agent&quot;));
              }));
  
          // Perform the websocket handshake
          ws.handshake(host, &quot;/&quot;);
          
          // Send the message
          ws.write(net::buffer(std::string(text)));
          // This buffer will hold the incoming message
          beast::multi_buffer buffer;
          
          // Read a message into our buffer
          ws.read(buffer);
          // The make_printable() function helps print a ConstBufferSequence
          std::cout &lt;&lt; beast::make_printable(buffer.data()) &lt;&lt; std::endl;
          // If we get here then the connection is closed gracefully
          // Close the WebSocket connection
          ws.close(websocket::close_code::normal);
      }
      catch(std::exception const&amp; e)
      {
          std::cerr &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what() &lt;&lt; std::endl;
      }
      return EXIT_SUCCESS;
  }
  </code></pre>