我已经创建了一段代码,它接受一个IP地址(来自另一个类中的main方法),然后循环遍历一系列IP地址,并在每个IP地址上执行.我有一个GUI前端,它正在崩溃(因此我为什么要完成多线程.我的问题是我不能再将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 …
Run Code Online (Sandbox Code Playgroud) 任何人都可以弄清楚为什么我在这里获得索引超出范围的异常?我可以在第一批数据中读取,但是当我尝试再次循环时,它会出错.我也在is.read(readBytes,offset,length)上得到'赋值永不使用'错误; 这也是其中的一部分.
InetAddress address = packet.getAddress();
int port = packet.getPort();
RRQ RRQ = new RRQ();
int offset = 0;
int length = 512;
int block = 0;
byte[] readBytes = new byte[512];
File file = new File(filename);
FileInputStream is = new FileInputStream(file);
int fileBytes = is.read(readBytes, offset, length);
DatagramPacket outPacket = RRQ.doRRQ(readBytes, block, address, port);
socket.send(outPacket);
block++;
if (fileBytes != -1)
{
socket.receive(packet);
offset = offset + fileBytes;
System.out.println(offset);
Exceptions here:fileBytes = is.read(readBytes, offset, length);
outPacket = RRQ.doRRQ(readBytes, block, address, …
Run Code Online (Sandbox Code Playgroud)