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.
  • 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
                            • V victor wang

                              @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 Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #23

                              @victor-wang said in how can i know that my LAN is working or not:

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

                              No, it means that it was not possible to connect in that time.
                              You're doing it wrong. Please read documentation (http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost): hostName is a URL not IP.
                              If you want to use IP then use http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost-1

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

                              V 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @victor-wang said in how can i know that my LAN is working or not:

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

                                No, it means that it was not possible to connect in that time.
                                You're doing it wrong. Please read documentation (http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost): hostName is a URL not IP.
                                If you want to use IP then use http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost-1

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

                                @jsulm
                                Ok, i see.
                                Now i tried to connect to the IP 192.168.120.115 like here

                                And i print the error out shows "Connection refused"
                                Is that because this IP address need the password to enter it?

                                If it is, what should i do to solve this problem?

                                jsulmJ 1 Reply Last reply
                                0
                                • V victor wang

                                  @jsulm
                                  Ok, i see.
                                  Now i tried to connect to the IP 192.168.120.115 like here

                                  And i print the error out shows "Connection refused"
                                  Is that because this IP address need the password to enter it?

                                  If it is, what should i do to solve this problem?

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

                                  @victor-wang You're still doing it wrong. Please read documentation - I posted the links.
                                  If you want to use IP address you need to pass http://doc.qt.io/qt-5/qhostaddress.html as parameter not a string.

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

                                  V 1 Reply Last reply
                                  1
                                  • jsulmJ jsulm

                                    @victor-wang You're still doing it wrong. Please read documentation - I posted the links.
                                    If you want to use IP address you need to pass http://doc.qt.io/qt-5/qhostaddress.html as parameter not a string.

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

                                    @jsulm
                                    Is that mean i have to set address first like this?

                                    hostaddress->setAddress(192.168.120.115);
                                    

                                    Then connect it like this?

                                    socket->connectToHost(hostaddress,PT_lanHostPort);
                                    

                                    Is that right?

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • V victor wang

                                      @jsulm
                                      Is that mean i have to set address first like this?

                                      hostaddress->setAddress(192.168.120.115);
                                      

                                      Then connect it like this?

                                      socket->connectToHost(hostaddress,PT_lanHostPort);
                                      

                                      Is that right?

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

                                      @victor-wang Yes

                                      socket->connectToHost(QHostAddress("192.168.120.115"), PT_lanHostPort);
                                      

                                      Why do you allocate everything on the heap with new? hostaddress can be just a local variable:

                                      QHostAddress hostaddress("192.168.120.115");
                                      socket.connectToHost(hostaddress, PT_lanHostPort); // No need to allocate socket on the heap with new as well!
                                      

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

                                      V 1 Reply Last reply
                                      1
                                      • jsulmJ jsulm

                                        @victor-wang Yes

                                        socket->connectToHost(QHostAddress("192.168.120.115"), PT_lanHostPort);
                                        

                                        Why do you allocate everything on the heap with new? hostaddress can be just a local variable:

                                        QHostAddress hostaddress("192.168.120.115");
                                        socket.connectToHost(hostaddress, PT_lanHostPort); // No need to allocate socket on the heap with new as well!
                                        
                                        V Offline
                                        V Offline
                                        victor wang
                                        wrote on last edited by victor wang
                                        #28

                                        @jsulm
                                        Sorry, i'm new in Qt.Please be patient to me :)
                                        This is my code here

                                        It still print "connection refused".

                                        Why will it refused to connect?

                                        "192.168.120.115" this is an IP which is a server and i need password and user to login.
                                        Is this a factor that cause the error?

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • V victor wang

                                          @jsulm
                                          Sorry, i'm new in Qt.Please be patient to me :)
                                          This is my code here

                                          It still print "connection refused".

                                          Why will it refused to connect?

                                          "192.168.120.115" this is an IP which is a server and i need password and user to login.
                                          Is this a factor that cause the error?

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

                                          @victor-wang It is not a Qt thing, it is C++.
                                          It should actually work even if authentication is required. Is port number correct? Can you ping this IP?

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

                                          V 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