这是我自己写的端口扫描程序,不过有2点失误的地方,第一只能扫描自己的端口,如果是另一个IP,却扫描不出来,我发帖子问了,都没人回答.
第二,扫描的速度特别慢,所以最好从自己的IP开始扫描,而且有的端口第一次扫描出现,第二次没有出现,不知道为什么,希望有人解答.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class SuperScaner extends JFrame
{
    public static JTextField sjtf1,sjtf2,sjtf3,sjtf4,ejtf1,ejtf2,ejtf3,ejtf4,threadjtf;
    public static JLabel sjl1,sjl2,sjl3,jl4,jl,ejl1,ejl2,ejl3,threadjl,areajl,resultjl;
    public static JTextArea jta;
    public JPanel jp1,jp2,jp3,jp4,jp;
    public JScrollPane jsp;
    public static JButton Sbutton,jb2;
    public SuperScaner()
    {
        Container c=getContentPane();
        sjtf1=new JTextField(4);sjtf2=new JTextField(4);sjtf3=new JTextField(4);sjtf4=new JTextField(4);
        ejtf1=new JTextField(4);ejtf2=new JTextField(4);ejtf3=new JTextField(4);ejtf4=new JTextField(4);
        threadjtf=new JTextField(7);
        jl=new JLabel(" IP地址:");sjl1=new JLabel(".");sjl2=new JLabel(".");sjl3=new JLabel(".");
        jl4=new JLabel("~");threadjl=new JLabel("线程数(1~100):");
        areajl=new JLabel("扫描结果:");
        ejl1=new JLabel(".");ejl2=new JLabel(".");ejl3=new JLabel(".");resultjl=new JLabel();
        resultjl.setLayout(new FlowLayout(FlowLayout.LEFT));
        jta=new JTextArea(15,65);
        jsp=new JScrollPane(jta);
        Sbutton=new JButton("确定");
        jb2=new JButton("取消");
        jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
        jp2=new JPanel();
        jp2.setLayout(new FlowLayout(FlowLayout.LEFT));
        jp3=new JPanel();
        jp3.setLayout(new BorderLayout());
        jp4=new JPanel();
        jp4.setLayout(new FlowLayout(FlowLayout.RIGHT));
        jp1.add(jl);jp1.add(sjtf1);jp1.add(sjl1);jp1.add(sjtf2);jp1.add(sjl2);jp1.add(sjtf3);jp1.add(sjl3);jp1.add(sjtf4);
        jp1.add(jl4);jp1.add(ejtf1);jp1.add(ejl1);jp1.add(ejtf2);jp1.add(ejl2);jp1.add(ejtf3);jp1.add(ejl3);jp1.add(ejtf4);
        jp1.add(threadjl);jp1.add(threadjtf);
        jp2.add(areajl);jp2.add(jsp);
        jp3.add(resultjl,BorderLayout.CENTER);jp4.add(Sbutton);jp4.add(jb2);
        jp3.add(jp4,BorderLayout.EAST);
        c.add(jp1,BorderLayout.NORTH);
        c.add(jp2,BorderLayout.CENTER);
        c.add(jp3,BorderLayout.SOUTH);
        setSize(740,400);
        setResizable(false);
        jta.setEditable(false);
        setLocation(400,300);
        show();
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        Sbutton.addActionListener(new Sbutton());
    }
    public static void main(String args[])
    {
        new SuperScaner();
    }
};
class ThreadScaner implements Runnable
{
    public static InetAddress address;
    public static int min,max;
    private int i;
    Object obj = new Object();
    public Sbutton s;
    public ThreadScaner(Sbutton s)
    {
        this.s=s;
    }
    
    public void run()
    {
        Socket theTCPsocket;
            //SuperScaner.jta.append(address.getHostName()+" , "+address.getHostAddress()+"\n");
            for(i=1;i<2000;i++)
            {
                SuperScaner.resultjl.setText("扫描状态:正在扫描 "+ i +" 端口"+"\n");
                try
                {
                    theTCPsocket=new Socket(address,i);
                    theTCPsocket.close();
                    SuperScaner.jta.append(" "+i);
                    switch(i)
                    {
                        case 21:
                        SuperScaner.jta.append("(FTP)");
                        break;
                        case 23:
                        SuperScaner.jta.append("(TELNET)");
                        break;
                        case 25:
                        SuperScaner.jta.append("(SMTP)");
                        break; 
                        case 80:
                        SuperScaner.jta.append("(HTTP)");
                        break;
                        case 110:
                        SuperScaner.jta.append("(POP3)");
                        break;
                        case 135:
                        SuperScaner.jta.append("(Remote Procedure Call服务)");
                        break;
                        case 139:
                        SuperScaner.jta.append("(netBIOS)");
                        break;
                        case 2000:
                        SuperScaner.jta.append("(木马默认端口)");
                        break;
                        case 3389:
                        SuperScaner.jta.append("(Win2000 远程登陆端口)");
                        break;
                        case 4000:
                        SuperScaner.jta.append("(OICQ)");
                        break;
                        case 6667:
                        SuperScaner.jta.append("(IRC)");
                        break;
                    }
                    SuperScaner.jta.append("\n");
                }
                catch (Exception e)
                {
                }
            }
            if(i==2000){SuperScaner.jta.append("扫描完成\n");SuperScaner.resultjl.setText(ThreadScaner.address.getHostName()+"扫描状态:完成!\n");Sbutton.is4++;s.caclu();}
    }
};
class Sbutton implements ActionListener
{
    public static int is1,is2,is3,is4,ie1,ie2,ie3,ie4,threadnum;
    ThreadScaner ts=new ThreadScaner(this);
    public int i;
    byte b[]=new byte[4];
    public void actionPerformed(ActionEvent e)
    {
        if(SuperScaner.sjtf1.getText().length()!=0&&SuperScaner.sjtf2.getText().length()!=0&&SuperScaner.sjtf3.getText().length()!=0&&SuperScaner.sjtf4.getText().length()!=0)
        {
            is1=Integer.parseInt(SuperScaner.sjtf1.getText());is2=Integer.parseInt(SuperScaner.sjtf2.getText());is3=Integer.parseInt(SuperScaner.sjtf3.getText());is4=Integer.parseInt(SuperScaner.sjtf4.getText());
            ie1=Integer.parseInt(SuperScaner.ejtf1.getText());ie2=Integer.parseInt(SuperScaner.ejtf2.getText());ie3=Integer.parseInt(SuperScaner.ejtf3.getText());ie4=Integer.parseInt(SuperScaner.ejtf4.getText());
            threadnum=Integer.parseInt(SuperScaner.threadjtf.getText());
            if(is1>=0 && is1<=255&&is2>=0&&is2<=255&&is3>=0&&is3<=255&&is4>=0&&is4<=255)
            {
                if(is1==ie1&&is2==ie2&&is3==ie3&&is4<ie4)
                {
                    ThreadScaner.min=is4;ThreadScaner.max=ie4;
                }
            }
            else JOptionPane.showMessageDialog(null,"数值超出范围","错误提示",JOptionPane.ERROR_MESSAGE);
            
            SuperScaner.jta.setText("");
            caclu();
        }
        else JOptionPane.showMessageDialog(null,"没有输入数据","错误提示",JOptionPane.ERROR_MESSAGE);
    }
    public void caclu()
    {
        int count=0;
            try
            {
                if(is4<=ie4){System.out.println(""+is4);
                b[0]=(byte)is1;b[1]=(byte)is2;b[2]=(byte)is3;b[3]=(byte)is4;
                ThreadScaner.address=InetAddress.getByAddress(b);
                SuperScaner.jta.append(ThreadScaner.address.getHostName()+" , "+ThreadScaner.address.getHostAddress()+"\n");
                if(ThreadScaner.address.getHostName().equals(ThreadScaner.address.getHostAddress())){SuperScaner.jta.append("这个IP可能没人用!\n");is4++;caclu();}
                else{
                SuperScaner.jta.append("正在扫描 "+ThreadScaner.address.getHostName()+" 请稍等\n");
                SuperScaner.jta.append("开放端口:  \n");
                while(count<threadnum){new Thread(ts).start();count++;}}
                }
                else {SuperScaner.jta.append("扫描完成!\n");SuperScaner.resultjl.setText("扫描状态:完成!");}
            }
            catch (Exception ex)
            {
                ex.getMessage();
            }
    }
};

 
											





