How to find other machine's IP Address in Qt ??



  • Hi All,

    I just want to know how to find ip Adrress of another machine programmatically in Qt.

    Any help will be appreciated.
    Thanx in Advance.



  • Hi,
    are you working with sockets? You have to catch the signal newConnection() for incoming connections and get the new QTcpSocket (nextPendingConnection()).
    With the QTcpSocket you can get the adress via peerAddress()...



  • Thank you @Binary91 for your response.

    I tried using sockets. but it takes sometime to find out all the address in a network.

    Then I combined sockets with multithreading to get the ip adress quickly. but In case if there are thousands of system , this procedure will take a lot of CPU usage and RAM. So I want to minimize it also.

    So Is there any other method to achieve this ?



  • check this QNetworkInterface



  • I have used QNetworkInterface class but it is not very helpful as i needed to ipaddress of other machine or say (ip address of) all machines.



  • you can try:

    @// Send a broadcast packet on all interfaces
    void DuktoProtocol::sendToAllBroadcast(QByteArray *packet)
    {
    // Get network interfaces list
    QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();

    // Interfaces iteration
    for (int i = 0; i < ifaces.size(); i++)
    {
        // Now get all IP addresses for the current interface
        QList<QNetworkAddressEntry> addrs = ifaces[i].addressEntries();
    
        // And for any IP address, if it is IPv4 and the interface is active, send the packet
        for (int j = 0; j < addrs.size(); j++)
            if ((addrs[j].ip().protocol() == QAbstractSocket::IPv4Protocol) && (addrs[j].broadcast().toString() != ""))
                mSocket->writeDatagram(packet->data(), packet->length(), addrs[j].broadcast(), UDP_PORT);
    }
    

    }@



  • Thank u @Salvatello for posting the code.
    I tried it but unfortunately it is not providing required output.

    This code is providing all kind of local Address of hosting machine.



  • Hi andreyc!! Thank u for your reply.

    As your suggestion, I am searching for mayne library on Windows. Will you please provide me the link for mayne libary.

    Thank you.


Log in to reply
 

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