import java.net.*;
import java.io.*;
public class PortScanner {
public static void main(String args[]){
String host="localhost";
new PortScanner().scan(host);
}
public void scan(String host){
Socket socket=null;
System.out.println("scanning...");
for(int port=1;port<65535;port++){
try {
//socket=new Socket(host,port);
socket=new Socket();
SocketAddress remoteAddr=new InetSocketAddress(host,port);
socket.connect(remoteAddr,1);//超时时间1ms
System.out.println("there is a server on port "+port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
//System.out.println("connot connect to port"+port);
}finally{
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
System.out.println("scan over.");
}
}