首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

查看局域网在线IP的java源代码,该怎么解决

2013-09-17 
查看局域网在线IP的java源代码老师布置的任务:统计校园网内在线机子的数量,并将这些在线机子的IP输入到数

查看局域网在线IP的java源代码
老师布置的任务:统计校园网内在线机子的数量,并将这些在线机子的IP输入到数据库!之后检测这些在线IP是否提供web服务或ftp服务!只针对windows平台或linux平台求源代码啊!!!!java源代码!不要批处理文件! java 局域网 源代码 windows web服务
[解决办法]
随便写了一个,添加了线程池,写的有点乱,就这个意思,你凑合看。


public class Test {
public static void main(String[] args) {
int ips[] = new int[] { 192, 168, 85, 0 };

for (int i = 1; i < 255; i++) {
String ip = ips[0] + "." + ips[1] + "." + ips[2] + "." + i;
while (!runPT(ip)) {
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
//System.out.println("start "+ip);

}

}

public static class PingThread extends Thread {
private String ip;
private boolean running;

public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
public PingThread() {
}

@Override
public void run() {
while (true) {
synchronized (this) {
//running = true;
try {
if (ip != null) {
String cmd = "ping -c 4 " + ip;
String key = "bytes from " + ip;
//System.out.println(cmd);
boolean pingok = false;
java.lang.Process p = java.lang.Runtime
.getRuntime().exec(cmd);
java.io.InputStream is = p.getInputStream();
java.io.BufferedReader br = new java.io.BufferedReader(
new java.io.InputStreamReader(is));


String line;
int linenum=0;
while (null != (line = br.readLine())) {
linenum++;
//System.out.println(linenum+" : "+line);
if (line.indexOf(key) >= 0) {
pingok = true;
break;
}
if(linenum>2){
break;
}
}
p.destroy();
if (pingok) {
System.out.println("ping " + ip + " is ok");
 for (int i = 1; i < 255; i++) {
 while (!runCT(ip, i)) {
 try {
 Thread.sleep(10);
 } catch (Exception e) {
 }
 }
 }
}else{
//System.out.println("Unknow host "+ip);
}
}
} catch (Exception e) {
} finally {
this.ip = null;
running = false;
try {
this.wait();
} catch (Exception e) {
}

}
}
}
}
}

public static class ConnectThread extends Thread {
private String host;
private int port;
private boolean running;

public ConnectThread() {
}

public boolean isRunning() {
return running;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public void setRunning(boolean running) {
this.running = running;
}

@Override
public void run() {
while (true) {
synchronized (this) {
//running = true;
try {
if (host != null) {
java.net.Socket s = new java.net.Socket(this.host,
this.port);
s.close();
System.out.println("Host " + this.host + " port "
+ this.port + " is opened");
}
} catch (Exception e) {
} finally {
running = false;
try {


this.wait();
} catch (Exception e) {
}
}
}
}
}
}

private static final Object ct_locker = new Object();
private static java.util.List<ConnectThread> ct_pool = new java.util.ArrayList<ConnectThread>();
private static int ct_pool_limit = 500;

public static boolean runCT(String host, int port) {
synchronized (ct_locker) {
ConnectThread ct = null;
for (int i = 0; i < ct_pool.size(); i++) {
if (!ct_pool.get(i).isRunning()) {
ct = ct_pool.get(i);
ct.setHost(host);
ct.setPort(port);
synchronized (ct) {
ct.notify();
}
break;
}
}
if (ct == null && ct_pool.size() < ct_pool_limit) {
ct = new ConnectThread();
ct.setHost(host);
ct.setPort(port);
ct.start();
ct_pool.add(ct);
}
if (ct != null) {

return true;
} else {
return false;
}
}
}

private static final Object pt_locker = new Object();
private static java.util.List<PingThread> pt_pool = new java.util.ArrayList<PingThread>();
private static int pt_pool_limit = 20;

public static boolean runPT(String ip) {
synchronized (pt_locker) {
boolean ret = false;
PingThread pt = null;
for (int i = 0; i < pt_pool.size(); i++) {
if (!pt_pool.get(i).isRunning()) {
pt = pt_pool.get(i);
pt.setIp(ip);
synchronized (pt) {
pt.setRunning(true);
pt.notify();
}
break;
}
}
if (pt == null && pt_pool.size() < pt_pool_limit) {
pt = new PingThread();
pt.setIp(ip);
pt.start();
pt.setRunning(true);
pt_pool.add(pt);
}
if (pt != null) {
ret = true;
}
return ret;
}
}

}

热点排行