⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scanner.java

📁 实现了
💻 JAVA
字号:
import java.net.*;
import java.io.*;

public class Scanner implements Runnable
{
 private InetAddress IPAddress;
 private int Port;
 private Thread MultipleThread;

 public Scanner(String IPAddress, int port)
 {
  try
  {
   this.IPAddress = this.IPAddress.getByName(IPAddress); //判断主机是否合法。
   this.Port = port;
  }
  catch(Exception e)
  {
   System.out.println("The host does not exist.");
   System.exit(0);
  }
 }

 public void run()
 {

  try
  {
   Socket TestPort = new Socket(this.IPAddress, this.Port); //,如果连接成功,说明书是连同的。if this port cannot been connected, throw a exception.
   System.out.println(this.IPAddress+" --> "+this.Port + " is open"); //if connected, print out.
  }
  catch(Exception e)
  {  
    //System.out.println(this.IPAddress+" --> "+this.Port + " is closed");       
  }
 }

 public static void main(String args[])
 {
 while(true){
  String str="";
  str += "\n\nAuthor: xzh (E_mail:zhaohui923@hotmail.com)\n";
  str += "Choice:\n";
  str += "   SinglePort -->0     MultiplePort-->1";
  
  System.out.println(str);
   int SMPOrt=1;
   int singleport=0;
   int startport=0;
   int endport=0;
   String hostname="";
   
  
   System.out.println("Please enter your choice: ");
   try
   {
   	SMPOrt=System.in.read();
   	System.in.skip(System.in.available());  //skip the byte which is not readed
   }
   catch(IOException e)
   {
   	System.err.println(e);
   }
   
  switch(SMPOrt)
   {
    case '1':    //scan the multipleport
    {
    
     System.out.println("Please enter the IP or HostName:");
     try
      {
       BufferedReader in=new  BufferedReader(new InputStreamReader(System.in));
   	   hostname=in.readLine();
       }
     catch(IOException e)
       {
   	  System.err.println(e);
       }
       
       
     System.out.println("Please enter the StartPort: ");
     try
      {
   	  //startport=System.in.read();
   	  BufferedReader in=new  BufferedReader(new InputStreamReader(System.in));
   	  startport=Integer.parseInt(in.readLine());
      }
     catch(IOException e)
      {
   	  System.err.println(e);
      }
      
     System.out.println("Please enter the EndPort: ");
     try
      {
   	  //endport=System.in.read();
   	  BufferedReader in=new  BufferedReader(new InputStreamReader(System.in));
   	  endport=Integer.parseInt(in.readLine());
      }
     catch(IOException e)
      {
   	  System.err.println(e);
      }
    System.out.println("The HostName : "+hostname+"\t"+"StartPort : "+startport+"\t"+"EndPort : "+endport);	
      
      //if(endport-startport+1>=1000)
    System.out.println("The process is deply on your computer and the netware,Please Waiting...");
    
    System.out.println("HostName/IP"+"\t\t"+"Open Port"+"\t\t"+"Port Information");
     for(int i=startport;i<=endport;i++)
     {
      Scanner mainScanner = new Scanner(hostname,i);
      if(mainScanner.MultipleThread.activeCount()>100) //activeCount()方法返回当前线程的线程组中活动线程的数目
      {
       mainScanner.run();
      }
      else
      {
       mainScanner.MultipleThread = new Thread(mainScanner);
       mainScanner.MultipleThread.start();
      }
     }
     System.out.println("Scan Complete...");
     break;
    }
    
    case '0':   //scan the single port
    { 
    
     System.out.println("Please enter the IP or HostName: ");
     try
      {
      BufferedReader in=new  BufferedReader(new InputStreamReader(System.in));
   	  hostname=in.readLine();
      }
     catch(IOException e)
      {
   	  System.err.println(e);
      }
     
     System.out.println("Please enter port: ");
     try
     {
   	  //singleport=System.in.read();
   	  BufferedReader in=new  BufferedReader(new InputStreamReader(System.in));
   	  singleport=Integer.parseInt(in.readLine());
     }
     catch(IOException e)
     {
   	  System.err.println(e);
     }
     
     System.out.println("The HostName : "+hostname+"\t"+"ScanPort : "+singleport);
     
     System.out.println("The process is deply on your computer and the netware,Please Waiting...");
     
     System.out.println("HostName/IP"+"\t\t"+"Open Port"+"\t\t"+"Port Information");
     Scanner mainScanner = new Scanner(hostname,singleport); //参数传给构造方法
     
     try
     {
      mainScanner.run();
      System.out.println("Scan Complete...");
     }
     catch(Exception e)
     {
      System.out.println("parameter error");
     }
     break;
     
    }
    default:
     System.out.println(str); //输出提示信息,如果选择的不是0、1.
   }
  }
 }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -