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 -
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.