QAbstractSocket::localPort() is different from QAbstractSocket::peerPort() in a connection?

  • I setup a QSslSocket between a client and a server, using a port number. At the server side when check I found the localPort() and peerPort() are different. The localPort() match to the port number I used and the peerPort() show some unknown number. How can that be?

  • This is because the peer dynamically allocates a high port for their connection attempt.

    As you know, ports below 1024 are reserved ports that can only be used by an administrative user (root / Administrator). Imagine your server was running on one of the reserved ports, and an unprivileged user wanted to connect to it. If the same port was used on both the server and the client side, then the unprivileged user would not be able to connect, because he couldn't open the port.

    Furthermore, imagine the user was using a browser on a server that also runs an http and a https server. In this instance, the user would have to disable the http and https servers if they wanted to browse the internet, which would be very impractical.

    Hence the ports are different.

  • I see. Thanks a lot!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.