package work2;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class portScan {
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
System.out.print("请输入你想要扫描的ip地址:");
String yid=sc.next();
System.out.print("请输入timeout时间");
int timeout=sc.nextInt();
InetAddress ip=InetAddress.getByName(yid);
System.out.println("开始扫描:");
for(int i=0;i<=65535;i++) {
try {
Socket socket=new Socket();
//实现与端口连接
socket.connect(new InetSocketAddress(ip, i), timeout);
//判断端口连接状态
if(socket.isBound())
System.out.println(i+"端口是开放的");
} catch (Exception e) {
}
}
System.out.println("结束扫描");
}
}
注意:端口扫描是需要一点时间的,请耐心等待扫描完毕。