Use QT Qtcpsocket to detect the network status of your device:
The function returns TRUE, device network status is normal, return false, device network exception.
BOOL Tcpipsocket::sendatcpsockettoip (QString IP) {m_ipadress = IP; Abort (); Connecttohost (m_ipadress, Port); Wait timeout time, if still not pass, then exception return waitforconnected (timeout);}
To achieve real-time detection, use while:
runWhile = true; runBreak = false; bool retping; while (Runwhile) { retping = m_senderagent.sendatcpsockettoip (ipadress); if (retping == true) { //ping successful, target IP network normal, send commandsuccessed emit commandsuccessed (); }else{ //ping failed, the target IP network is not normal, Send commandfailed emit commandfailed (); } sleep (1000 );     // SLEEP&NBSp;1s if (Runbreak) { break; } } End of while
The problem with running is that if you use a different function to trigger at some point, make runbreak = true, or Runwhile = False, but the while is still looping and does not jump out, but when the comment falls off sleep (1000), While it jumps out. But after commenting out sleep (1000), the while consumes a lot of CPU resources to send the packets (this is not what you want, only two of them are lighter).
This article is from the "Sun really Round" blog, make sure to keep this source http://xuedengfeng.blog.51cto.com/7132939/1870947
QT uses Qtcpsocket to detect the network status of IP devices