Reputation: 1407
Is there a platform independent way in Qt to get an unused TCP port? I have a need to launch an existing application which must be given an open TCP port in order for it to work.
Upvotes: 6
Views: 2405
Reputation: 852
use QTcpServer is easier way.
bool QTcpServer::listen(const QHostAddress & address = QHostAddress::Any, quint16 port = 0)
If port
is 0, a port
is chosen automatically, then you use quint16 QTcpServer::serverPort() const
to get the "idle" port
then close your Tcp Server
OR
generate a ramdom port, use QTcpSocket
to connect it(local connection)
QTcpSocket::localPort()
and close this tcp socketUpvotes: 7
Reputation: 4010
Do you mean some kind of tcp server? Then there is QTcpServer class.
If you want to start an existiong server, then you need QProcess class. Example:
QString program = "path/to/server";
QStringList arguments;
arguments << "-p" << "1234"; //or what ever you want
QProcess *myProcess = new QProcess(parent);
myProcess->start(program, arguments);
Upvotes: 0