usando el comando ping del sistema
public String ping(){
Runtime runtime=Runtime.getRuntime();
String os=System.getProperty("os.name");
if(os.equalsIgnoreCase("Linux")){
try {
Process process=runtime.exec("ping -c 4 "+address);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream()));
String line,data="";
while((line=bufferedReader.readLine())!=null)
if(line.startsWith("--- "+address)){
data=bufferedReader.readLine();
break;
}
process.destroy();
bufferedReader.close();
return data.split(",")[3].trim();
} catch (IOException e) {
System.err.println(e);
}
}else if(os.indexOf("win")!=-1){
try {
Process process=runtime.exec("ping "+address);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream()));
String line,data="";
while((line=bufferedReader.readLine())!=null)
if(line.startsWith("Approximate")||line.startsWith("Tiempos")){
data=bufferedReader.readLine();
data=data.split(",")[2].split("=")[1].trim();
break;
}
process.destroy();
bufferedReader.close();
return data;
} catch (IOException e) {System.err.println(e);
}
}
return null;
}
usando una pagina
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* @author Leyer
*/
public class Ping {
private URLConnection urlConnection;
private String address;
public Ping(String address){
try {
this.address = address;
urlConnection=new URL("http://clez.net/net.ping?ip="+address+"&t=icmp&p=80#scan")
.openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String start(){
BufferedReader bufferedReader = null;
try {
InputStream inputStream=urlConnection.getInputStream();
bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while((line=bufferedReader.readLine())!=null){
if(line.equalsIgnoreCase("--- "+address+" ping statistics ---")){
String ping=bufferedReader.readLine();
return ping;
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bufferedReader != null)
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "-1";
}
public static void main(String args[]){
System.out.println(new Ping("Direccion IP").start());
}
}
<pre>

