import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
public class PortScanner {
public static void main(String[] args) throws Exception {
String host = "localhost";
InetAddress inetAddress = InetAddress.getByName(host);
String hostName = inetAddress.getHostName();
ThreadGroup tg = new ThreadGroup("Parent ThreadGroup");
for (int port = 0; port <= 65535; port++) {
final int portid = port;
// System.out.println(port);
new Thread(tg, "" + port) {
public void run() {
try {
Socket socket = new Socket(hostName, portid);
String text = hostName + " is listening on port " + portid;
System.out.println(text);
socket.close();
} catch (IOException e) {
}
}
}.start();
}
while (tg.activeCount() > 0) {
try {
System.out.println("Waiting for " + tg.activeCount() + " CThreads to Complete");
Thread.sleep(1000); // Main Thread or someThradObject.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Java多线程TCP端口扫描程序
最新推荐文章于 2024-08-03 21:02:21 发布