Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

question in java

  Asked By: Alma    Date: Oct 15    Category: Java    Views: 633
  

in my java applecation i want to scan all ports to get all free ports and used ports and who use this port

if there are any methods in java do what i wont please tell me and if not please tell me too

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Deloris Harris     Answered On: Oct 15

Here is a simple port  scanner that scan  all ports:


import java.net.*;
import java.io.*;

public class ScanPort {

public static void main(String args[]) {
for (int portNumber = 0; portNumber < 65536; portNumber++) {
try {
System.out.println("checking for port " + portNumber + " availability");

/*create a ServerSocket */
ServerSocket serverSocket = new ServerSocket(portNumber);

/*Available portNumbers*/
System.out.println("checking for port " + portNumber + " availability, succeeded");

/*if port number is occupied an IOException will be throwen*/
catch (IOException x) {
System.out.println("ERROR MESSAGE: checking for port " + portNumber +
" availability, failed");
}
}
}
}

see also:
http://java.sun.com/j2se/1.5.0/docs/api/java/net/ServerSocket.html

 
Answer #2    Answered By: Luisa Fischer     Answered On: Oct 15

Here comes mine...
check out this code


import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class PortScanner extends Frame{



Socket s;
int startPort,endPort;
Label l1;
TextField hostName,fromPort,toPort;
TextArea log;
Label host,from,to,label;
Button scan,reset;
int fp,tp;
String h;
static boolean close=false;
Dialog d;


public PortScanner(){

Frame f=new Frame("Port Scanner");

f.setVisible(true);
f.setLayout(new GridLayout(5,1));

l1=new Label("Port Scanner version 1.0",Label.CENTER);
l1.setForeground(Color.blue);
l1.setFont(new Font("TimesRoman",Font.BOLD,20));
f.add(l1);

Panel p1=new Panel(new GridLayout(3,3));
host=new Label("Host Name:");
host.setForeground(Color.blue);
p1.add(host);
hostName=new TextField(15);
p1.add(hostName);
from=new Label("From Port:");
from.setForeground(Color.blue);
p1.add(from);
fromPort=new TextField(15);
p1.add(fromPort);
to=new Label("To Port:");
to.setForeground(Color.blue);
p1.add(to);
toPort=new TextField(15);
p1.add(toPort);

f.add(p1);

Panel p2=new Panel();
scan=new Button("Scan Now");
scan.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent r){
if(hostName.getText().equals("")){log.setText("Please complete all the
required fields");
return;
}
else if (fromPort.getText().equals("")){log.setText("Please complete
all the required fields");
return;}
else if(toPort.getText().equals("")){log.setText("Please complete
all the required fields");
return;}

close=false;
Thread run1=new Thread(){
public void run(){

scan.setEnabled(false);
reset.setLabel("Stop");
log.setText("");
log.repaint();

h=hostName.getText();
fp=Integer.parseInt(fromPort.getText());
tp=Integer.parseInt(toPort.getText());

for(int i=fp;i<=tp;i++){
label.setText("Port "+i+" is being tested");
if(close)
break;
try{
s=new Socket(h,i);
log.append("Port "+i+" is open."+"\n");
log.repaint();
s.close();
}
catch(Exception er){continue;}

}
scan.setEnabled(true);
reset.setLabel("Reset");
label.setText("Press scan  to start.");
}
};
run1.start();
}
});



reset=new Button("Reset");
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent t){
Thread run2=new Thread(){
public void run(){
close=true;
hostName.setText("");
fromPort.setText("");
toPort.setText("");
}
};
run2.start();
}
});





Label empty=new Label();
p2.add(scan);
p2.add(empty);
p2.add(reset);
f.add(p2);

log=new TextArea();
f.add(log);

label=new Label("Press Scan to start.");
label.setForeground(Color.blue);
f.add(label);

f.setSize(320,460);
//f.show(true);
f.repaint();
f.setResizable(false);

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});


}

public static void main(String args[]){

new PortScanner();
}

 
Didn't find what you were looking for? Find more on question in java Or get search suggestion and latest updates.




Tagged: