package org.sz.net;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class IPUtils {
/**
* 獲取所在內網所有IP
* @param args
* @throws InterruptedException
*/
public static void gainAllIp() throws InterruptedException {
final List<String> innerIp = Collections.synchronizedList(new ArrayList<String>());
final CyclicBarrier barrier = new CyclicBarrier(255, new IpShow(innerIp));
String hostAddress = getLocalIP();
int pos = hostAddress.lastIndexOf(".");
String wd = hostAddress.substring(0, pos + 1);
for (int i = 1; i <= 255; i++) {
String ip = wd + i;
PingIpThread thread = new IPUtils.PingIpThread(ip, barrier, innerIp);
thread.start();
}
}
public static class IpShow implements Runnable {
private List<String> innerIp;
public IpShow(List<String> innerIp) {
this.innerIp = innerIp;
}
@Override
public void run() {
for (String ip : innerIp) {
System.out.println(ip);
}
}
}
public static class PingIpThread extends Thread {
private String ip;
private CyclicBarrier barrier;
private List<String> list;
public PingIpThread(String ip, CyclicBarrier barrier, List<String> list) {
this.ip = ip;
this.barrier = barrier;
this.list = list;
}
public void run() {
try {
if (InetAddress.getByName(ip).isReachable(5000))
list.add(ip);
barrier.await();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}
public static String getLocalIP() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
}
public static String getLocalHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
}
public static String getCanonicalHostName() {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
}
public static String getIPFromDomainName(String domainName) {
try {
return InetAddress.getByName(domainName).getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
}
public static String getDomainNameFromIP(String ip) {
try {
return InetAddress.getByAddress(getIPBytes(ip)).getCanonicalHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
}
private static byte[] getIPBytes(String ip) {
byte[] ipBytes = new byte[4];
String[] ipStr = ip.split("[.]");
for (int i = 0; i < 4; i++) {
int m = Integer.parseInt(ipStr[i]);
byte b= (byte)(m & 0xff);
ipBytes[i] = b;
}
return ipBytes;
}
public static void main(String[] args) throws InterruptedException {
gainAllIp();
}
}
IPUtils
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.