Java實(shí)現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址
題目描述
在進(jìn)行網(wǎng)絡(luò)編程時(shí),有時(shí)需要對局域網(wǎng)的所有主機(jī)進(jìn)行遍歷,為此需要獲得內(nèi)網(wǎng)的所以IP地址
題目實(shí)現(xiàn):獲得內(nèi)網(wǎng)的所有IP地址的小應(yīng)用。
解題思路
創(chuàng)建一個(gè)類:GainAlllpFrame,繼承JFrame窗體類
定義一個(gè)gainAlllp()方法:用于獲得所有IP,并顯示在文本域中的方法
定義一個(gè)內(nèi)部類Pinglp Thread,且是線程類。用于判斷給定IP是否為內(nèi)網(wǎng)IP的線程對象
線程類的執(zhí)行邏輯是對指定的IP進(jìn)行ping 訪問
獲得本機(jī)的IP地址和網(wǎng)段
InetAddress host = InetAddress.getLocalHost();// 獲得本機(jī)的InetAddress對象
String hostAddress = host.getHostAddress();// 獲得本機(jī)的IP地址
int pos = hostAddress.lastIndexOf(".");// 獲得IP地址中最后一個(gè)點(diǎn)的位置
String wd = hostAddress.substring(0, pos + 1);// 對本機(jī)的IP進(jìn)行截取,獲得網(wǎng)段
ping***指定的IP地址,獲取ping**結(jié)果
// 獲得所ping的IP進(jìn)程,-w 280是等待每次回復(fù)的超時(shí)時(shí)間,-n 1是要發(fā)送的回顯請求數(shù)
Process process = Runtime.getRuntime().exec(
"ping " + ip + " -w 280 -n 1");
InputStream is = process.getInputStream();// 獲得進(jìn)程的輸入流對象
InputStreamReader isr = new InputStreamReader(is);// 創(chuàng)建InputStreamReader對象
BufferedReader in = new BufferedReader(isr);// 創(chuàng)建緩沖字符流對象
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運(yùn)行
代碼詳解
引入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 小王同學(xué)
* @version 1.0
*/
class GainAllIpFrame extends JFrame {
private JTextArea ta_allIp;
static public Hashtable<String, String> pingMap; // 用于存儲所ping的IP是否為內(nèi)網(wǎng)IP的集合
public static void main(String args[]) {
GainAllIpFrame frame = new GainAllIpFrame();
frame.setVisible(true);
}
public void gainAllIp() throws Exception {// 獲得所有IP,并顯示在文本域中的方法
InetAddress host = InetAddress.getLocalHost();// 獲得本機(jī)的InetAddress對象
String hostAddress = host.getHostAddress();// 獲得本機(jī)的IP地址
int pos = hostAddress.lastIndexOf(".");// 獲得IP地址中最后一個(gè)點(diǎn)的位置
String wd = hostAddress.substring(0, pos + 1);// 對本機(jī)的IP進(jìn)行截取,獲得網(wǎng)段
for (int i = 1; i <= 255; i++) { // 對局域網(wǎng)的IP地址進(jìn)行遍歷
String ip = wd + i;// 生成IP地址
PingIpThread thread = new PingIpThread(ip);// 創(chuàng)建線程對象
thread.start();// 啟動(dòng)線程對象
}
Set<String> set = pingMap.keySet();// 獲得集合中鍵的Set視圖
Iterator<String> it = set.iterator();// 獲得迭代器對象
while (it.hasNext()) { // 迭代器中有元素,則執(zhí)行循環(huán)體
String key = it.next(); // 獲得下一個(gè)鍵的名稱
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("獲得內(nèi)網(wǎng)的所有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, "應(yīng)用程序異常,請?jiān)僭囈淮巍?);
}
}
});
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是否為內(nèi)網(wǎng)IP的線程對象
public String ip; // 表示IP地址的成員變量
public PingIpThread(String ip) {// 參數(shù)為需要判斷的IP地址
this.ip = ip;
}
public void run() {
try {
// 獲得所ping的IP進(jìn)程,-w 280是等待每次回復(fù)的超時(shí)時(shí)間,-n 1是要發(fā)送的回顯請求數(shù)
System.out.println("嘗試ping IP:"+ip);
Process process = Runtime.getRuntime().exec(
"ping " + ip + " -w 280 -n 1");
InputStream is = process.getInputStream();// 獲得進(jìn)程的輸入流對象
InputStreamReader isr = new InputStreamReader(is);// 創(chuàng)建InputStreamReader對象
BufferedReader in = new BufferedReader(isr);// 創(chuàng)建緩沖字符流對象
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實(shí)現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址的詳細(xì)內(nèi)容,更多關(guān)于Java獲取內(nèi)網(wǎng)IP地址的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一篇文章帶你解決 IDEA 每次新建項(xiàng)目 maven home directory 總是改變的問題
這篇文章主要介紹了一篇文章帶你解決 IDEA 每次新建項(xiàng)目 maven home directory 總是改變的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Spring事務(wù)控制策略及@Transactional失效問題解決避坑
這篇文章主要為大家介紹了Spring事務(wù)控制策略及@Transactional失效問題解決避坑,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
java代碼實(shí)現(xiàn)C盤文件統(tǒng)計(jì)工具
今天周末,給大家分享基于java代碼實(shí)現(xiàn)C盤文件統(tǒng)計(jì)工具,在這小編使用的版本是Maven-3.9.9,jdk1.8,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07
手把手帶你理解java線程池之工作隊(duì)列workQueue
這篇文章主要介紹了java線程池之工作隊(duì)列workQueue,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
java語言實(shí)現(xiàn)權(quán)重隨機(jī)算法完整實(shí)例
這篇文章主要介紹了java語言實(shí)現(xiàn)權(quán)重隨機(jī)算法完整實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11
Java實(shí)現(xiàn)Random隨機(jī)數(shù)生成雙色球號碼
使用Random類是Java中用于生成隨機(jī)數(shù)的標(biāo)準(zhǔn)類,本文主要介紹了Java實(shí)現(xiàn)Random隨機(jī)數(shù)生成雙色球號碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
java開發(fā)AOP基礎(chǔ)JdkDynamicAopProxy
這篇文章主要為大家介紹了java開發(fā)AOP基礎(chǔ)JdkDynamicAopProxy源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

