Search code examples
c++websocketserverportwebsocket++

WebSocket++ server ending, but port says still in use


Currently, I'm ending my server by closing (or refreshing) a client browser tab. When the WebSocket close function is called on the server, the server stops listening, closes all connections, etc. and exits. Here is the code:

void onClose(server* s, websocketpp::connection_hdl hdl) {
    bool running = false;
    s->stop_listening ();
    for (vector<Player *>::iterator it = players.begin(); it != players.end(); ++it) {
        Player *p = *it;
        s->pause_reading(p->io);
        s->close(p->io, websocketpp::close::status::normal, "");
    }
    s->stop ();
    exit (0);
}

In the snippet above, I'm looping through all the "players" (connected clients). The use of p->io refers to a websocketpp::connection_hdl.

However, when I run the server again, I get this error:

[info] asio listen error: system:98 (Address already in use)

I have to wait a few minutes before I can use the server again. This is both annoying, and potentially dangerous in production.

Any ideas what could be going on?


Solution

  • Do as David said so you know what you are doing. Then use the websocketspp function s->set_reuse_addr(true)