Why am I getting a public IP on mobile and local IP on desktop?



  • I have this code here that sends a single character to the first CompRoomsBlinds it finds on the network on port 12000:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(shadeToggleClicked()));
        tcpSocket = new QTcpSocket(this);
        QHostInfo::lookupHost("CompRoomBlinds", this, SLOT(hostInfoResults(QHostInfo)));
    }
    
    void MainWindow::shadeToggleClicked()
    {
        tcpSocket->connectToHost(address, 12000);
        char test = 'R';
        tcpSocket->write(&test, 1);
        tcpSocket->disconnectFromHost();
    }
    
    void MainWindow::hostInfoResults(QHostInfo info)
    {
        address = info.addresses().first();
    }
    

    When I run this code on my desktop the address is the local ip of the device I want to connect to. When I run it on mobile I get the public ip? Everything is on the same network. I guess the only difference would be that my desktop is plugged in via ethernet and my phone over wifi. So why are there different results?
    The server only operates over LAN so I need the local ip for it to work.

    Also note I verified the local ip works from my phone by putting it in as a literal string but the local ip could change so I need to find it by name.



  • I expect hostInfoResults is returning a public interface. Probably just luck PC works, maybe it lists local interfaces first?

    These might help:
    http://doc.qt.io/qt-5/qhostaddress.html#SpecialAddress-enum
    http://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.