QT uses Qtcpsocket to detect the network status of IP devices

Source: Internet
Author: User
Tags emit

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.