My computer's IP on the local network is 192.168.0.100
, I start my QTcpServer with
if (!tcpServer->listen(QHostAddress::LocalHost, 1234)) {
When I try to connect to it with netcat 192.168.0.100 1234
, the connection is refused, but netcat localhost 1234
succeeds.
At the same time, if I listen with netcat -l -p 1234
, I can connect on both 192.168.0.100
and localhost
without any problem.
This has me scratching my head, why is it happening?
In order to accept connections from the outside, you have to listen on 0.0.0.0
, not on 127.0.0.1
or localhost
. The latter will only allow connections coming from the same machine. It's also the value of QHostAddress::LocalHost
.
So change the first argument to QHostAddress::Any
and it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With