您的位置:首页 > 其它

获取局域网内ip

2013-04-04 23:53 113 查看
获取局域网内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 blocke.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() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubInetAddress address = null;try {address = InetAddress.getLocalHost();// 获取本机InetAddress对象String hostAddress = address.getHostAddress();// 获取本机ipint 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 blocke1.printStackTrace();}}});buttons[1].addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSystem.exit(0);}});}public static void main(String[] args) {new GainAllIp().gainIp();}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: