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
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
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, ...) ?
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
http://doc.qt.nokia.com/4.7/qnetworkinterface.html
Check the status "isUp" or "isRunning" of the interface you want to use.
I am working on QT4.6, where i am usingstate 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??Qt Code:
To copy to clipboard, switch view to plain text mode
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.
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.
Thats what i did in the new thread where in
I am waiting inside the thread with the low priority for 5 sec then polling again still i have the problemQt Code:
QFuture<void> future = QtConcurrent::run(this,&MyClass::scanNetwork); void MyClass::scanNetwork() { while(1) { if(iface.isValid()) { { currentState = Running; } else { currentState = Stopped; } } if(previousState!=currentState) { previousState=currentState; emit (myClass->NetworkCallbackSignal(currentState)); } wait(5000); } }To copy to clipboard, switch view to plain text mode![]()
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.
Bookmarks