① 用java怎麼實現調用cmd,並執行ping命令,求完整的語句
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
Process p = rt.exec("cmd /c ping www..com -t");
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String temp = null;
while((temp =br.readLine()) != null){
System.out.println(temp);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
② java怎樣實現ping的功能...
可以用InetAddress的isReachable方法:
import java.net.InetAddress;
public class MainTest {
public static void main(String[] args) {
try {
int timeOut = 3000;
byte[] ip = new byte[] { (byte) 192, (byte) 168, (byte) 100, (byte) 151 };
int retry = 4;
InetAddress address = InetAddress.getByAddress(ip);
for (int i = 0; i < retry; i++) {
if (address.isReachable(timeOut)) {
System.out.println(i + " OK");
} else {
System.out.println(i + " LOSS");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
③ 如何在JAVA語言中調用PING命令
*
* Ping.java
*
* Created on 2007年7月3日, 下午2:44
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package pingtest;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author tommy
*/
public class Ping
{
public Ping()
{
for(int i =0;i<255;i++)
result[i] = 0;
}
public void scan()
{
for(int i=70;i<85;i++)
{
String tmp = exec + i;
try
{
Process pr = Runtime.getRuntime().exec(tmp);
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while (true)
{
String s = br.readLine();
if(s==null)
break;
if (s.contains("TTL"))
result[i] = 1;
System.out.println(s);
}
br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
public static void main(String args[])
{
Ping a = new Ping();
a.scan();
for(int i =0;i<255;i++)
System.out.println("result["+i+"] = "+a.result[i]);
}
private static String exec = new String("cmd /c ping 100.3.3.");
public int[] result = new int[255];
}
④ 用java寫個quick ping
簡單點的直接使用Runtime的exec來搞定,如下:
publicstaticvoidmain(String[]args)
{
StringBufferbuf=newStringBuffer();
Strings="";
Processprocess;
try
{
process=Runtime.getRuntime().exec("cmd/c"+"ping127.0.0.1");
BufferedReaderbr=newBufferedReader(newInputStreamReader(
process.getInputStream()));
while((s=br.readLine())!=null)
{
buf.append(s+" ");
}
process.waitFor();
System.out.println(buf);
}catch(Exceptionex)
{
ex.printStackTrace();
}
}
復雜點的,可以查看Oracle的官網文檔,實現了完整的ping:(粘貼下面的路徑到瀏覽器地址欄,加上http://)
docs.oracle.com/javase/1.5.0/docs/guide/nio/example/Ping.java
⑤ Java實現IP是否能Ping通功能
1.Jdk1.5的InetAddresss方式
自從Java 1.5,java.net包中就實現了ICMP ping的功能。
見:Ping類的ping(String)函數,String為ip不加埠號。
使用時應注意,如果遠程伺服器設置了防火牆或相關的配製,可能會影響到結果。另外,由於發送ICMP請求需要程序對系統有一定的許可權,當這個許可權無法滿足時, isReachable方法將試著連接遠程主機的TCP埠 7(Echo)。
2.最簡單的辦法,直接調用CMD
見Ping類的ping02(String)函數。
3.Java調用控制台執行ping命令
具體的思路是這樣的:
通過程序調用類似「ping 127.0.0.1 -n 10 -w 4」的命令,這命令會執行ping十次,如果通順則會輸出類似「來自127.0.0.1的回復: 位元組=32 時間<1ms TTL=64」的文本(具體數字根據實際情況會有變化),其中中文是根據環境本地化的,有些機器上的中文部分是英文,但不論是中英文環境, 後面的「<1ms TTL=62」字樣總是固定的,它表明一次ping的結果是能通的。如果這個字樣出現的次數等於10次即測試的次數,則說明127.0.0.1是百分之百能連通的。
技術上:具體調用dos命令用Runtime.getRuntime().exec實現,查看字元串是否符合格式用正則表達式實現。
見Ping類的ping(String,int,int)函數。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Ping {
public static boolean ping(String ipAddress) throws Exception {
int timeOut = 3000 ; //超時應該在3鈔以上
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 當返回值是true時,說明host是可用的,false則不可。
return status;
}
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()));
while ((line = buf.readLine()) != null)
System.out.println(line);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public static boolean ping(String ipAddress, int pingTimes, int timeOut) {
BufferedReader in = null;
Runtime r = Runtime.getRuntime(); // 將要執行的ping命令,此命令是windows格式的命令
String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut;
try { // 執行命令並獲取輸出
System.out.println(pingCommand);
Process p = r.exec(pingCommand);
if (p == null) {
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream())); // 逐行檢查輸出,計算類似出現=23ms TTL=62字樣的次數
int connectedCount = 0;
String line = null;
while ((line = in.readLine()) != null) {
connectedCount += getCheckResult(line);
} // 如果出現類似=23ms TTL=62這樣的字樣,出現的次數=測試次數則返回真
return connectedCount == pingTimes;
} catch (Exception ex) {
ex.printStackTrace(); // 出現異常則返回假
return false;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//若line含有=18ms TTL=16字樣,說明已經ping通,返回1,否則返回0.
private static int getCheckResult(String line) { // System.out.println("控制台輸出的結果為:"+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;
}
public static void main(String[] args) throws Exception {
String ipAddress = "127.0.0.1";
System.out.println(ping(ipAddress));
ping02(ipAddress);
System.out.println(ping(ipAddress, 5, 5000));
}
}
⑥ 用JAVA實現ping並給出詳細解釋
速度 = 距離 / 時間;
java 肯定是能實現的。前提需要移動設備,測量距離;
或者調用 XXX地圖的API,你自身有定位服務,能計算出距離;