Results 1 to 10 of 10

Thread: Status update on Network Disconnected

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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

  5. #5
    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
  •  
Qt is a trademark of The Qt Company.