温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java如何实现获取内网的所有IP地址

发布时间:2022-06-14 09:23:34 来源:亿速云 阅读:577 作者:iii 栏目:开发技术

本篇内容主要讲解“Java如何实现获取内网的所有IP地址”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java如何实现获取内网的所有IP地址”吧!

题目描述

在进行网络编程时,有时需要对局域网的所有主机进行遍历,为此需要获得内网的所以IP地址

题目实现:获得内网的所有IP地址的小应用。

解题思路

创建一个类:GainAlllpFrame,继承JFrame窗体类

定义一个gainAlllp()方法:用于获得所有IP,并显示在文本域中的方法

定义一个内部类Pinglp Thread,且是线程类。用于判断给定IP是否为内网IP的线程对象

线程类的执行逻辑是对指定的IP进行ping 访问

获得本机的IP地址和网段

 InetAddress host = InetAddress.getLocalHost();// 获得本机的InetAddress对象
        String hostAddress = host.getHostAddress();// 获得本机的IP地址
        int pos = hostAddress.lastIndexOf(".");// 获得IP地址中最后一个点的位置
        String wd = hostAddress.substring(0, pos + 1);// 对本机的IP进行截取,获得网段

ping***指定的IP地址,获取ping**结果

 // 获得所ping的IP进程,-w 280是等待每次回复的超时时间,-n 1是要发送的回显请求数
                Process process = Runtime.getRuntime().exec(
                        "ping " + ip + " -w 280 -n 1");
                InputStream is = process.getInputStream();// 获得进程的输入流对象
                InputStreamReader isr = new InputStreamReader(is);// 创建InputStreamReader对象
                BufferedReader in = new BufferedReader(isr);// 创建缓冲字符流对象
                String line = in.readLine();// 读取信息
                while (line != null) {
                    if (line != null && !line.equals("")) {
                        if (line.substring(0, 2).equals("来自")
                                || (line.length() > 10 && line.substring(0, 10)
                                .equals("Reply from"))) {// 判断是ping通过的IP地址
                            pingMap.put(ip, "true");// 向集合中添加IP
                        }
                    }
                    line = in.readLine();// 再读取信息
                }

注意:本题只适合在window运行

代码详解

引入hutool,pom.xml增加

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.6.5</version>
</dependency>

GainAllpFrame

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
 * Description:
 *
 * @author 小王同学
 * @version 1.0
 */
class GainAllIpFrame extends JFrame {
    private JTextArea ta_allIp;
    static public Hashtable<String, String> pingMap; // 用于存储所ping的IP是否为内网IP的集合
 
 
    public static void main(String args[]) {
        GainAllIpFrame frame = new GainAllIpFrame();
        frame.setVisible(true);
    }
 
    public void gainAllIp() throws Exception {// 获得所有IP,并显示在文本域中的方法
        InetAddress host = InetAddress.getLocalHost();// 获得本机的InetAddress对象
        String hostAddress = host.getHostAddress();// 获得本机的IP地址
        int pos = hostAddress.lastIndexOf(".");// 获得IP地址中最后一个点的位置
        String wd = hostAddress.substring(0, pos + 1);// 对本机的IP进行截取,获得网段
        for (int i = 1; i <= 255; i++) { // 对局域网的IP地址进行遍历
            String ip = wd + i;// 生成IP地址
            PingIpThread thread = new PingIpThread(ip);// 创建线程对象
            thread.start();// 启动线程对象
        }
        Set<String> set = pingMap.keySet();// 获得集合中键的Set视图
        Iterator<String> it = set.iterator();// 获得迭代器对象
        while (it.hasNext()) { // 迭代器中有元素,则执行循环体
            String key = it.next(); // 获得下一个键的名称
            String value = pingMap.get(key);// 获得指定键的值
            if (value.equals("true")) {
                ta_allIp.append(key + "\n");// 追加显示IP地址
            }
        }
    }
 
    /**
     * Create the frame
     */
    public GainAllIpFrame() {
        super();
        addWindowListener(new WindowAdapter() {
            public void windowOpened(final WindowEvent e) {
                try {
                    gainAllIp();
                    ta_allIp.setText(null);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    ta_allIp.setText(null);
                }
            }
        });
        setTitle("获得内网的所有IP地址");
        setBounds(400, 200, 270, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        final JScrollPane scrollPane = new JScrollPane();
        getContentPane().add(scrollPane, BorderLayout.CENTER);
 
        ta_allIp = new JTextArea();
        scrollPane.setViewportView(ta_allIp);
 
        final JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.NORTH);
 
        final JButton button_2 = new JButton();
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                try {
                    ta_allIp.setText(null);
                    gainAllIp();
                } catch (Exception e1) {
                    e1.printStackTrace();
                    ta_allIp.setText(null);
                    JOptionPane.showMessageDialog(null, "应用程序异常,请再试一次。");
                }
            }
        });
        button_2.setText("显示所有IP");
        panel.add(button_2);
 
        final JButton button = new JButton();
        button.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                System.exit(0);
            }
        });
        button.setText("退    出");
        panel.add(button);
 
        pingMap = new Hashtable<String, String>();
    }
 
    class PingIpThread extends Thread {// 判断给定IP是否为内网IP的线程对象
        public String ip; // 表示IP地址的成员变量
        public PingIpThread(String ip) {// 参数为需要判断的IP地址
            this.ip = ip;
        }
        public void run() {
            try {
                // 获得所ping的IP进程,-w 280是等待每次回复的超时时间,-n 1是要发送的回显请求数
                System.out.println("尝试ping IP:"+ip);
                Process process = Runtime.getRuntime().exec(
                        "ping " + ip + " -w 280 -n 1");
                InputStream is = process.getInputStream();// 获得进程的输入流对象
                InputStreamReader isr = new InputStreamReader(is);// 创建InputStreamReader对象
                BufferedReader in = new BufferedReader(isr);// 创建缓冲字符流对象
                String line =  IoUtil.read(is,"GBK");//CMD获取的值是GBK格式的
                //String line = in.readLine();// 读取信息
                if (line != null && !line.equals("")) {
                    if (line.indexOf("来自") >0 || line.indexOf("Reply from")>0) {// 判断是ping通过的IP地址
                        pingMap.put(ip, "true");// 向集合中添加IP
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

效果展示

Java如何实现获取内网的所有IP地址

到此,相信大家对“Java如何实现获取内网的所有IP地址”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI