端口扫描程序java实现

[入库:2005年8月19日] [更新:2007年3月25日]

本文简介:选择自 whandeycn 的 blog

怎么样扫描计算机系统本地和远程的端口,监测其是打开还是关闭的在很多应用程序中都要用到,下面是用java实现的简单的端口扫描程序。

source code:


/*
 * created on 2005-3-22
 *
 * todo to change the template for this generated file go to
 * window - preferences - java - code style - code templates
 */

/**
 * @author whandey    connect to me:
whandey@163.com
 *
 * todo to change the template for this generated type comment go to
 * window - preferences - java - code style - code templates
 */
import java.io.*;
import java.net.*;
import java.util.*;


public class socketport {

 public static void main(string[] args) {
  
  string ip = "159.162.39.27";   
  string hostname = new string();
  
     try{ //get the target ip address and hostname
   inetaddress address = inetaddress.getbyname(ip);
   system.out.println(address);
   hostname = address.gethostname();
   system.out.println(hostname);
   }
   catch(unknownhostexception e)
   {
    system.out.println("could not find "+ ip);
    
    }
  
  
  try
  {   // creat the output file
   printwriter fout = new printwriter( new filewriter("portinf.txt"));
   fout.println("information of the port on the " + hostname +"computer ");
   fout.println();
  
  // do ports scan
  for(int nport = 25;nport < 30;++nport)
  {
   try
  {
   
   socket s = new socket(hostname,nport);
   fout.println("the port " + nport + " is open!");
   
   fout.println("connected to "+ s.getinetaddress() + " on port " + s.getport() + " from port "+ s.getlocalport() + " of " + s.getlocaladdress());
   //print the connected socket information
   }
   
   catch(ioexception e)
   {
    fout.println("the port " + nport + " is closed!");
    }
   
  }
  fout.close();
  
  }
  catch(ioexception e){}
  
 }
}


本文关键:端口扫描程序java实现
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top