QTcpServer can only be accessed through localhost

My computer is IP on the local network 192.168.0.100 , I am running my QTcpServer using

 if (!tcpServer->listen(QHostAddress::LocalHost, 1234)) { 

When I try to connect to it using netcat 192.168.0.100 1234 , the connection is rejected, but netcat localhost 1234 succeeds.

At the same time, if I listen using netcat -l -p 1234 , I can connect to both 192.168.0.100 and localhost without any problems.

It makes me scratch my head, why is this happening?

+6
source share
2 answers

To accept connections from outside, you must listen on 0.0.0.0 , not on 127.0.0.1 or localhost . The latter will only allow connections coming from one machine. This is also the value of QHostAddress::LocalHost .

So, change the first argument to QHostAddress::Any , and it should work.

+6
source

localhost is on a separate network interface

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

+3
source

Source: https://habr.com/ru/post/957356/


All Articles