1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.*;
import java.util.*;
public class NetWork2 {
private static String outcome[][]=new String[254][2];
private static int threadCount = 0;
public static void main(String[] args) {
try{
InetAddress ay= InetAddress.getLocalHost();
String a1=ay.getHostAddress().toString();
String y[] = a1.split("\\.");
String a4=y[0]+"."+y[1]+"."+y[2]+".";
System.out.println(ay);
System.out.println(a1);
System.out.println(y[0]+"."+y[1]+"."+y[2]+".");
System.out.println(a4);
for(int i=1;i<=254;i++){
String a5=a4+i;
outcome[i-1][0] = a5;
isReachablePingTestThread irpt = new isReachablePingTestThread(i,a5);
irpt.start();
}
while(threadCount<254) {
try{
Thread.currentThread().sleep(1000);
System.out.println("threadCountCheck="+threadCount);
}
catch(InterruptedException ex) {
}
}
for(int j=1;j<=254;j++){
System.out.println(outcome[j-1][0]+"..."+outcome[j-1][1]);
}
}
catch(Exception e){
System.out.println("Exception e ="+e);
}
}
public static byte[] transform(String str) {
String[] x=str.split("\\.",4);
byte[] ip = new byte[x.length];
for(int i=0; i<x.length; i++) {
ip[i] = (byte) Integer.parseInt(x[i]);
}
return ip;
}
public static class isReachablePingTestThread extends Thread{
int count;
String testIP;
isReachablePingTestThread(int count,String testIP) {
this.count = count;
this.testIP = testIP;
}
public void run() {
try{
Process p = Runtime.getRuntime().exec("ping -n 1 " + testIP);
int status = p.waitFor();
if(status==0) {
outcome[count-1][1] = "alive";
}
else {
outcome[count-1][1] = "dead";
}
threadCount ++;
}
catch(Exception e){
System.out.println("isReachablePingTestThread error... "+e);
}
}
}
}
|