怎么样用java程序实现一个类似于cmd ping的功能

SANBENREN 2009-10-20 09:52:29
怎么样用java程序实现一个类似于ping的功能,就是比如输入一个ip地址 看能不能通
...全文
246 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SANBENREN 2009-10-21
  • 打赏
  • 举报
回复
可是我要怎么得到一个返回标志 看它通不通呢

lz12366007 2009-10-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sanbenren 的回复:]
可是我要怎么得到一个返回标志 看它通不通呢


[/Quote]
1楼已经说了啊
获得进程返回的流 里面有进程返回的信息
如果返回

Pinging www.l.google.com [66.249.89.147] with 32 bytes of data:



Reply from 66.249.89.147: bytes=32 time=88ms TTL=243

Reply from 66.249.89.147: bytes=32 time=87ms TTL=243

Reply from 66.249.89.147: bytes=32 time=86ms TTL=243

Reply from 66.249.89.147: bytes=32 time=89ms TTL=243



Ping statistics for 66.249.89.147:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

Minimum = 86ms, Maximum = 89ms, Average = 87ms
这样的 信息 那就 成功了啊
dlutqw 2009-10-20
  • 打赏
  • 举报
回复
用java调cmd有很多例子的,可以根据返回值判断是否ping通。
Meronasia 2009-10-20
  • 打赏
  • 举报
回复
Socket
一头头 2009-10-20
  • 打赏
  • 举报
回复

import java.io.*;

import java.lang.*;

public class test4 {

public static void main(String[] args) {

try {

Process p = Runtime.getRuntime().exec("ping www.google.com");

byte[] msg = new byte[128];

int len;

while((len=p.getInputStream().read(msg)) > 0) {

System.out.print(new String(msg, 0, len));

}


byte[] rb = new byte[] { (byte)'\n' } ;

OutputStream os = p.getOutputStream();

os.write(rb);

os.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧