Get the bound port number when QTcpSocket in Qt is used as a client

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::connectedsignal. 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

Guess you like

Origin blog.csdn.net/weixin_39308337/article/details/106652493