我一般是编程新手,必须为我的课程用 Java 创建一个 IP 扫描仪,而我目前遇到了障碍。在朋友的帮助下,我能够从 jFrame 中获取 IP 地址并将其转换为 IP 号码。从那里我将它传递到一个公共字符串中,该字符串扫描地址。它目前可以毫无问题地遍历地址,但是我不知道如何将结果从 try 块中取出以显示在文本块中,就像您期望 IP 扫描程序那样。这是我的代码:
private void jBtnPSScanActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//Get text from text boxes
String pingSSIP1 = jTxtPSSIPOne.getText();
String pingSSIP2 = jTxtPSSIPTwo.getText();
String pingSSIP3 = jTxtPSSIPThree.getText();
String pingSSIP4 = jTxtPSSIPFour.getText();
String pingSEIP1 = jTxtPSEIPOne.getText();
String pingSEIP2 = jTxtPSEIPTwo.getText();
String pingSEIP3 = jTxtPSEIPThree.getText();
String pingSEIP4 = jTxtPSEIPFour.getText();
//Convert text into IP Numbers
long ipNumberS = 0;
ipNumberS = ipNumberS + (Long.parseLong(pingSSIP1)*256*256*256) ;
ipNumberS = ipNumberS + (Long.parseLong(pingSSIP2)*256*256) ;
ipNumberS = ipNumberS + (Long.parseLong(pingSSIP3)*256) ;
ipNumberS = ipNumberS + (Long.parseLong(pingSSIP4)) ;
System.out.println (ipNumberS);
long ipNumberE = 0;
ipNumberE = ipNumberE + (Long.parseLong(pingSEIP1)*256*256*256) ;
ipNumberE = ipNumberE + (Long.parseLong(pingSEIP2)*256*256) ;
ipNumberE = ipNumberE + (Long.parseLong(pingSEIP3)*256) ;
ipNumberE = ipNumberE + (Long.parseLong(pingSEIP4)) ;
System.out.println (ipNumberE);
String startAddress = Long.toString(ipNumberS);
String endAddress = Long.toString(ipNumberE);
String returnIP = pingSweep(startAddress, endAddress, ipNumberS, ipNumberE);
System.out.println(returnIP);
//Update jTxtBlock with returnIP and result
}
和:
public String pingSweep (String startAddress, String endAddress, long ipNumberS, long ipNumberE)
{
String ip = "";
for (long currIP = ipNumberS; currIP < ipNumberE; currIP++)
{
try {
ip = Long.toString(currIP) ;
System.out.println(currIP);
InetAddress host = InetAddress.getByName(ip);
System.out.println("Host Reachable? " + host.isReachable(4000));
System.out.println(currIP);
System.out.println(ip);
ip = ip + " " + currIP + " " + host.isReachable(4000);
}
catch (UnknownHostException ex) {
System.err.println(ex);
break;
}
catch (IOException ex) {
System.err.println(ex);
break;
}
}
return ip;
}
提前致谢!
马修