Java determines whether the network is healthy by pinging

Source: Internet
Author: User

1 ImportJava.io.BufferedReader; 2 Importjava.io.IOException; 3 ImportJava.io.InputStream; 4 ImportJava.io.InputStreamReader; 5      6 /**  7 * Determine the network connection status. 8  *  9  */   Ten  Public classNetstate { One       A      Public BooleanIsconnect () { -         BooleanConnect =false;  -Runtime runtime =Runtime.getruntime ();  the process process;  -         Try {     -Process = Runtime.exec ("ping" + "www.baidu.com");  -InputStream is =Process.getinputstream ();  +InputStreamReader ISR =NewInputStreamReader (IS);  -BufferedReader br =NewBufferedReader (ISR);  +String line =NULL;  AStringBuffer SB =NewStringBuffer ();  at              while(line = Br.readline ())! =NULL) {      - Sb.append (line);  -             }      -System.out.println ("Return value is:" +SB);  - Is.close ();  - Isr.close ();  in Br.close ();  -       to             if(NULL! = SB &&!sb.tostring (). Equals ("")) {      +String logstring = "";  -                 if(Sb.tostring (). IndexOf ("TTL") > 0) {      the                     //Network unblocked *Connect =true;  $}Else {     Panax Notoginseng                     //Network is not unblocked -Connect =false;  the                 }      +             }      A}Catch(IOException e) { the E.printstacktrace ();  +         }      -         returnConnect;  $     }     $           -      Public Static voidMain (string[] args) { -Netstate netstate =Newnetstate ();  the System.out.println (Netstate.isconnect ());  -      Wuyi     }     the}

Java determines whether the network is healthy by pinging

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.