import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; /** * @name * @author wujianxiong * @date 2016-5-19 */ public class PortScanner { /** * @name @param args * @author wujianxiong * @date 2016-5-19 */ public static void main(String[] args) { // TODO Auto-generated method stub String host = "127.0.0.1"; if(args.length>0) host = args[0]; PortScanner.scan(host); } /** * 扫描端口 * @name @param host * @author wujianxiong * @date 2016-5-19 */ public static void scan(String host){ Socket socket = null; //扫描端口 for(int port=1;port<1024;port++){ try { socket = new Socket(host, port); System.out.println("成功连接本地端口:"+port); } catch (UnknownHostException e) { System.out.println("未知错误!"); } catch (IOException e) { System.out.println("连接端口:"+port+"失败!"); } finally{ try { if(socket!=null) socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }