簡單的多線程端口檢測程序

下面把網絡課設的源碼拿出來曬曬,供大家參考

//檢測線程

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wst;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 *
 * @author Administrator
 */
public class TelnetThread implements Runnable {

    private int port;
    private String ip;

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

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

    public int getPort() {
        return port;
    }

    public String getIp() {
        return ip;
    }

    public TelnetThread() {
        ip = "127.0.0.1";
        port = 8080;
    }

    public TelnetThread(String ip, int port) {
        this.port = port;
        this.ip = ip;
    }

    public void run() {
        try {
            Socket server = new Socket();
            InetSocketAddress address = new InetSocketAddress(ip, port);
            server.connect(address, 1000);
            System.out.println(address.toString() + "可達");
        } catch (IOException e) {
            System.out.println("Thread: "+ip+":"+port+"不可達");
        }
    }
}
//檢測主函數

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wst;

import java.io.IOException;
import java.net.InetAddress;

/**
 *
 * @author Administrator
 */
public class TelnetTest {

    public static void main(String[] args) {
        try {
            System.out.println("======InetAddress測試IP是否可達======");
            InetAddress addressLocal = InetAddress.getLocalHost();
            InetAddress addre***obot = InetAddress.getByName("210.45.251.3");
            if (addressLocal.isReachable(3000)) {
                System.out.println(addressLocal.toString() + "可達");
            } else {
                System.out.println(addressLocal.toString() + "不可達");
            }
            if (addre***obot.isReachable(3000)) {
                System.out.println(addre***obot.toString() + "可達");
            } else {
                System.out.println(addre***obot.toString() + "不可達");
            }
            System.out.println("======Telnet測試IP是否可達======");
            for (int i = 20; i < 8000; i++) {
                Thread socketThread = new Thread(new TelnetThread("127.0.0.1",i));
                socketThread.start();
            }
        } catch (IOException e) {
            System.out.println("Main");
        }
//        Runtime run = Runtime.getRuntime();
//
//        String msg = "";
//        Process p = null;
//        for (int i =20; i < 4000; i++) {
//            StringBuffer command = new StringBuffer("telnet localhost ");
//            p = run.exec((command.append(" ").append(i)).toString());
//            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
//            while ((msg = in.readLine()) != null) {
//                System.out.println("returnMsg:  "+msg);
//            }
////            System.out.println("End:  "+(command.toString()));
//        }
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章