Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 346|回复: 0

[网络编程学习]java实现的简单的端口扫描程序

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-31 00:00:15 | 显示全部楼层 |阅读模式
    怎么样扫描计算机系统本地和远程的端口,监测其是打开还是关闭的在很多应用程序中都要用到,下面是用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
    */
      
    1. import java.io.*;
    2. import java.net.*;
    3. import java.util.*;
    4. public class SocketPort {
    5. public static void main(String[] args) {
    6. String ip = "10.10.1.1";
    7. String hostname = new String();
    8. try{ //get the target ip address and hostname
    9.    InetAddress address = InetAddress.getByName(ip);
    10.    System.out.println(address);
    11.    hostname = address.getHostName();
    12.    System.out.println(hostname);
    13. }
    14. catch(UnknownHostException e){
    15.     System.out.println("Could not find "+ ip);
    16. }
    17. try{ // creat the output file
    18.     PrintWriter fout = new PrintWriter( new FileWriter("PortInf.txt"));
    19.     fout.println("Information Of The Port On the " + hostname +"computer ");
    20.     System.out.println("Information Of The Port On the " + hostname +"computer ");
    21.     fout.println();
    22. // do ports scan
    23. for(int nport = 24;nport <= 60;++nport){
    24.    try{
    25.    Socket s = new Socket(hostname,nport);
    26.    fout.println("The port " + nport + " is open!");
    27.    System.out.println("The port " + nport + " is open!");
    28.    fout.println("Connected to "+ s.getInetAddress() + " on port " + s.getPort() + " from port "+
    29.        s.getLocalPort() + " of " + s.getLocalAddress());
    30.    System.out.println("Connected to "+ s.getInetAddress() + " on port " + s.getPort() + " from port "+
    31.        s.getLocalPort() + " of " + s.getLocalAddress());
    32.   //print the connected socket information
    33. }
    34. catch(IOException e){
    35.    fout.println("The port " + nport + " is closed!");
    36.    System.out.println("The port " + nport + " is closed!");
    37. }
    38. }
    39. fout.close();
    40. }
    41. catch(IOException e){}
    42. }
    43. }
    复制代码
    下面是程序运行结果:

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











    源码下载:http://file.javaxxz.com/2014/10/31/000014968.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 19:05 , Processed in 0.351313 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表