首页 > 程序开发 > 综合编程 > 安全编程 >

多线程端口扫描器代码

2008-11-18

import java.io.*;d6Vimport java.net.*;d6V public class scan {d6V    public static void main(String args[])d6V    {d6V        int Thread_Num=50;  

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

public class scan {d6V
public static void main(String args[])d6V
{d6V
int Thread_Num=50; //设置默认线程数50d6V
String host="localhost";d6V
int minPort;d6V
int maxPort;d6V
int portNum;d6V

ScanPort sp[]=new ScanPort[Thread_Num];d6V

minPort=Integer.parseInt(args[1]);d6V
maxPort=Integer.parseInt(args[2]);d6V

if(args.length == 3 && minPort<maxPort)d6V
{d6V
host=args[0];d6V
minPort=Integer.parseInt(args[1]);d6V
maxPort=Integer.parseInt(args[2]);d6V
InetAddress thisComputer;d6V
byte[] address;d6V

//获取IP地址的字节d6V
tryd6V
{d6V
thisComputer=InetAddress.getByName(host);d6V
address=thisComputer.getAddress();d6V
}d6V

catch(UnknownHostException e)d6V
{d6V
System.out.println("Cannot find host"+host);d6V
}d6V
}d6V

if (args.length == 4 && minPort<maxPort)d6V
{d6V
host=args[0];d6V
minPort=Integer.parseInt(args[1]);d6V
maxPort=Integer.parseInt(args[2]);d6V
Thread_Num=Integer.parseInt(args[3]); //自定义线程数d6V
InetAddress thisComputer;d6V
byte[] address;d6V

//获取IP地址的字节d6V
tryd6V
{d6V
thisComputer=InetAddress.getByName(host);d6V
address=thisComputer.getAddress();d6V
}d6V

catch(UnknownHostException e)d6V
{d6V
System.out.println("Cannot find host"+host);d6V
}d6V
}d6V

//--- 计算待扫瞄端口量 ---//d6V
portNum=maxPort-minPort+1;d6V

if(portNum < Thread_Num)d6V
{d6V
//--- 如果端口数小于总线程 ---//d6V
for(int i=0; i<portNum; i++)d6V
{d6V
sp[i]=new ScanPort(host,minPort+i,minPort+i,i);d6V
sp[i].start();d6V
}d6V
}d6V
elsed6V
{d6V
int startPort = minPort, endPort = minPort;d6V
int A = portNum / Thread_Num ;d6V
int B = portNum % Thread_Num;d6V

for(int i=0; i<Thread_Num; i++)d6V
{d6V
if(B <= Thread_Num)d6V
{d6V
startPort = endPort;d6V
if ( i!= 0)d6V
{d6V
startPort += 1;d6V
endPort = startPort + A -1;d6V
}d6V

if ( i < B)d6V
endPort += 1;d6V

sp[i]=new ScanPort(host,startPort,endPort,i);d6V
sp[i].start();d6V
}d6V
elsed6V
{d6V
&

相关文章
最新文章
热点推荐