我已经创建了一段代码,它需要一个IP地址(来自另一个类中的main方法),然后循环遍历每个IP地址的IP地址范围。我有一个图形用户界面的前端,这是崩溃(因此,为什么我已经完成了多线程。我的问题是我不能再将IP地址作为我的ping代码中的一个参数,因为它的可调用。对于这个,并不能找到一种方法来解决这个问题。有没有一种可调用的方法来采取参数?如果不是有任何其他方式来完成我想要做的事吗?有没有办法在可调用方法中使用参数?
我的样本代码:
public class doPing implements Callable<String>{
public String call() throws Exception{
String pingOutput = null;
//gets IP address and places into new IP object
InetAddress IPAddress = InetAddress.getByName(IPtoPing);
//finds if IP is reachable or not. a timeout timer of 3000 milliseconds is set.
//Results can vary depending on permissions so cmd method of doing this has also been added as backup
boolean reachable = IPAddress.isReachable(1400);
if (reachable){
pingOutput = IPtoPing + " is reachable.\n";
}else{
//runs ping command once on the IP address in CMD
Process ping = Runtime.getRuntime().exec("ping " + IPtoPing + " -n 1 -w 300");
//reads input from command line
BufferedReader in = new BufferedReader(new InputStreamReader(ping.getInputStream()));
String line;
int lineCount = 0;
while ((line = in.readLine()) != null) {
//increase line count to find part of command prompt output that we want
lineCount++;
//when line count is 3 print result
if (lineCount == 3){
pingOutput = "Ping to " + IPtoPing + ": " + line + "\n";
}
}
}
return pingOutput;
}
}
IPtoPing曾经是拍摄参数
所以....如果你通过的可赎回作为参数传递给它试图迭代可调用另一个函数,这将不起作用针对它通过的一系列参数... – Michael 2015-09-12 20:54:21
这是作为陈述或问题的目的吗?无论哪种方式,我都搞不清你在说什么/问。 – 2015-09-12 23:32:18