import java.net.*;
import java.applet.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class PortScan extends java.applet.Applet
{
private String IPAddress;
private String PortList;
private int Port;
public String result;
public String errorPort;
public String version;
public String pingResult;
public boolean analyseAppletArgs()
{
String ip = getParameter(“ip”);
if (ip != null) {
this.IPAddress=ip;
}else{
this.result=”IP is Null”;
return false;
}
String port = getParameter(“port”);
if (port != null) {
this.PortList=port;
}else{
this.result=”PortList is Null”;
return false;
}
return true;
}
public static void main(char args[]){
}
public void run()
{
this.version=System.getProperty(“java.version”);
this.result=”";
String rs = “”;
StringBuffer s1=new StringBuffer();
StringBuffer s2=new StringBuffer();
char find=’`';
int count=0;
for(int i=0;i<this.PortList.length();i++){
if(this.PortList.charAt(i)==find)
count++;
}
String[] portArray = new String[count-1];
String port80Result = “false”;
portArray= this.PortList.split(“`”);
for (int i = 0; i < portArray.length; i++){
rs = this.scanPort(this.IPAddress, portArray[i]);
if (rs !=”ok”){
rs=”false”;
}
if (s1.length()>0)
{
s1.append(“`”);
}else{
port80Result = rs;
}
s1.append(rs);
}
this.result=s1.toString();
if (port80Result == “ok”){
for (int i=0; i<6; i++){
rs = this.doPing(this.IPAddress, Integer.valueOf(portArray[0]));
if (s2.length()>0){
s2.append(“`”);
}
s2.append(rs);
}
this.pingResult=s2.toString();
}else{
this.pingResult=port80Result;
}
}
public String scanPort(String ip, String port)
{
if (ip != null) {
this.IPAddress=ip;
}else{
return “ip is Null”;
}
if (port != null) {
this.Port=Integer.valueOf(port);
}else{
return “port is Null”;
}
try
{
Socket TestPort = new Socket(); //if this port cannot been connected, throw a exception.
TestPort.connect(new InetSocketAddress(this.IPAddress, this.Port), 3000);
TestPort.setSoTimeout(3000);
//TestPort.getInputStream().read();
TestPort.close();
return result=”ok”; //if connected, print ok.
}
catch(Exception e)
{
return e.getMessage(); //if not connected, print failed.
}
}
public String doPing(String hostName, int Port){
try{
Socket socket = new Socket(hostName, Port); //if this port cannot been connected, throw a exception.
socket.setSoTimeout(1500);
long lasting = System.currentTimeMillis();
long runtime;
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), “UTF8″));
out.write(“GET / HTTP/1.1\r\n”);
out.write(“Connection: Close\r\n”);
out.write(“User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n”);
out.write(“Cache-Control: no-cache\r\n”);
out.write(“Host: “+hostName+”\r\n”);
out.write(“Accept: */*\r\n”);
out.write(“\r\n”);
out.write(“\r\n”);
out.flush();
// 读取[du qu]返回信息[xin xi]
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), “UTF-8″));
String line;
while((line = in.readLine()) != null) {
//System.out.println(line);
break;
}
out.close();
in.close();
runtime = System.currentTimeMillis() – lasting;
Thread.sleep(500);
socket.close();
return Long.toString(runtime);
}catch(Exception e){
return “flase”;
}
}
public void init()
{
if (analyseAppletArgs())
{
run();
}
}
}