求帮忙写个程序

wozuiaijiahui 2012-06-27 02:11:06
我想一直去连一个IP,也就是PING那个IP地址,求帮忙写个程序一直PINT它,如果连同就打印连接成功,否则打印连接失败。
...全文
129 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
raistlic 2012-06-29
  • 打赏
  • 举报
回复


import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.Executors;

public class Pinger implements Runnable {

public static void main(String[] args) throws Exception {

Executors.newCachedThreadPool().submit(new Pinger("127.0.0.1", 3000));
}

private final String ip;
private final int timeout;
public Pinger(String ip, int timeout) {

this.ip = ip;
this.timeout = Math.max(1000, timeout);
}

@Override
public void run() {

try {

while(true) {

InetAddress inet = InetAddress.getByName(ip);

System.out.print(ip);
if( inet.isReachable(timeout) )
System.out.println("连接成功");
else
System.out.println("连接失败");
}
}
catch(IOException ex) {

ex.printStackTrace();
}
}
}
daxiang253 2012-06-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

public class CommandUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(CommandUtil.class);

public static String exec(String command) {
LOGGER.info("开始执行命令: " + command);
Stri……
[/Quote]
.......................
  • 打赏
  • 举报
回复
重点在Runtime.getRuntime().exec("ping xxx");
chuanbing 2012-06-28
  • 打赏
  • 举报
回复
大致思路:通过java调用执行shell命令,对返回的结果进行解析,并且通过计算可得出流量。。。你可以百度一下吧
默默的伟哥 2012-06-27
  • 打赏
  • 举报
回复
public class CommandUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(CommandUtil.class);

public static String exec(String command) {
LOGGER.info("开始执行命令: " + command);
String result = null;
try {
Process process = Runtime.getRuntime().exec(command);
if (process != null) {
result = getExecMessage(process);
// 输出执行记录详情
LOGGER.info(result);
process.destroy();
}
LOGGER.info("执行完毕!");
}
catch (Exception ex) {
LOGGER.error("执行shell命令出现异常, 详情:", ex);
}
return result;
}

public static String exec(String[] cmdarray) {
LOGGER.info("开始执行命令: " + ArrayUtil.toString(cmdarray, "\n"));
String result = null;
try {
Process process = Runtime.getRuntime().exec(cmdarray);
if (process != null) {
result = getExecMessage(process);
// 输出执行记录详情
LOGGER.info(result);
process.destroy();
}
LOGGER.info("执行完毕!");
}
catch (Exception ex) {
LOGGER.error("执行shell命令出现异常, 详情: ", ex);
}
return result;
}

public static String getExecMessage(Process process) {
BufferedReader stream = null;
StringBuilder result = new StringBuilder();
try {
stream = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = stream.readLine()) != null) {
result.append(line).append("\n");
NullUtil.toNull(line);
}
LOGGER.info(result.insert(0, "执行详情: ").toString());
// if (process.waitFor() != 0) {
// error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// while ((line = error.readLine()) != null) {
// buffer.append(line).append("\n");
// NullUtil.toNull(line);
// }
// }
}
catch (Exception ex) {
LOGGER.error("执行错误, 详情: ", ex);
}
finally {
if (stream != null) {
try {
stream.close();
}
catch (IOException e) {}
}
}
return result.toString();
}

public static void main(String[] args) {
System.out.println(exec("ping www.baidu.com"));
}

62,614

社区成员

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

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