老师布置的任务:统计校园网内在线机子的数量,并将这些在线机子的IP输入到数据库!之后检测这些在线IP是否提供web服务或ftp服务!只针对windows平台或linux平台求源代码啊!!!!java源代码!不要批处理文件!java局域网源代码windowsweb服务

解决方案 »

  1.   

    简单点就是:用 ping 协议穷举,然后socket探测其 80 和 25 端口是否开放。如果要代码,那就请见谅了~~~
      

  2.   

    你说的我知道,我做出来用for遍历校园网内提供web服务的IP,可是我们老师要我们用try-catch-finally去遍历啊!!!凌乱了!不会了啊!!!求代码!!
      

  3.   


    问清楚你老师的目的吧,try catch 并不适合做循环结构,你们老师究竟希望在这个程序中体现 try catch 的什么思想,光靠猜不是办法。
      

  4.   

    老师给了我们思路!可是我们没听懂!我们学java才三个月没听懂老师说的!而且try-catch可以做到遍历的!
      

  5.   

    随便写了一个,添加了线程池,写的有点乱,就这个意思,你凑合看。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;
    }
    }}
      

  6.   

    谢谢beerspume大虾!你是在192.168.85.0所在的网段进行扫描,但是程序可以运行却不能检测出结果、、、(我刚试过)