星期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<br>");
}
catch(ioexception e){
}finally{
try{s.close();}catch(exception e){}
}
}
}
}
新闻热点
疑难解答