Simple Qt server and open ports.



  • I have created a very basic server that all it does is echo all the messages it receives.

    I originally chose a port for the server and everything seemed fine. Upon running it again though it said that it cannot use that port since it is in use. So I had to change the port my server was to listen to.

    I have to do this every time I re-run my server. Is there any way upon closing my server to also free the port so it can be used again?



  • @ealione said:

    Upon running it again though it said that it cannot use that port since it is in use.

    This means that the listening socket is not closed properly when the application exit.

    I also suggest to use QAbstractSocket::ReuseAddressHint to allow to reuse the port; this is very useful when your server crashes



  • Hi mcosta,
    How is one expected to properly close the socket?
    My server was based on a tutorial by voidRealms on youtube and it was for an earlier version of Qt, so it is possible that things have changed.



  • Hi, can you post the code that manages the socket?



  • This must be the part that you want

    void MyServer::StartServer()
    {
    thisIp = 2346;

    if(!this->listen(QHostAddress::Any, thisIp))
    {
        qDebug() << "Could not start server.";
        qDebug() << errorString();
    }
    else
    {
        qDebug() << "Listening at" << thisIp;
    }
    

    }

    and here is the original. tutorial.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.