Call ping test Network in Java

Source: Internet
Author: User
Tags readline
public static final string[] encodes = new string[] {"UTF-8", "GBK", "GB2312", "iso-8859-1", "iso-8859-2"};
public static void Main (string[] args) throws Exception {
TODO auto-generated Method Stub
Boolean flag = Ping ("172.0.0.1");
SYSTEM.OUT.PRINTLN (flag);
PING02 ("172.28.20.92");
}
The first method: Jdk1.5 's inetaddresss, the code is simple.
public static Boolean ping (String ipaddress) throws Exception {
int timeOut = 3000; Timeout should be over 3 banknotes
Boolean status = Inetaddress.getbyname (IPAddress). isreachable (TimeOut); When the return value is true, the host is available and false is not.
return status;
}
The second approach: Use Java to invoke the cmd command, which is the simplest way to display the ping process locally.
public static void Ping02 (String ipaddress) throws Exception {
String line = null;
try {
Process Pro = Runtime.getruntime (). EXEC ("ping" + ipaddress);
BufferedReader buf = new BufferedReader (New InputStreamReader (
Pro.getinputstream (), "GBK"));
while (line = Buf.readline ())!= null) {
line = new String (line.getbytes ("iso-8859-1"), "UTF-8");
line = new String (line.getbytes ("UTF-8"), "GBK");
line = new String (line.getbytes ("iso-8859-1"), "Utf-8");
line = new String (line.getbytes ("iso-8859-1"), "GBK");
line = new String (line.getbytes ("gb2312"), "GBK");
line = new String (line.getbytes ("gb2312"), "UTF-8");
System.out.println (transcoding (line, "GBK"));
System.out.println (line);

}


catch (Exception ex) {
System.out.println (Ex.getmessage ());
}
}

The third way: It is also using the Java Invoke Console ping command, which is more reliable, but also universal, easy to use: Incoming IP, set the number of ping and timeout, can be based on the return value to determine whether Ping pass.


public static Boolean ping (String ipaddress, int pingtimes, int timeOut) {
BufferedReader in = null;
Runtime r = Runtime.getruntime (); The ping command that will be executed, which is a command in Windows format
String Pingcommand = "Ping" + ipaddress + "-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 ())); 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) {
Connectedcount + + getcheckresult (line);
//If a word similar to =23ms ttl=62 appears, the number of occurrences = number of tests returns True
return connectedcount = = Pingtimes;
catch (Exception ex) {
Ex.printstacktrace (); Returns False if an exception occurs
return false;
finally {
try {
In.close ();
catch (Exception e) {
E.printstacktrace ();
}
}
}
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);
Matcher Matcher = Pattern.matcher (line);
while (Matcher.find ()) {
return 1;
}
return 0;
}
Related Article

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.