首页
论坛
图书
开发资料
在线文档
网址
下载
联系我们
 新闻│Java│JavaScript│Eclipse│Eclipse 英文│J2EE│J2ME│J2SE│JSP│Netbeans│Hibernate│JBuilder│Spring│Struts
站内搜索: 请输入搜索关键词

当前页面: 开发资料首页 → Java 专题 → java实现的简单的端口扫描程序

java实现的简单的端口扫描程序

摘要: java实现的简单的端口扫描程序

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="332" height="86" align="center" valign="top"> </td> <td width="352" valign="top">
怎么样扫描计算机系统本地和远程的端口,监测其是打开还是关闭的在很多应用程序中都要用到,下面是用java实现的简单的端口扫描程序,在局域网中使用还不错。扫描结果会输出到文件到控制台。</td> </tr> <tr> <td height="44" colspan="2">

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
*/

</td> </tr> </table>

import java.io.*;

import java.net.*;

import java.util.*;

public class SocketPort {

public static void main(String[] args) {

 String ip = "10.10.1.1";

 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 ");

    System.out.println("Information Of The Port On the " + hostname +"computer ");

    fout.println();

 // do ports scan

 for(int nport = 24;nport <= 60;++nport){

   try{

   Socket s = new Socket(hostname,nport);

   fout.println("The port " + nport + " is open!");

   System.out.println("The port " + nport + " is open!");

   fout.println("Connected to "+ s.getInetAddress() + " on port " + s.getPort() + " from port "+
s.getLocalPort() + " of " + s.getLocalAddress()); System.out.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!"); System.out.println("The port " + nport + " is closed!"); } } fout.close(); } catch(IOException e){} } }
下面是程序运行结果:

C:\java>java SocketPort
/10.10.1.1
ZZ3ZONE
Information Of The Port On the ZZ3ZONEcomputer
The port 24 is closed!
The port 25 is open!
Connected to ZZ3ZONE/10.10.1.1 on port 25 from port 4868 of /10.10.1.99
The port 26 is closed!
The port 27 is closed!
The port 28 is closed!
The port 29 is closed!
The port 30 is closed!
The port 31 is closed!
The port 32 is closed!
The port 33 is closed!
The port 34 is closed!
The port 35 is closed!
The port 36 is closed!
The port 37 is closed!
The port 38 is closed!
The port 39 is closed!
The port 40 is closed!
The port 41 is closed!
The port 42 is open!
Connected to ZZ3ZONE/10.10.1.1 on port 42 from port 1177 of /10.10.1.99
The port 43 is closed!
The port 44 is closed!
The port 45 is closed!
The port 46 is closed!
The port 47 is closed!
The port 48 is closed!
The port 49 is closed!
The port 50 is closed!
The port 51 is closed!
The port 52 is closed!
The port 53 is open!
Connected to ZZ3ZONE/10.10.1.1 on port 53 from port 1313 of /10.10.1.99
The port 54 is closed!
The port 55 is closed!
The port 56 is closed!
The port 57 is closed!
The port 58 is closed!
The port 59 is closed!
The port 60 is closed!

C:\java>


function TempSave(ElementID) { CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value); CommentsPersistDiv.save("CommentXMLStore"); } function Restore(ElementID) { CommentsPersistDiv.load("CommentXMLStore"); document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent"); } </td> </tr> <tr>


↑返回目录
前一篇: 深入理解Java对象实例生成的例子
后一篇: jdk1.5中的元数据一例

首页 | 全站 Sitemap | 联系我们 | 设为首页 | 收藏本站
版权所有 Copyright © 2006-2007, Java 编程资料牛鼻站, All rights reserved