获取局域网内ip

获取局域网内ip是进行局域网通信的必然要求,这里使用ping并解析其返回值获取局域网内ip
不过效率不高,,只是简单实现功能


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.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class GainAllIp extends JFrame {
	JTextPane text;
	Map<String, String> pingMap = new HashMap<String, String>();
	StringBuilder str = new StringBuilder();

	class PingIpThread extends Thread {
		public String ip;

		public PingIpThread(String ip) {
			this.ip = ip;
		}

		public void run() {
			try {			// 获取ip的进程,-w 280是等待每次回复的超时时间 -n 1发送回显请求数
				Process process = Runtime.getRuntime().exec(
						"ping " + ip + " -w 280 -n 1");
				InputStream is = process.getInputStream();
				InputStreamReader reader = new InputStreamReader(is, "gbk");
				BufferedReader in = new BufferedReader(reader);
				String line = in.readLine();
				while (line != null) {
					if (line != null && !line.equals("")) {
						//System.out.println(line);
						if (line.substring(0, 2).equals("来自")
								|| (line.length() > 10 && line.substring(0, 10)
										.equals("Reply from"))) {
							pingMap.put(ip, "true");
						}
					}
					line = in.readLine();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void gainIp() {
		this.setTitle("获取局域网全部ip");
		this.setVisible(true);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		this.setBounds(100, 100, 1000,500);
		this.setLayout(null);
		JButton[] buttons = new JButton[2];
		buttons[0] = new JButton("获取所有IP");
		buttons[1] = new JButton("退出");
		buttons[0].setBounds(10, 10, 150, 30);
		buttons[1].setBounds(180, 10, 90, 30);
		this.add(buttons[0]);
		this.add(buttons[1]);
		text = new JTextPane();
		text.setBounds(10, 50, 1000, 450);
		this.add(text);
		buttons[0].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				InetAddress address = null;
				try {
					address = InetAddress.getLocalHost();// 获取本机InetAddress对象
					String hostAddress = address.getHostAddress();// 获取本机ip
					int n = hostAddress.lastIndexOf(".");
					String wd = hostAddress.substring(0, n + 1);// 获取网段
					for (int i = 1; i <= 255; i++) {
						String ip = wd + i;
						PingIpThread thread = new PingIpThread(ip);
						thread.run();
						
					}
					Set<String> set = pingMap.keySet();
					Iterator<String> it = set.iterator();

					while (it.hasNext()) {
						String key = it.next();
						String value = pingMap.get(key);
						if (value.equals("true")) {
							System.out.println(key);
							str.append(key + "\n");
						}
					}
					if (str != null) {
						text.setText(str.toString());
					}
				} catch (UnknownHostException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		buttons[1].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
	}

	public static void main(String[] args) {
		new GainAllIp().gainIp();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值