質問

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.

他のヒント

localhost is on a separate network interface

you can use QHostAddress::Any to listen for external connections

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top