导航:首页 > 编程语言 > javaping实现

javaping实现

发布时间:2022-07-18 12:08:23

① 用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,你自身有定位服务,能计算出距离;

阅读全文

与javaping实现相关的资料

热点内容
战双程序员 浏览:479
him触摸编程软件 浏览:929
植物大战僵尸存档怎么转移安卓 浏览:852
java栈的元素 浏览:737
程序员与篮球事件 浏览:675
app反编译不完整 浏览:788
电脑上的文件夹怎么调整 浏览:7
服务器无响应是什么原因呀 浏览:984
wd文档里的app怎么制作 浏览:513
电脑里的文件夹没有了一般能恢复吗 浏览:418
哪里有配加密钥匙的 浏览:210
服务器开不了机怎么把数据弄出来 浏览:958
gif动态图片怎么压缩 浏览:521
黑猴子棒球压缩文件解压密码 浏览:631
如何让app适应不同的手机屏幕大小 浏览:10
苹果手机如何给安卓手机分享软件 浏览:761
苹果电脑怎么运行腾讯云服务器 浏览:59
明日之后沙石堡命令助手 浏览:261
蛋糕店用什么样的app 浏览:877
长安银行信用卡app怎么取现 浏览:635