sashoalm
sashoalm

Reputation: 79685

Detect that a server is running locally by its IP address

My program uses Bonjour to get a list of servers running on various IP addresses on the local network, but one or more of them might be running on the same machine.

I need to know if a server is running on the same machine, by checking its IP address.

For example, servers on 127.0.0.1, 192.168.0.100, and 192.168.56.1 are all running on my local machine, but a server on 192.168.0.104 or on 192.168.56.2 would be running on another machine.

Upvotes: 0

Views: 450

Answers (1)

TheDarkKnight
TheDarkKnight

Reputation: 27629

The QNeworkInterfaces class has a static function that you can call: -

QList<QHostAddress> addressList = QNetworkInterfaces::allAddresses();

You can then iterate through the addressList and compare those with the server addresses: -

bool IsLocalServer()
{
    QList<QHostAddress> addressList = QNetworkInterfaces::allAddresses();

    foreach(QHostAddress address, addressList)
    {
        if(address == QHostAddress("192.168.0.100")
            return true;
        else if(address == QHostAddress("192.168.56.1")
            return true;
    }
    return false;
}

Upvotes: 2

Related Questions