通过下面代码,可以进行扫描指定ip的所有端口
package socketStudyDemo;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class SocketScan {
public static void main(String[] args) throws IOException {
// 47.***.***.***
Scanner input = new Scanner(System.in);
System.out.print("请输入你的学号姓名:");
String xx= input.next();
System.out.print("请输入要扫描的Ip地址:");
String serveip= input.next();
System.out.print("请输入端口连接最长时间(毫秒):");
int timeout= input.nextInt();
System.out.print("开始进行扫描常用端口...\n");
InetAddress ip = InetAddress.getByName(serveip);
Socket socket;
// int port[] = {80,118,3306,71,185};
// for(int i = 0;i<65535;i++) {
// port[port.length]=i+1;
// }
for(int i =0;i<=65535;i++) {
try {
socket= new Socket();
socket.connect(new InetSocketAddress(ip,i),timeout);
if(socket.isBound()) {
System.out.println(i+"端口开放\n");
}
}
catch(Exception e) {
// System.out.println(port[i]+"该端口不开放\n");
}
}
System.out.print("扫描完毕...\n");
}
}