Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

localhost is on a separate network interface

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top