Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Network Status.

    General and Desktop
    4
    5
    1888
    Loading More Posts
    • 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.
    • M
      MyQT last edited by

      Hi,

      I am using below code to check whether the network of my machine is up or not.
      If network is up and running then I am trying to connect the TCP socket.

      @
      QList<QNetworkInterface> tempList = ni.allInterfaces();
      int status = ((tempList[0].flags()) & QNetworkInterface::IsUp);
      if(status)
      {
      tcp_.connectToHost(QHostAddress(gpsRecIPAddr_), portNumber_);
      }
      @

      Above code was wooking fine.
      I was getting below newtwork status,
      QFlags(0x1|0x2|0x4|0x20)

      But when we connected wirless network(Netgear) to my PC then bove code started giving problem.

      In above code, "status" variable did not turn true. So TCP connection was failed.
      Also i was getting below network flags,
      QFlags(0x4|0x20) -- (Here network flags status which is output of allInterfaces function, is also changed as compered previous one)

      Just wanted to know why TCP connecion was not happning through above code?. I removed netgear wireless connection and its divers but getting same issue TCP connection failure.

      Any help will be helpful.

      1 Reply Last reply Reply Quote 0
      • T
        tobias.hunger last edited by

        That code is a bit stupid...

        First you grab a list of interfaces and then check that the first one is up and ignore whether any other is up. So if the first one is down (e.g. your built-in ethernet without a wire in it) but there are 20 others that are up (e.g. a wireless card), then you do not even try to connect.

        If there is no network interface at all, then you crash.

        If there is no interface up the connectToHost should fail instantly, so you do not gain anything by the IsUp test.

        1 Reply Last reply Reply Quote 0
        • M
          MyQT last edited by

          Hi, Thanks for the reply.

          Here my pupose is, I want to check that LAN cable is connected propely to my PC first then only trying to connect the TCP.
          Is there any other better way to check that?

          Thansk in advance.

          1 Reply Last reply Reply Quote 0
          • M
            MuldeR last edited by

            Tobias Hunger said, after you got the list of Network Interfaces, don't blindly access the first one (index #0), as the list might be empty and you could easily get a crash there! So first step would be to check if the number of network interfaces is greater than zero. If not, then you don't have a network interface and thus you obviously don't have a connection. If there is a network interface in the list (maybe several!), don't just check the first one. Instead, check all of them in a loop. What you want probably is at least one interface that "is up".

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply Reply Quote 0
            • M
              mlong last edited by

              Do you really have a guarantee that @tempList[0]@ is really your physical ethernet port?

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post