扫描端口的程序,不要太复杂,我是新手,希望大家能帮助我,谢谢。

解决方案 »

  1.   

    import java.io.*;
    import java.net.*;
    class scan extends Thread
    {
    static int port=1;
    public static void main(String args[]) throws Exception
    {
    String host="127.0.0.1";
    int num=100;
    try{
    if(args[0]!=null)host=args[0];
    if(args[1]!=null)num=Integer.parseInt(args[1]); 
    }catch(Exception e){}
    System.out.print("扫描"+host+"从端口1到"+num+"开始");
    if(num<=0)num=100;
    new everyrun(num);
    for(;port<=num;port++)
    {
    new every(host,port).start();
    while(every.num>50&&port<=num)sleep(10);
    }//while
    System.exit(0);
    }//main
    }//class
    class every extends Thread
    {
    static int num=0;
    String host;int port;
    every(String nh,int np)
    {host=nh;port=np;num++;}
    public void run()
    {
    try{
    Socket ss=new Socket(host,port);
    System.out.println("\r......\t端口:"+port+"开放");
    ss.close();
    num--;
    }catch(Exception e){num--;}
    }
    }
    class everyrun extends Thread
    {
    int num;
    public everyrun(int num)
    {this.num=num;start();}
    public void run()
    {
    System.out.println();
    try{
    while(scan.port<=num)
    {
    System.out.print("...");sleep(500);
    System.out.print("\b \b");sleep(500);
    System.out.print("\b \b");sleep(500);
    System.out.print("\b \b");sleep(500);
    System.out.print(".");sleep(500);
    System.out.print(".");sleep(500);
    System.out.print("\b\b");
    }
    }catch(Exception e){}
    }
    }
      

  2.   

    programming these codes is not available for java
    you need to use c double plus or c to complete the function
    because the java did't supply the API to support raw socket or third layer
    protocol. 
    A better way to find the remote host port thru have-opend scan!