java获取内网的所有IP地址.doc_第1页
java获取内网的所有IP地址.doc_第2页
java获取内网的所有IP地址.doc_第3页
java获取内网的所有IP地址.doc_第4页
java获取内网的所有IP地址.doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JAVA获取内网所有IP地址程序代码: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 .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;/* * author 张振坤 * 获得内网的所有IP地址 */SuppressWarnings(serial)public class gainallip extends JFrame private JTextArea ta_allIp; static public Hashtable pingMap; / 用于存储所ping的IP是否为内网IP的集合 /* * Launch the application * * param args */ public static void main(String args) gainallip frame = new gainallip(); 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 gainallip() super(); addWindowListener(new WindowAdapter() public void windowOpened(final WindowEvent e) try gainAllIp(); ta_allIp.setText(null); catch (Exception e1) 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) 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); final JButton button_1 = new JButton(); button_1.setText(New JButton); panel.add(button_1); 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是要发送的回显请求数 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,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论