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. how can i know that my LAN is working or not
Qt 6.11 is out! See what's new in the release blog

how can i know that my LAN is working or not

Scheduled Pinned Locked Moved Solved General and Desktop
38 Posts 5 Posters 11.9k Views 2 Watching
  • 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.
  • dheerendraD dheerendra

    There is nothing like LAN is working or not. You can check ipaddress exist for the interface or u can try connecting to some website using networkaccessmanager or try connecting to remote server. Based on this u can check the same.

    V Offline
    V Offline
    victor wang
    wrote on last edited by victor wang
    #3

    @dheerendra
    First, i wanna know is my network is working or not.
    Second, I want to know if the address that i connect is valid or not.
    If there are all true for my condition.
    Then i will do other things for next step.

    For now
    I had used this function like below to show my Lan interface

    Process::executeProcessSync(QString("/sbin/ifconfig"),QStringList()<<lanName,message);
    

    But it is not enough to prove that the address i want to connect is valid.
    So i mean i need the solution that to check the address is the valid IP.

    jsulmJ 1 Reply Last reply
    0
    • V victor wang

      @dheerendra
      First, i wanna know is my network is working or not.
      Second, I want to know if the address that i connect is valid or not.
      If there are all true for my condition.
      Then i will do other things for next step.

      For now
      I had used this function like below to show my Lan interface

      Process::executeProcessSync(QString("/sbin/ifconfig"),QStringList()<<lanName,message);
      

      But it is not enough to prove that the address i want to connect is valid.
      So i mean i need the solution that to check the address is the valid IP.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @victor-wang What is a "valid" address/IP? You need to connect to it to be really sure it is valid and you can connect. There is no other way.
      What does "working network" mean? That you are connected to a LAN? That you have access to the Internet?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply
      2
      • jsulmJ jsulm

        @victor-wang What is a "valid" address/IP? You need to connect to it to be really sure it is valid and you can connect. There is no other way.
        What does "working network" mean? That you are connected to a LAN? That you have access to the Internet?

        V Offline
        V Offline
        victor wang
        wrote on last edited by
        #5

        @jsulm
        The meaning of "valid" is when i give this commend "ping 192.168.120.111" in my console, if it is a valid IP address it will return the speed of your internet back to you.
        In the other hand, if it is not a "valid" IP address, it will not return anything.
        The product i developed need to make sure that any possible that client might using invalid address and i have to make an error for them.

        jsulmJ 1 Reply Last reply
        0
        • V victor wang

          @jsulm
          The meaning of "valid" is when i give this commend "ping 192.168.120.111" in my console, if it is a valid IP address it will return the speed of your internet back to you.
          In the other hand, if it is not a "valid" IP address, it will not return anything.
          The product i developed need to make sure that any possible that client might using invalid address and i have to make an error for them.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @victor-wang Then try to connect to the server. If you succeed then everything is fine if not then there is no connection.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          V 1 Reply Last reply
          2
          • jsulmJ jsulm

            @victor-wang Then try to connect to the server. If you succeed then everything is fine if not then there is no connection.

            V Offline
            V Offline
            victor wang
            wrote on last edited by
            #7

            @jsulm
            Do you have any function that you suggest?
            I know to connect but i con't know which function can help me connect and if success will return true for example.

            jsulmJ 1 Reply Last reply
            0
            • V victor wang

              @jsulm
              Do you have any function that you suggest?
              I know to connect but i con't know which function can help me connect and if success will return true for example.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @victor-wang Use QTcpSocket: http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost
              connectToHost will emit connected() signal when connected and error() if not. As alternative you can call http://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected after calling connectToHost, but it will block until connected or timeout:

              socket->connectToHost("imap", 143);
              if (socket->waitForConnected(1000))
                  qDebug("Connected!");
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              V 1 Reply Last reply
              2
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #9

                To read the ipadress you can use qnetwork interface class. Also to check if ip is valid there is direct way. You need to connect to that ip using some port number. U need to find some server running on that port. In summary there is no direct way

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                V 1 Reply Last reply
                3
                • jsulmJ jsulm

                  @victor-wang Use QTcpSocket: http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost
                  connectToHost will emit connected() signal when connected and error() if not. As alternative you can call http://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected after calling connectToHost, but it will block until connected or timeout:

                  socket->connectToHost("imap", 143);
                  if (socket->waitForConnected(1000))
                      qDebug("Connected!");
                  
                  V Offline
                  V Offline
                  victor wang
                  wrote on last edited by
                  #10

                  @jsulm
                  It didn't work.
                  I'm sure that the IP Address is the right one and i've ping the IP Address it's worked.
                  This is my code here
                  It always get in to the qDebug which print "failed to connect host".
                  Did i miss something?

                  the_T jsulmJ 2 Replies Last reply
                  0
                  • dheerendraD dheerendra

                    To read the ipadress you can use qnetwork interface class. Also to check if ip is valid there is direct way. You need to connect to that ip using some port number. U need to find some server running on that port. In summary there is no direct way

                    V Offline
                    V Offline
                    victor wang
                    wrote on last edited by
                    #11

                    @dheerendra
                    Do u have an example for me?

                    1 Reply Last reply
                    0
                    • V victor wang

                      @jsulm
                      It didn't work.
                      I'm sure that the IP Address is the right one and i've ping the IP Address it's worked.
                      This is my code here
                      It always get in to the qDebug which print "failed to connect host".
                      Did i miss something?

                      the_T Offline
                      the_T Offline
                      the_
                      wrote on last edited by
                      #12

                      @victor-wang
                      Is the timeout long enough to get response from the server? If not the connection will always fail

                      -- No support in PM --

                      V 1 Reply Last reply
                      0
                      • V victor wang

                        @jsulm
                        It didn't work.
                        I'm sure that the IP Address is the right one and i've ping the IP Address it's worked.
                        This is my code here
                        It always get in to the qDebug which print "failed to connect host".
                        Did i miss something?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @victor-wang What is the port you're using?
                        Also check what http://doc.qt.io/qt-5/qabstractsocket.html#error is returning.
                        From documentation http://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected:
                        "Note: This function may fail randomly on Windows. Consider using the event loop and the connected() signal if your software will run on Windows."

                        A note: there is no need to allocate socket on the heap, just use a local variable.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        V 1 Reply Last reply
                        2
                        • the_T the_

                          @victor-wang
                          Is the timeout long enough to get response from the server? If not the connection will always fail

                          V Offline
                          V Offline
                          victor wang
                          wrote on last edited by
                          #14

                          @the_
                          I had try to set the time to 5000.
                          But it still didn't work for me.

                          1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @victor-wang What is the port you're using?
                            Also check what http://doc.qt.io/qt-5/qabstractsocket.html#error is returning.
                            From documentation http://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected:
                            "Note: This function may fail randomly on Windows. Consider using the event loop and the connected() signal if your software will run on Windows."

                            A note: there is no need to allocate socket on the heap, just use a local variable.

                            V Offline
                            V Offline
                            victor wang
                            wrote on last edited by
                            #15

                            @jsulm
                            It got some error when i during compiling.
                            It is my code here

                            It is the error here

                            I just type the same thing up to my code.

                            the_T jsulmJ 3 Replies Last reply
                            0
                            • V victor wang

                              @jsulm
                              It got some error when i during compiling.
                              It is my code here

                              It is the error here

                              I just type the same thing up to my code.

                              the_T Offline
                              the_T Offline
                              the_
                              wrote on last edited by
                              #16

                              @victor-wang
                              would be easier if you paste your code here and not on external sites as pictures ;)

                              -- No support in PM --

                              V 1 Reply Last reply
                              0
                              • V victor wang

                                @jsulm
                                It got some error when i during compiling.
                                It is my code here

                                It is the error here

                                I just type the same thing up to my code.

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by jsulm
                                #17

                                @victor-wang Error is not a signal it is just a method, call it if the connection doesn't succeed and print out its output:

                                qDebug() << socket->error();
                                

                                Your code would be wrong even if error() would be a signal: you're trying to connect AFTER the connection failed. That means you would miss the signal.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • V victor wang

                                  @jsulm
                                  It got some error when i during compiling.
                                  It is my code here

                                  It is the error here

                                  I just type the same thing up to my code.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #18

                                  @victor-wang Even better is http://doc.qt.io/qt-5/qiodevice.html#errorString

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  V 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @victor-wang Even better is http://doc.qt.io/qt-5/qiodevice.html#errorString

                                    V Offline
                                    V Offline
                                    victor wang
                                    wrote on last edited by victor wang
                                    #19

                                    @jsulm
                                    You mean change like this?
                                    If not where i should add this on?

                                    I had tried it. and will show the error
                                    "QAbstractSocket::SocketTimeoutError"

                                    And if i use

                                    qDebug <<socket->errorString();
                                    

                                    It will show this error "Socket operation timed out"

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • V victor wang

                                      @jsulm
                                      You mean change like this?
                                      If not where i should add this on?

                                      I had tried it. and will show the error
                                      "QAbstractSocket::SocketTimeoutError"

                                      And if i use

                                      qDebug <<socket->errorString();
                                      

                                      It will show this error "Socket operation timed out"

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #20

                                      @victor-wang Yes, like this. Even better is http://doc.qt.io/qt-5/qiodevice.html#errorString

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      V 1 Reply Last reply
                                      0
                                      • jsulmJ jsulm

                                        @victor-wang Yes, like this. Even better is http://doc.qt.io/qt-5/qiodevice.html#errorString

                                        V Offline
                                        V Offline
                                        victor wang
                                        wrote on last edited by victor wang
                                        #21

                                        @jsulm
                                        I'm using port 8888.

                                        I had tried it. and will show the error
                                        "QAbstractSocket::SocketTimeoutError"

                                        And i if i use

                                        qDebug<<socket->errorString();
                                        

                                        It will show this error "Socket operation timed out"

                                        So is that mean i had set the time too long?

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • the_T the_

                                          @victor-wang
                                          would be easier if you paste your code here and not on external sites as pictures ;)

                                          V Offline
                                          V Offline
                                          victor wang
                                          wrote on last edited by
                                          #22

                                          @the_
                                          My code is too long.
                                          I'm using qtcreator via Xming.
                                          So i can't copy my code, i just can type it.
                                          And because of there are too many codes i have no time to type it.
                                          That's why i using as picture.

                                          1 Reply Last reply
                                          0

                                          • Login

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