package New;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SearchPort extends JFrame {
JButton jb1;
JButton jb2;
JTextField jt;
JTextField jt1;
JTextField jt2;
static JTextArea ja;
static JLabel jl;
SearchPort() {
super("homework:端口扫描");
this.setVisible(true);
this.setSize(500, 400);
this.setLocation(300, 300);
this.setLayout(new BorderLayout());
JPanel p = new JPanel(new FlowLayout());
JPanel p1 = new JPanel(new FlowLayout());
JPanel p2 = new JPanel();
jb1 = new JButton("获得本地IP");
jb2 = new JButton("扫描端口");
jt = new JTextField(10);
jl = new JLabel(" 无扫描");
ja = new JTextArea(15, 40);
jt1 = new JTextField(5);
jt1.setText("1");
jt2 = new JTextField(5);
jt2.setText("2000");
jt.setEditable(false);
jt.setBackground(Color.WHITE);
ja.setEditable(false);
ja.setBackground(Color.WHITE);
jb2.setEnabled(false);
p.add(jb1);
p.add(jt);
p.add(jl);
p1.add(jb2);
p1.add(new JLabel("端口范围:"));
p1.add(jt1);
p1.add(new JLabel("至"));
p1.add(jt2);
p2.add(new JScrollPane(ja));
this.add(BorderLayout.NORTH, p);
this.add(BorderLayout.CENTER, p2);
this.add(BorderLayout.SOUTH, p1);
//注册监听
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
SearchThread.hostAddress = InetAddress.getLocalHost();
String ip = InetAddress.getLocalHost().getHostAddress();
jt.setText(ip);
jb2.setEnabled(true);
}
catch(Exception ex) {
JOptionPane.showMessageDialog(null, "获取IP出错!");
}
}
});
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int minPort;
int maxPort;
minPort = Integer.parseInt(jt1.getText());
maxPort = Integer.parseInt(jt2.getText());
if (minPort < 0 || maxPort > 65535 || minPort > maxPort) {
ja.setText("错误:请确认端口范围0~65535");
jl.setText(" 无扫描");
return;
}
ja.setText("");
ja.setText("正在扫描" + jt.getText() + "开放端口:\n");
SearchThread.Maxport = maxPort;
SearchThread.Minport = minPort;
for (int i = 0; i < 200; i++) {
//ja.append(SearchThread.hostAddress.getHostAddress()+ "\n");
new SearchThread(i).start();
}
}
});
}
public static void main(String[] args) {
new SearchPort();
}
}
- 1
- 2
前往页