Simple Qt server and open ports.
-
wrote on 10 Mar 2015, 07:52 last edited by
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?
-
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?
wrote on 10 Mar 2015, 07:55 last edited by@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 -
wrote on 10 Mar 2015, 07:58 last edited by
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. -
wrote on 10 Mar 2015, 08:00 last edited by
Hi, can you post the code that manages the socket?
-
wrote on 10 Mar 2015, 08:07 last edited by
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.
2/5