Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTcpServer can only be accessed through localhost

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?

like image 628
sashoalm Avatar asked Nov 04 '13 16:11

sashoalm


1 Answers

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.

like image 69
lethal-guitar Avatar answered Sep 21 '22 05:09

lethal-guitar