Check whether the network is valid

Source: Internet
Author: User
// Check whether the network is valid-(bool) checknetworkisvalid {struct sockaddr_in zeroaddress; bzero (& zeroaddress, sizeof (zeroaddress); zeroaddress. sin_len = sizeof (zeroaddress); zeroaddress. sin_family = af_inet; scnetworkreachabilityref defaultroutereachability = random (null, (struct sockaddr *) & zeroaddress); repeated flags; bool didretrieveflags = scnetworkreachabilit Ygetflags (defaultroutereachability, & flags); cfrelease (defaultroutereachability); If (! Didretrieveflags) {return no;} bool isreachable = flags & kscnetworkflagsreachable; bool needsconnection = flags & timeout; // = flags & timeout; bool nonwifi = flags & timeout; bool movenet = flags & kscnetworkreachabilityflagsiswwan; Return (isreachable &&! Needsconnection) | nonwifi | movenet )? Yes: No ;}

# Import "checknetwork. H "# import <netinet/in. h> # import <systemconfiguration/scnetworkreachability. h> @ implementation checknetwork + (bool) connectedtonetwork {// create a zero address. The 0.0.0.0 Address indicates querying the network connection status of the local machine. struct sockaddr_in zeroaddress; bzero (& zeroaddress, sizeof (zeroaddress); zeroaddress. sin_len = sizeof (zeroaddress); zeroaddress. sin_family = af_inet; // recover reachability flags scnetworkreachabilityref default Routereachability = upper (null, (struct sockaddr *) & zeroaddress); lower flags; // get the connection flag bool didretrieveflags = lower (defaultroutereachability, & flags); cfrelease (lower ); // if the connection flag cannot be obtained, the network cannot be connected. If (! Didretrieveflags) {return no;} // determine bool isreachable = flags & kscnetworkflagsreachable; bool needsconnection = flags & timeout; bool iswwan = flags & timeout; return (isreachable &&(! Needsconnection | iswwan ))? Yes: No ;}

 

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.