谁能给我一个ping程序的java实现啊(急用) 马上要交作业了,必须交一个ping程序的java实现,哪位大侠帮帮我啊,发个能运行的ping程序给我。谢谢你们了 [email protected] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;public class Ping{ public static void main(String args[]) { String str = "127.0.0.1"; String line = null; try { Process pro = Runtime.getRuntime().exec("ping " + str); 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()); } }} ping是基于icmp协议的,而java是在tcp和udp协议层上的。所以不能直接用Java实现Ping功能。 import java.io.IOException;import java.io.InputStream;public class TestBat { public static void main(String[] args) { String command = "c:\\demo.bat"; try { Process child = Runtime.getRuntime().exec(command); InputStream in = child.getInputStream(); int c; while ((c = in.read()) != -1) { System.out.print(c);//如果你不需要看输出,这行可以注销掉 } in.close(); try { child.waitFor(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("done"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} 哈哈,这样可以吗?很偷懒的思路,直接调用系统自带的pingimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class pingFake { public static void main(String[] args) { String ip = args[0]; String pingResult = ""; String pingCmd = "ping " + ip; try { Runtime r = Runtime.getRuntime(); Process p = r.exec(pingCmd); BufferedReader in = new BufferedReader(new InputStreamReader(p .getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); pingResult += inputLine; } in.close(); } catch (IOException e) { System.out.println(e); } }}output:java pingFake 127.0.0.1Pinging 127.0.0.1 with 32 bytes of data:Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Ping statistics for 127.0.0.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms Runtime.getRuntime().exec("cmd /c start ping 192.168.1.1"); 没什么说的,一楼的就基本可以满足要求了,作业嘛,不可能要求那么高,还要去管协议的东西~直接调系统的命令就行了,呵呵 Runtime.getRuntime().exec("cmd /c ping 192.168.1.1"); Integer o1=new Integer(10); Integer o2=new Integer(10); import java.io.IOException;import java.io.InputStream;public class TestBat { public static void main(String[] args) { String command = "c:\\demo.bat"; try { Process child = Runtime.getRuntime().exec(command); InputStream in = child.getInputStream(); int c; while ((c = in.read()) != -1) { System.out.print(c);//如果你不需要看输出,这行可以注销掉 } in.close(); try { child.waitFor(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("done"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}看不到ping 的结果就是 关于Java单链表的疑惑 0-9这10个数字组成无重复的三个数字组合 Spring注释驱动模式下如何进行代码混淆 怎么实现没有触发任何事件而窗口自己重绘? 我欲进入JSP,是否需学习JAVA 一个调试的问题,在main方法中调用formSubmit(IRequestCycle cycle) 讨论一下 C/S移植B/S的开发 请问编写JSP是否需要另外下载Servlet包 ? ba 数据库连接向大侠们求救 一个关于两个窗体之间的通讯问题 登陆界面问题 split数组越界问题
{
public static void main(String args[])
{
String str = "127.0.0.1";
String line = null;
try
{
Process pro = Runtime.getRuntime().exec("ping " + str);
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());
}
}}
所以不能直接用Java实现Ping功能。
import java.io.InputStream;public class TestBat {
public static void main(String[] args) {
String command = "c:\\demo.bat";
try {
Process child = Runtime.getRuntime().exec(command);
InputStream in = child.getInputStream();
int c;
while ((c = in.read()) != -1) {
System.out.print(c);//如果你不需要看输出,这行可以注销掉
}
in.close();
try {
child.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("done");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;public class pingFake { public static void main(String[] args) { String ip = args[0];
String pingResult = ""; String pingCmd = "ping " + ip; try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd); BufferedReader in = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
pingResult += inputLine;
}
in.close(); } catch (IOException e) {
System.out.println(e);
}
}
}output:
java pingFake 127.0.0.1Pinging 127.0.0.1 with 32 bytes of data:Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
Integer o2=new Integer(10);
import java.io.InputStream;public class TestBat {
public static void main(String[] args) {
String command = "c:\\demo.bat";
try {
Process child = Runtime.getRuntime().exec(command);
InputStream in = child.getInputStream();
int c;
while ((c = in.read()) != -1) {
System.out.print(c);//如果你不需要看输出,这行可以注销掉
}
in.close();
try {
child.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("done");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}看不到ping 的结果就是