Results 1 to 10 of 10

Thread: Status update on Network Disconnected

  1. #1
    Join Date
    Oct 2009
    Posts
    66
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Status update on Network Disconnected

    Hi,
    I am using Qt 4.6, and i want to know when the network is disconnected, how can i achieve the same without polling on some thread.

    Thanks in advance.

    regards,
    sudhish

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Status update on Network Disconnected

    What do you mean?

    You want to know when network lead is unplugged ?

    You want to know when you can no longer access some host ?

    You want to know when a mobile device turns off it's data access (Wifi, 3G, ...) ?

  3. #3
    Join Date
    Oct 2009
    Posts
    66
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Status update on Network Disconnected

    What i want to know is when Network cable is unplugged.

    I have implemented this with polling mechanism i want to know if the same can be achieved with the event's and not on polling?

    Regards
    Sudhish Kapoor

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Status update on Network Disconnected

    http://doc.qt.nokia.com/4.7/qnetworkinterface.html

    Check the status "isUp" or "isRunning" of the interface you want to use.

  5. #5
    Join Date
    Oct 2009
    Posts
    66
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Status update on Network Disconnected

    I am working on QT4.6, where i am using
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    state to detect when the network cable is unplugged, but with this approach, i have to use continuous polling to scan the status of the network and then update the UI is there a better way than polling on network interface to find the status network changed??

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Status update on Network Disconnected

    Depending on the operating system, I don't know.
    But my first guess is no, there's no notification when the network cable is unplugged, you need to check that yourself.

    Maybe on linux using hal (or its replacement)/dbus/... there might be a way to get a notification.
    On Windows, check MSDN for System Notification Services.

    You can also try to play with the keep alive option of the socket.

    A socket is closed when you explicitly close it. Unplugging the network cable unfortunatly doesn't close the connection, it will just time out. By setting the keep alive value very low, you can minimize the time out. However, this might come with more problems than solutions.

    Constantly checkking the status of the network isn't a problem though. It is a correct thing to do. Certainly when you know that the OS, other libraries, ... will most likely do the same but make you add more complexity to your program.
    What are your biggest problems with this technique?
    Last edited by tbscope; 12th January 2011 at 06:07.

  7. #7
    Join Date
    Oct 2009
    Posts
    66
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Status update on Network Disconnected

    Quote Originally Posted by tbscope View Post
    Depending on the operating system, I don't know.
    But my first guess is no, there's no notification when the network cable is unplugged, you need to check that yourself.

    Maybe on linux using hal (or its replacement)/dbus/... there might be a way to get a notification.
    On Windows, check MSDN for System Notification Services.

    You can also try to play with the keep alive option of the socket.

    A socket is closed when you explicitly close it. Unplugging the network cable unfortunatly doesn't close the connection, it will just time out. By setting the keep alive value very low, you can minimize the time out. However, this might come with more problems than solutions.
    "][/HIGHLIGHT]

    Constantly checkking the status of the network isn't a problem though. It is a correct thing to do. Certainly when you know that the OS, other libraries, ... will most likely do the same but make you add more complexity to your program.
    What are your biggest problems with this technique?
    I am working on Linux platform, adding a thread for polling, is eating up CPU for the UI thread, hence i want a parallel approach which is cleaner than polling from UI.

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Status update on Network Disconnected

    Hmm, a timer that checks the network connection every second or so will not consume noticable cpu power.
    You don't even need a thread if you don't block the event loop.

  9. #9
    Join Date
    Oct 2009
    Posts
    66
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Status update on Network Disconnected

    Thats what i did in the new thread where in
    Qt Code:
    1. QFuture<void> future = QtConcurrent::run(this,&MyClass::scanNetwork);
    2.  
    3. void MyClass::scanNetwork()
    4. {
    5. QThread::currentThread()->setPriority(QThread::LowestPriority);
    6. while(1)
    7. {
    8. QNetworkInterface iface (QNetworkInterface::interfaceFromName("eth0"));
    9. if(iface.isValid())
    10. {
    11. if ( iface.flags().testFlag(QNetworkInterface::IsRunning))
    12. {
    13. currentState = Running;
    14. }
    15. else
    16. {
    17. currentState = Stopped;
    18. }
    19. }
    20. if(previousState!=currentState)
    21. {
    22. previousState=currentState;
    23. emit (myClass->NetworkCallbackSignal(currentState));
    24. }
    25. wait(5000);
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 
    I am waiting inside the thread with the low priority for 5 sec then polling again still i have the problem

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Status update on Network Disconnected

    Get rid of the thread.

    Create a class. Use a QTimer. When the timer fires, check the status of the network. If it is different than last time, fire a signal which you app can detect via a slot.

Similar Threads

  1. Replies: 6
    Last Post: 24th February 2012, 11:17
  2. Replies: 2
    Last Post: 16th July 2010, 19:14
  3. Local Status Bar Does Not Update
    By shawno in forum Qt Programming
    Replies: 0
    Last Post: 16th July 2010, 01:34
  4. Replies: 3
    Last Post: 8th July 2010, 07:41
  5. I want t update my status bar froe webview!..
    By thewooferbbk in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2009, 13:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.