📄 scanhost.java~1~
字号:
package javanet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.net.InetAddress;
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.Socket;
public class Scanhost
extends JFrame {
Object sort[]={"TCP/IP","UDP"};
BorderLayout borderLayout1 = new BorderLayout();
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
JLabel jLabel1 = new JLabel();
JComboBox jComboBox1 = new JComboBox(sort);
JLabel jLabel2 = new JLabel();
JButton jButton2 = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JList jList1 = new JList();
DefaultListModel model=new DefaultListModel();
DatagramSocket socket;
DatagramPacket packet;
String s="0";
String head;
int fol=0;
Socket sockett;
public Scanhost() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
public void scanUDP()
{
try
{
if(fol>255){return;}
InetAddress addr = InetAddress.getByName(head + fol);
socket = new DatagramSocket();
packet = new DatagramPacket(s.getBytes(), s.getBytes().length, addr, 8181);
model.addElement(addr.toString().replaceAll("/",""));
socket.close();
throw new Exception();
}catch(Exception er){fol++;scanUDP();}
}
public void scanTCP()
{
try
{
if(fol>255){return;}
sockett = new Socket(head + fol, 8000);
model.addElement(head+fol);
sockett.close();
throw new Exception();
}catch(Exception er){fol++;scanTCP();}
}
private void jbInit() throws Exception {
setSize(new Dimension(297, 260));
getContentPane().setLayout(null);
jButton1.setBounds(new Rectangle(56, 179, 68, 29));
jButton1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jButton1.setText("扫描");
jButton1.addActionListener(new Scanhost_jButton1_actionAdapter(this));
this.setTitle("扫描可连接的主机");
jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jLabel1.setText("起始位置:");
jLabel1.setBounds(new Rectangle(8, 140, 59, 26));
jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jLabel2.setText("类 型:");
jLabel2.setBounds(new Rectangle(148, 140, 59, 26));
jComboBox1.setBounds(new Rectangle(210, 140, 69, 26));
jButton2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jButton2.addActionListener(new Scanhost_jButton2_actionAdapter(this));
jTextField1.setToolTipText("如:192.168.1.1");
this.getContentPane().add(jComboBox1);
jScrollPane1.setBounds(new Rectangle(40, 7, 211, 116));
jButton2.setBounds(new Rectangle(170, 179, 68, 29));
jButton2.setText("关闭");
this.getContentPane().add(jLabel1);
this.getContentPane().add(jTextField1);
this.getContentPane().add(jLabel2);
jList1.setModel(model);
this.getContentPane().add(jButton1);
this.getContentPane().add(jButton2);
this.getContentPane().add(jScrollPane1);
jScrollPane1.getViewport().add(jList1);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
jTextField1.setText("");
jTextField1.setBounds(new Rectangle(70, 140, 69, 26));
}
public void jButton2_actionPerformed(ActionEvent e) {
this.dispose();
}
public void jButton1_actionPerformed(ActionEvent e) {
model.removeAllElements();
String temp=jTextField1.getText().trim();
try
{
head = temp.substring(0, temp.lastIndexOf(".") + 1);
fol = Integer.parseInt(temp.substring(temp.lastIndexOf(".") + 1,
temp.length()));
}catch(Exception er){
new JOptionPane().showMessageDialog(this,"地址格式不正确","错误",0);
return;
}
if(jComboBox1.getSelectedIndex()==0)
{
scanTCP();
}else
{
scanUDP();
}
fol=0;
}
}
class Scanhost_jButton1_actionAdapter
implements ActionListener {
private Scanhost adaptee;
Scanhost_jButton1_actionAdapter(Scanhost adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Scanhost_jButton2_actionAdapter
implements ActionListener {
private Scanhost adaptee;
Scanhost_jButton2_actionAdapter(Scanhost adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -