Platform Independent Qt5 Method for Opening an Open TCP Port

Is there a platform-independent way in Qt to get an unused TCP port? I need to run an existing application that needs to be given an open TCP port for it to work.

+4
source share
2 answers

using QTcpServer is easier.

bool QTcpServer::listen(const QHostAddress & address = QHostAddress::Any, quint16 port = 0)

If port0, a portis automatically selected, then you use quint16 QTcpServer::serverPort() constto get an "unoccupied" port

then close your tcp server

OR

create ramdom port, use QTcpSocketto connect it (local connection)

  • , QTcpSocket::localPort() tcp
  • , ;
+5

- tcp-? QTcpServer .

, QProcess. :

QString program = "path/to/server";
QStringList arguments;
arguments << "-p" << "1234"; //or what ever you want

QProcess *myProcess = new QProcess(parent);
myProcess->start(program, arguments);
0

Source: https://habr.com/ru/post/1623428/


All Articles