title: Poco c++ Websocket server connection reset by peer
body: <p>I am writing a kind of chat server app where a message received from one websocket client is sent out to all other websocket clients. To do this, I keep the connected clients in a list. When a client disconnects, I need to remove it from the list (so that future "sends" do not fail).</p>
  
  <p>However, sometimes when a client disconnects, the server just gets an exception "connection reset by peer", and the code does not get chance to remove from the client list. Is there a way to guarantee a "nice" notification that the connection has been reset?</p>
  
  <p>My code is:</p>
  
  <pre><code>void WsRequestHandler::handleRequest(HTTPServerRequest &amp;req, HTTPServerResponse &amp;resp)
  {
      int             n;
      Poco::Timespan  timeOut(5,0);
  
      try
      {
          req.set("Connection","Upgrade"); // knock out any extra tokens firefox may send such as "keep-alive"
          ws = new WebSocket(req, resp);
          ws-&gt;setKeepAlive(false);
          connectedSockets-&gt;push_back(this);
  
          do
          {
              flags = 0;
              if (!ws-&gt;poll(timeOut,Poco::Net::Socket::SELECT_READ || Poco::Net::Socket::SELECT_ERROR))
              {
  //                  cout &lt;&lt; ".";
              }
              else
              {
                  n = ws-&gt;receiveFrame(buffer, sizeof(buffer), flags);
                  if (n &gt; 0)
                  {
                      if ((flags &amp; WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_BINARY)
                      {
                          // process and send out to all other clients
                          DoReceived(ws, buffer, n);
                      }
                  }
              }
          }
          while ((flags &amp; WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
          // client has closed, so remove from list
          for (vector&lt;WsRequestHandler *&gt;::iterator it = connectedSockets-&gt;begin() ; it != connectedSockets-&gt;end(); ++it)
          {
              if (*it == this)
              {
                  connectedSockets-&gt;erase(it);
                  logger-&gt;information("Connection closed %s", ws-&gt;peerAddress().toString());
                  break;
              }
          }
          delete(ws);
          ws = NULL;
      }
      catch (WebSocketException&amp; exc)
      {
      //never gets called
      }
  
  }
  </code></pre>