package q;
public class main {
public static void main(String[] args) {
new PortScannerFrame();
}
}
package q;
import java.io.IOException;
import .*;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class PortScan {
String ip;
Socket socket;
int port =0,endPort = 65525;
public PortScan(){
this.ip = "127.0.0.1";
this.port = 8000;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getEndPort() {
return endPort;
}
public void setEndPort(int endPort) {
this.endPort = endPort;
}
}
package q;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import .Socket;
import .UnknownHostException;
import javax.swing.*;
public class PortScannerFrame extends JFrame implements ActionListener{
JTabbedPane jtp = new JTabbedPane();//卡片布局
JPanel portPanel = new JPanel();
//端口扫描部分
JLabel label3 = new JLabel("请输入要扫描的主机的ip地址:");
JTextField ipFieldForPortScan = new JTextField("127.0.0.1",20);
JButton startPortScan = new JButton("开始端口扫描");
JTextArea jta2 = new JTextArea("目标主机开放的端口:\n");//显示端口扫描结果,此处显示开放的端口
JTextArea jta3 = new JTextArea("目标主机未开放的端口:\n");
JLabel label4 = new JLabel("请输入开始端口:");
JLabel label5 = new JLabel("请输入终止端口:");
JTextField startPort = new JTextField("8000",10);
JTextField endPort = new JTextField("8080",10);
PortScan portscan = new PortScan();
public PortScannerFrame(){
init();
portScanInit();
}
private void portScanInit(){
jtp.add(portPanel,"port扫描");
JPanel portPanelNorth = new JPanel();
JPanel portPanelCenter = new JPanel();
JScrollPane jsp1 = new JScrollPane(jta2);
JScrollPane jsp2 = new JScrollPane(jta3);
portPanelCenter.setLayout(new GridLayout(0,2));
portPanelNorth.setLayout(new GridLayout(0,2));
portPanel.setLayout(new BorderLayout());
portPanelNorth.add(label3);
portPanelNorth.add(ipFieldForPortScan);
portPanelNorth.add(label4);
portPanelNorth.add(startPort);
portPanelNorth.add(label5);
portPanelNorth.add(endPort);
portPanelNorth.add(startPortScan);
portPanel.add(portPanelNorth,BorderLayout.NORTH);
portPanelCenter.add(jsp1);
portPanelCenter.add(jsp2);
portPanel.add(portPanelCenter);
startPortScan.addActionListener(this);
startPort.addActionListener(this);
}
private void init(){
//ipPanel.setLayout(new GridLayout(0,2));
this.add(jtp);
this.setResizable(false);
this.setVisible(true);
this.setSize(60
0, 400);
this.setTitle("端口扫描器");
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setBackground(Color.cyan);
this.setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == this.startPortScan){
portscan.setIp(ipFieldForPortScan.getText().trim());
portscan.setPort(Integer.parseInt(startPort.getText()));//设置起始扫描端口
portscan.setEndPort(Integer.parseInt(endPort.getText()));
//portscan.start(jta2);
new Thread(new PortScanThread()).start();
}
if(e.getSource() == this.startPort){
System.exit(0);
}
}
class PortScanThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
startScan();
}
public void startScan(){//调用该方法开始扫描
while(portscan.getPort() <= portscan.getEndPort()){
try {
Socket socket = new Socket(portscan.getIp(),portscan.getPort());
if(socket.isConnected()){
jta2.append(portscan.getPort() + "\n");
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
jta3.append(portscan.getPort() + "\n");
}
portscan.setPort(portscan.getPort() + 1);
}
}
}
}