java 扫描tcp端口号_多线程TCP端口扫描 java实现

界面部分:

import java.awt.Color;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextArea;

public class Scaner extends JFrame {

private

static final long serialVersionUID = 1L;

public

static JCoolButton submit = new JCoolButton();

public

static JCoolButton stop = new JCoolButton();

public

static JTextArea ip = new JTextArea();

public

static JTextArea min = new JTextArea();

public

static JLabel to = new JLabel("至");

public

static JTextArea max = new JTextArea();

public

static JTextArea result = new JTextArea();

public

static JLabel ipadd = new JLabel("IP地址:");

public

static JLabel port = new JLabel("端口:");

public

static JDialog errInf = new JDialog();

public

static JLabel inf = new JLabel("端口号必须为0至65535的整数");

public

void launchframe() {

JPanel jp = new JPanel();

jp.setLayout(null);

jp.setOpaque(true);

jp.setBackground(Color.LIGHT_GRAY);

setSize(480, 320);

setLocation(450, 250);

setTitle("多线程局域网TCP端口扫描");

ipadd.setBounds(10, 30, 50, 25);

ip.setBounds(55, 30, 100, 25);

port.setBounds(160, 30, 35, 25);

min.setBounds(195, 30, 35, 25);

to.setBounds(235, 30, 15, 25);

max.setBounds(255, 30, 35, 25);

submit.setBounds(310, 20, 60, 35);

submit.setText("确定");

submit.addActionListener(new submitAction());

stop.setBounds(380, 20, 60, 35);

stop.setText("停止");

stop.addActionListener(new cancelAction());

result.setBounds(10, 60, 430, 210);

result.setLineWrap(true);

result.setEditable(false);

Container dPanel = errInf.getContentPane();

errInf.setSize(200, 80);

errInf.setLocation(550, 300);

dPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

dPanel.add(inf);

jp.add(ipadd);

jp.add(ip);

jp.add(port);

jp.add(min);

jp.add(to);

jp.add(max);

jp.add(submit);

jp.add(stop);

jp.add(result);

add(jp);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

setVisible(true);

}

public

static void main(String[] args) {

new Scaner().launchframe();

}

}

线程部分:

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.net.InetAddress;

import java.net.Socket;

import java.util.*;

class TCPThread extends Thread {

public

static InetAddress hostAddress;

public

static boolean runFlag = true;

public

static int iport = Integer.parseInt(Scaner.min.getText());

public

static int aport = Integer.parseInt(Scaner.max.getText());

List ports = new

ArrayList();

Set s = new

HashSet();

private

int threadnum;

public

TCPThread() {

}

public

TCPThread(String name, int threadnum) {

super(name);

this.threadnum = threadnum;

}

public

static List

removeDuplicateWithOrder(List

list) {

Set set = new

HashSet();

List newList = new

ArrayList();

for

(Iterator iter =

list.iterator(); iter.hasNext();) {

Object element = iter.next();

if (set.add((Integer) element))

newList.add((Integer) element);

}

return

newList;

}

public

void run() {

int i = 0;

Socket TCPSocket;

runFlag = true;

for (i = iport + threadnum; i < aport; i += 200)

{

if (runFlag) {

try {

hostAddress = InetAddress

.getByName(Scaner.ip.getText());

TCPSocket = new Socket(hostAddress, i);

TCPSocket.close();

ports.add(i);

for(Iterator iter =

TCPThread.removeDuplicateWithOrder(ports).iterator();iter.hasNext();){

Scaner.result.append(" " + iter.next());

}

} catch (IOException e) {

}

}

}

runFlag = false;

if(i == aport){;

Scaner.result.append("\n" + "扫描结束...");

}

}

}

class submitAction implements ActionListener {

public

void actionPerformed(ActionEvent a) {

if (Integer.parseInt(Scaner.min.getText()) < 0

|| Integer.parseInt(Scaner.max.getText()) > 65535)

{

Scaner.errInf.setVisible(true);

};

Scaner.result.setText("开始扫描:");

synchronized (this) {

for (int i = 0; i < 200; i++) {

new TCPThread("T" + i, i).start();

}

}

}

}

class cancelAction implements ActionListener {

public

void actionPerformed(ActionEvent a) {

TCPThread.runFlag = false;

}

}

按钮变换部分:

import javax.swing.JButton;

import java.awt.Cursor;

import java.awt.event.*;

public class JCoolButton extends JButton {

private

static final long serialVersionUID = 1L;

public

JCoolButton() {

super();

init();

}

public

void init() {

setBorderPainted(false);

addMouseListener(new MouseAdapter() {

@Override

public void mouseEntered(MouseEvent arg0) {

setBorderPainted(true);

setCursor(new Cursor(Cursor.HAND_CURSOR));

}

@Override

public void mouseExited(MouseEvent arg0) {

setBorderPainted(false);

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值