Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Platform independent Qt5 way to get open TCP port

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.

like image 716
Paul Grinberg Avatar asked Jan 08 '16 14:01

Paul Grinberg


People also ask

How can I tell if a TCP port is open?

Enter "telnet + IP address or hostname + port number" (e.g., telnet www.example.com 1723 or telnet 10.17. xxx. xxx 5000) to run the telnet command in Command Prompt and test the TCP port status. If the port is open, only a cursor will show.

How do I know if port 443 is open?

You can use netstat command to list the tcp port, if 443 port is listed there and state is established means 443 is open for outbound communication.

How do I check if a TCP port is open Linux?

Use ss command to display all open TCP and UDP ports in Linux. Another option is to use the netstat command to list all ports in Linux. Apart from ss / netstat one can use the lsof command to list open files and ports on Linux based system. Finally, one can use nmap command to check TCP and UDP ports too.


1 Answers

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)

  1. if connected, your port is QTcpSocket::localPort() and close this tcp socket
  2. if not connected, your port is random port;
like image 159
0xFFFFFFFF Avatar answered Oct 09 '22 00:10

0xFFFFFFFF