Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.io.UnsupportedEncodingException;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Pinger {
/** * To ping the host * *
Private String remoteipaddress;
/** * The number of times the ping was set * *
private final int pingtimes;
/** * Setting Timeout * *
private int timeOut;
/** * constructor * @param remoteipaddress * @param pingtimes * @param timeOut * *
Public Pinger (String remoteipaddress, int pingtimes, int timeOut) {
Super ();
this.remoteipaddress = remoteipaddress;
This.pingtimes = Pingtimes;
This.timeout = TimeOut;
}
/** * Test If you can ping the * @param server * @param timeout * @return
* @throws unsupportedencodingexception * *
public Boolean isreachable () throws Unsupportedencodingexception {
BufferedReader in = null;
Runtime r = Runtime.getruntime ();
The ping command that will be executed, which is a command in Windows format
String Pingcommand = "Ping" + remoteipaddress + "-n" + Pingtimes + "-w" + TimeOut;
try {
Execute command and get output
System.out.println (Pingcommand);
Process p = r.exec (Pingcommand);
if (p = = null) {
return false;
}
in = new BufferedReader (New InputStreamReader (P.getinputstream (), "GBK"));
Check output line by row to calculate the number of occurrences of =23ms ttl=62 typeface
int connectedcount = 0;
String line = null;
while (line = In.readline ())!= null) {
String Str=new string (line.getbytes ("GB2312"), "GBK");
System.out.println (str);
System.out.println ("Getcheckresult (line) = =" +getcheckresult (line));
Connectedcount + + getcheckresult (line);
}
If a word like =23ms ttl=62 appears, the number of occurrences = the number of tests returns True
System.out.println ("connectedcount==" +connectedcount);
return connectedcount = = Pingtimes;
catch (Exception ex) {
Ex.printstacktrace ();
Returns False if an exception occurs
return false;
finally {
try {
In.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
/** * If line contains =18ms ttl=16 words, indicating has ping pass, return 1, or return 0.
* * * @param line * @return * *
private static int Getcheckresult (String line) {
SYSTEM.OUT.PRINTLN ("The result of the console output is:" +line);
Pattern pattern = Pattern.compile ("(\\d+ms) (\\s+) (ttl=\\d+)",
pattern.case_insensitive);
System.out.println ("pattern.matcher (line) = =" +pattern.matcher (line));
Matcher Matcher = Pattern.matcher (line);
System.out.println ("matcher===" +matcher);
while (Matcher.find ()) {
return 1;
}
return 0;
}
public static void Main (string[] args) {
Pinger p = new Pinger ("192.168.2.171", 10, 5000);
try {
System.out.println (P.isreachable ());
catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
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.