In the socket, the client socket does not have to call the bind function to specify the port number. But after the connection is successful, an available port number will be automatically assigned to the client socket.
Solution
After QTcpSocket calls the connectToHost function to successfully connect to the server. It will trigger a QTcpSocket::connected
signal. Write related slot functions. Call the
localPort function in the slot function to get the port number automatically bound by the client.
As a server
As the server specifies the binding port by itself, there is no need to obtain it