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

网友投稿 331 2022-07-23


目录题目描述代码详解效果展示

题目描述

在进行网络编程时,有时需要对局域网的所有主机进行遍历,为此需要获得内网的所以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)

http:// .equals("Reply from"))) {// 判断是ping通过的IP地址

pingMap.put(ip, "true");// 向集合中添加IP

}

}

line = in.readLine();// 再读取信息

}

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

代码详解

引入hutool,pom.xml增加

cn.hutool

hutool-core

5.6.5

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 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 set = pingMap.keySet();// 获得集合中键的Set视图

Iterator 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 WindowEvhcYyfUQLent 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();

}

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(

hcYyfUQL "ping " + ip + " -w 280 -n 1");

InputStream is = process.getInputStream();// 获得进程的输入流对象

InputStreamReader isr = new InputStreamReader(is);// 创建InputStreamReader对象

BufferedReadhcYyfUQLer 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地址的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mybatis基础概念与高级应用小结
下一篇:Spring Boot 集成Elasticsearch模块实现简单查询功能
相关文章

 发表评论

暂时没有评论,来抢沙发吧~