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