Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Search IP address in DHCP local network
QtWS25 Last Chance

Search IP address in DHCP local network

Scheduled Pinned Locked Moved Unsolved General and Desktop
qhostinfohost namedhcp
2 Posts 2 Posters 918 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Davide87
    wrote on 15 Mar 2019, 11:10 last edited by Davide87
    #1

    Hello everybody

    I need to connect via QTcpSocket to a server in my local network. Such server has a dynamic IP assigned by the DHCP. Therefore, I don't know the IP address, but I know its hostname.

    However, if i use the example code in the QHostInfo Class documentation as follows

    QHostInfo::lookupHost("myServerHostName", this, SLOT(lookedUp(QHostInfo)));
    
    void MainWindow::lookedUp(const QHostInfo &host)
    {
        if (host.error() != QHostInfo::NoError) {
                qDebug() << "Lookup failed:" << host.errorString();
                return;
            }
    
            qDebug() << "Found HOST: " << host.hostName();
            const auto addresses = host.addresses();
            for (const QHostAddress &address : addresses)
                qDebug() << "Found address:" << address.toString();
    }
    

    it will return Lookup failed: "Host not found"
    If instead I use

    QHostInfo::lookupHost("192.168.etc.", this, SLOT(lookedUp(QHostInfo)));
    

    It will successfully find the server I need to connect to, but this can't be my solution since in the final release of my application I won't be able to know such IP address a priori.

    One possible solution would be to use a brute force approach for every possible IP address, but it would be way too slow.
    Another solution could be to try to get all IP addresses on the LAN (maybe by interrogating the DHCP server). Then I would brute force on a much smaller number of addresses and it would be fast enough, but I don't know hot to do it and HERE they say it is not a reliable way and do not give a solution to the question.
    I could try to ping a broadcast and try to use QHostInfo::lookupHost on everyone that answers to my ping, but I do not know how to do it either.

    Can anyone help me to solve this problem?

    ps: I needs to work regardless the operative system (windows/linux/macos).

    P 1 Reply Last reply 15 Mar 2019, 12:18
    0
    • D Davide87
      15 Mar 2019, 11:10

      Hello everybody

      I need to connect via QTcpSocket to a server in my local network. Such server has a dynamic IP assigned by the DHCP. Therefore, I don't know the IP address, but I know its hostname.

      However, if i use the example code in the QHostInfo Class documentation as follows

      QHostInfo::lookupHost("myServerHostName", this, SLOT(lookedUp(QHostInfo)));
      
      void MainWindow::lookedUp(const QHostInfo &host)
      {
          if (host.error() != QHostInfo::NoError) {
                  qDebug() << "Lookup failed:" << host.errorString();
                  return;
              }
      
              qDebug() << "Found HOST: " << host.hostName();
              const auto addresses = host.addresses();
              for (const QHostAddress &address : addresses)
                  qDebug() << "Found address:" << address.toString();
      }
      

      it will return Lookup failed: "Host not found"
      If instead I use

      QHostInfo::lookupHost("192.168.etc.", this, SLOT(lookedUp(QHostInfo)));
      

      It will successfully find the server I need to connect to, but this can't be my solution since in the final release of my application I won't be able to know such IP address a priori.

      One possible solution would be to use a brute force approach for every possible IP address, but it would be way too slow.
      Another solution could be to try to get all IP addresses on the LAN (maybe by interrogating the DHCP server). Then I would brute force on a much smaller number of addresses and it would be fast enough, but I don't know hot to do it and HERE they say it is not a reliable way and do not give a solution to the question.
      I could try to ping a broadcast and try to use QHostInfo::lookupHost on everyone that answers to my ping, but I do not know how to do it either.

      Can anyone help me to solve this problem?

      ps: I needs to work regardless the operative system (windows/linux/macos).

      P Offline
      P Offline
      Pablo J. Rogina
      wrote on 15 Mar 2019, 12:18 last edited by Pablo J. Rogina
      #2

      @Davide87 said in Search IP address in DHCP local network:

      Such server has a dynamic IP assigned by the DHCP. Therefore, I don't know the IP address, but I know its hostname.

      It looks like your problem is about networking, not Qt-related.

      You need to ensure your network provides a good way for name resolution (i.e. DNS service). If your server is assigned a dynamic IP via DHCP that a DNS entry for such server name is keep up to date. This post could be a potential guide just in case.

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      4

      2/2

      15 Mar 2019, 12:18

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved