Network Status.



  • 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.


  • Moderators

    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.



  • 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.



  • 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".


  • Moderators

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


Log in to reply
 

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