Java writing a port scanner

Source: Internet
Author: User
Tags gettext

Long time did not write Java, learning, also did not learn network programming this piece, inadvertently saw a book, so a little review of Java, write a port scanner, play it, online this little public has is, is his boring write play.

The source code is as follows:

A total of two categories, relatively simple Austrian

Importjavax.swing.*;Importjava.awt.Dimension;ImportJava.awt.Font;ImportJava.awt.Toolkit;Importjava.awt.event.*; Public classPortscanextendsJFrame {JPanel Center=NewJPanel (); Box hBox0=Box.createhorizontalbox (); Box HBox1=Box.createhorizontalbox (); Box HBox2=Box.createhorizontalbox (); Box HBox3=Box.createhorizontalbox (); PrivateJLabel state=NewJLabel ("Off"); PrivateJLabel host=NewJLabel ("Host name:"); PrivateJLabel port=NewJLabel ("Port number:"); PrivateJLabel state=NewJLabel ("Status:"); PrivateJButton btn1=NewJButton ("Start Scan"); PrivateJButton reset=NewJButton ("Reset"); PrivateJTextField host=NewJTextField (20); PrivateJTextField port=NewJTextField (7); Font F1=NewFont ("Official script", font.plain,15);  Public Static voidMain (string[] args) {JFrame Portscan=NewPortscan (); Portscan.setvisible (true); }         PublicPortscan () {Settitle ("Portscanner"); SetSize (300, 200); Center.setlayout (Newboxlayout (Center,boxlayout.y_axis)); Toolkit Kit=Toolkit.getdefaulttoolkit (); Dimension screensize= Kit.getscreensize ();//Get screen resolutionSetLocation (SCREENSIZE.WIDTH/4,SCREENSIZE.HEIGHT/4);//locationState.setfont (F1);          Host.setfont (F1);          Port.setfont (F1);          Btn1.setfont (F1);          Reset.setfont (F1);         State.setfont (F1);         Center.add (hBox0); Center.add (Box.createrigidarea (NewDimension (10,20)));         Center.add (HBox1); Center.add (Box.createrigidarea (NewDimension (2,20)));         Center.add (HBOX2); Center.add (Box.createrigidarea (NewDimension (2,20)));         Center.add (HBOX3);         Hbox0.add (HOST);         Hbox0.add (host);         Hbox1.add (PORT);         Hbox1.add (port);         Hbox2.add (BTN1);         Hbox2.add (reset);         Hbox3.add (state);         Hbox3.add (state);         Add (center); Btn1.addactionlistener (NewMyactionlistener ()); Reset.addactionlistener (NewMyactionlistener ()); }    classMyactionlistenerImplementsactionlistener{@Override Public voidactionperformed (ActionEvent e) {String post1=Host.gettext (); intport1=Integer.parseint (Port.gettext ()); if(E.getactioncommand () = = "Start Scan"){            if(Portscannerbyip.scan (Post1, Port1)) {State.settext (Open); }Else{State.settext (Close); }                            }            if(E.getactioncommand () = = "Reset") {Host.settext (""); Port.settext (""); }        }                    }}
Importjava.io.IOException;ImportJava.net.Socket; Public classPortscannerbyip { Public Static BooleanScan (String host,intPort) {        Booleanflag=true; Socket Socket=NULL; Try{Socket=NewSocket (Host,port); returnFlag; } Catch(IOException e) {flag=false; returnFlag; }finally{            Try {                if(socket!=NULL) Socket.close (); } Catch(IOException e) {e.printstacktrace (); }        }    }}

Run:

Java writing a port scanner

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.