This article describes a simple port scanner implemented by Java network programming. Share it for your reference, as follows:
In the study of computer networks, I can’t help but feel that there are many fragmentary knowledge points in this course. At the same time, because the textbooks I studied are foreigners’ textbooks - the top-down method , it is difficult to learn. However, from foreigners’ textbooks, I can understand the knowledge system of computer networks from a certain level, and I enjoy it. At the same time, doing English exercises is also very interesting. From all aspects, I can say that I have benefited a lot and learned a lot of professional vocabulary. After the class, I wanted to make a simple port scanner, which was used in Java, because the implementation interface is very simple, and there are also encapsulated Socket classes that can be used. The main ideas are as follows:
Enter the specified host name or IP address in the main program interface, press the start button to scan whether the commonly used port of the host is open. Common port numbers are set internally: 21, 22, 23, 25, 26, 69, 80, 110, 143, 443, 465, 995, 1080, 1158, 1433, 1521, 2100, 3128, 3306, 3389,7001, 8080, 8081, 9080, 9090, 43958. It can also be modified by yourself. The principle of program implementation is to use Java to establish Socket to connect to the specified port of the target IP. If it can be connected, it proves that the port is open. On the contrary, if there is no connection before the timeout, the port is closed and an exception will occur. At the same time, considering the low efficiency of single-threaded operation, it also takes advantage of Java's multi-threaded programming, and uses multi-threaded scanning whether one port of the target host is open. Therefore, the final display result is randomized, and the internal setting is that up to 10 threads can be run at the same time (it can be changed according to the actual situation).
The main network knowledge used is the use of sockets. As well as List containers inside Java, Java interface design, interface layout, and usage of simple generic programming.
The main code is as follows:
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.SocketAddress;import java.net.UnknownHostException;import java.util.Arrays;import java.util.LinkedList;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;public class ScanPort extends JFrame { /** * Port scan starts the main program*/ private static final long serialVersionUID = 1L; String str1 = " Common ports are:"; String str2 = "ftp:21,22,telnet:23,smtp:25,http:80"; String str3 = "dns:53,tftp:69,snmp:161,162"; String str4 = "1158,1433,1521,2100,3128,26,69"; String str5 = "3306,3389,7001,8080,8081,110,143"; String str6 = "9080,9090,43958,443,465,995,1080"; JButton jb1 = new JButton("strat"); JTextArea jta = new JTextArea(); JScrollPane jsp = new JScrollPane(jta); JTextField jtf = new JTextField(17); String IP = "";//The IP or domain name to be scanned <Integer>portList = new LinkedList<Integer>();// Define a List container that represents the scanned cluster port Integer[] ports = new Integer[] { 21, 22, 23, 25, 26, 53,69, 80, 110, 143, 443, 465,69,161,162,135,995,1080,1158,1433,1521,2100, 3128, 3306, 3389, 7001, 8080, 8081, 9080, 9090, 43958 ,135,445,1025,1026,1027,1028,1055,5357}; // Common port collection public ScanPort() { this.add(getPanel(), BorderLayout.SOUTH); jsp.setBorder(BorderFactory.createEtchedBorder()); jsp.setBackground(Color.cyan); this.add(jsp, BorderLayout.CENTER); this.add(getPanel2(), BorderLayout.NORTH); this.add(getPanel3(), BorderLayout.WEST); this.setBounds(300, 60, 600, 600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("ScanPort"); jta.setTabSize(4); jta.setFont(new Font("QuickKaiTi", Font.BOLD, 16)); jta.setLineWrap(true);// Activate the automatic line wrap function jta.setWrapStyleWord(true);// Activate the line-breaking word function portList.addAll(Arrays.asList(ports)); //Add the value in ports to set and remove the duplicate jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub IP = jta.getText(); //IP is the string entered in the text box scanPorts(IP, portList, 10, 800); //The timeout is set to 800, the number of threads is set to 10 } }); this.setVisible(true); } /** * Opening of the specified List port collection for multithreaded scanning target host* * @param ip * IP or domain name to be scanned* @param portList * List collection of ports to be scanned* @param threadNumber * Number of threads* @param timeout * Connection timeout* */ public void scanPorts(String ip, List<Integer> portSet,int threadNumber, int timeout) { ExecutorService threadPool = Executors.newCachedThreadPool(); //ThreadPool for (int i = 0; i < threadNumber; i++) { //10 threads are added to the thread pool ScanMethod scanMethod2 = new ScanMethod(ip, portSet,threadNumber, i, timeout); threadPool.execute(scanMethod2); } threadPool.shutdown(); while (true) { if (threadPool.isTerminated()) { System.out.println("Scan end"); break; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }// end of while } /* * Scan method: Scan for a List collection of ports to be scanned*/ private class ScanMethod implements Runnable { private String ip; // Target IP private List<Integer> portList; // List collection of ports to be scanned private int threadNumber, serial, timeout; // Number of threads, which thread is the number of threads, timeout public ScanMethod(String ip, List<Integer> portList, int threadNumber,int serial, int timeout) { this.ip = ip; this.portList = portList; this.threadNumber = threadNumber; this.serial = serial; this.timeout = timeout; } public void run() { int port = 0; Integer[] ports = portList.toArray(new Integer[portList.size()]); // List to array try { InetAddress address = InetAddress.getByName(ip); //If you enter the host name, try to get the ip address Socket socket;//Define socket SocketAddress socketAddress;//Pass ip and port if (ports.length < 1) //If the array has no elements, return will not be executed; for (port = 0 + serial; port <= ports.length - 1; port += threadNumber) { //10 threads run each time socket = new Socket(); //Allocate memory space for the object socketAddress = new InetSocketAddress(address, ports[port]); try { socket.connect(socketAddress, timeout); //Connect the specified port of the target host, and the connection fails after timeout socket.close(); //Close the port System.out.println("port" + ports[port] + ":open"); jta.append("port" + ports[port] + " : open /n"); //Update the message in the text area} catch (IOException e) { System.out.println("port" + ports[port] + " : close"); jta.append("port" + ports[port] + " : close /n"); //The exception is generated to indicate that the port is closed} } } catch (UnknownHostException e) { e.printStackTrace(); } } //end of run() }//end of ScanMethod public JPanel getPanel() { JPanel jp = new JPanel(); jp.add(jb1, BorderLayout.CENTER); jp.setBorder(BorderFactory.createRaisedBevelBorder()); jp.setBackground(Color.lightGray); return jp; } public JPanel getPanel2() { JPanel jp = new JPanel(); JLabel jl = new JLabel(); jl.setText("Please enter the host name or ip address, and the common port number of the host will be scanned: "); jp.add(jl); jp.add(jtf); jp.setBorder(BorderFactory.createRaisedBevelBorder()); jp.setBackground(Color.LIGHT_GRAY); return jp; } public JPanel getPanel3() { JLabel jl1 = new JLabel(str1); JLabel jl2 = new JLabel(str2); JLabel jl3 = new JLabel(str3); JLabel jl4 = new JLabel(str4); JLabel jl5 = new JLabel(str5); JLabel jl6 = new JLabel(str6); JPanel jp = new JPanel(); jp.setLayout(new GridLayout(6, 1)); jp.add(jl1); jp.add(jl2); jp.add(jl3); jp.add(jl4); jp.add(jl5); jp.add(jl6); jp.setBorder(BorderFactory.createEtchedBorder()); //Etching border return jp; } public static void main(String[] args) { new ScanPort(); }}For more information about Java related content, please check out the topics of this site: "Summary of Java Network Programming Skills", "Summary of Java Socket Programming Skills", "Summary of Java File and Directory Operation Skills", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.