js|端口扫描
星期5晚上写了的JSP实现扫描对方的端口...一开始测试比较麻烦~~因为要使用多线程就需要extends Thread或者implemets Runnable 也就是说需要一个类~~ 在JSP 里面写一个类,不就是成员内部类咯!哎郁闷啊!之后写完再测试~~ 又发现问题了!要是当前JSP的线程执行完了就会自动闭合out对象 然而我其他线程的out内容就会没法输入~~郁闷ing... 后来想了一个 没办法中的办法!就是在当前线程睡觉一会Thread.sleep() 废话少说了~~下面给出代码!
//扫描port函数
void scanPort(javax.servlet.jsp.JspWriter out,String strAddress,int startPort,int endPort)throws Exception{
if(endPort<startPort||startPort<=0||startPort>65535||endPort>65535||endPort<=0){
out.print("port setup error");
return;
}
InetAddress ia=InetAddress.getByName(strAddress);
for(int p=startPort;p<=endPort;p+=15){
(new ScanPort(ia,p,p+14,out)).start();
}
Thread.sleep((int)(endPort/startPort)*5000);
}//扫描类
class ScanPort extends Thread{
int startPort;
int endPort;
InetAddress address;
javax.servlet.jsp.JspWriter out;
public ScanPort(InetAddress address,int startPort,int endPort,javax.servlet.jsp.JspWriter out){
this.address=address;
this.startPort=startPort;
this.endPort=endPort;
this.out=out;
}
public void run(){
Socket s=null;
for(int port=startPort;port<=endPort;port++){
try{
s=new Socket(address,port);
out.println("port "+port+" is Open
");
}
catch(IOException e){
}finally{
try{s.close();}catch(Exception e){}
}
}
}
}