Java如何创建端口扫描程序?
在此示例中,您将看到如何创建一个简单的端口扫描程序以检查指定主机名的开放端口。
package org.nhooo.example.network;
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();
for (int port = 0; port <= 65535; port++) {
try {
Socket socket = new Socket(hostName, port);
String text = hostName + " is listening on port " + port;
System.out.println(text);
socket.close();
} catch (IOException e) {
//空挡块
}
}
}
}上面的代码片段的结果是:
localhost is listening on port 53 localhost is listening on port 80 localhost is listening on port 443 localhost is listening on port 631 localhost is listening on port 1099 localhost is listening on port 3306 localhost is listening on port 4380 localhost is listening on port 6942 localhost is listening on port 8005 localhost is listening on port 8009 localhost is listening on port 8021 localhost is listening on port 8080 ...
热门推荐
7 简短的二胎祝福语
10 简短霸气女儿生日 祝福语
11 祝福语高考小众诗句简短
12 元旦祝福语20秒简短
13 老师过年祝福语大全 简短
14 一生祝福语简短
15 产检祝福语简短霸气
16 朋友店开张祝福语简短
17 生日爱情祝福语大全简短
18 朋友女儿生日祝福语 简短