
1得到局域网网段,可由自己机器的IP来确定 (也可以手动获取主机IP-CMD-ipconfig /all)
2根据IP类型,一次遍历局域网内IP地址
JAVA类,编译之后直接运行便可以得到局域网内所有IP,具体怎样使用你自己编写相应代码调用便可
代码如下::
package bean;
import javaio;
import javautil;
public class Ip{
static public HashMap ping; //ping 后的结果集
public HashMap getPing(){ //用来得到ping后的结果集
return ping;
}
//当前线程的数量, 防止过多线程摧毁电脑
static int threadCount = 0;
public Ip() {
ping = new HashMap();
}
public void Ping(String ip) throws Exception{
//最多30个线程
while(threadCount>30)
Threadsleep(50);
threadCount +=1;
PingIp p = new PingIp(ip);
pstart();
}
public void PingAll() throws Exception{
//首先得到本机的IP,得到网段
InetAddress host = InetAddressgetLocalHost();
String hostAddress = hostgetHostAddress();
int k=0;
k=hostAddresslastIndexOf("");
String ss = hostAddresssubstring(0,k+1);
for(int i=1;i <=255;i++){ //对所有局域网Ip
String iip=ss+i;
Ping(iip);
}
//等着所有Ping结束
while(threadCount>0)
Threadsleep(50);
}
public static void main(String[] args) throws Exception{
Ip ip= new Ip();
ipPingAll();
javautilSet entries = pingentrySet();
Iterator iter=entriesiterator();
String k;
while(iterhasNext()){
MapEntry entry=(MapEntry)iternext();
String key=(String)entrygetKey();
String value=(String)entrygetValue();
if(valueequals("true"))
Systemoutprintln(key+"-->"+value);
}
}
class PingIp extends Thread{
public String ip; // IP
public PingIp(String ip){
thisip=ip;
}
public void run(){
try{
Process p= RuntimegetRuntime()exec ("ping "+ip+ " -w 300 -n 1");
InputStreamReader ir = new InputStreamReader(pgetInputStream());
LineNumberReader input = new LineNumberReader (ir);
//读取结果行
for (int i=1 ; i <7; i++)
inputreadLine();
String line= inputreadLine();
if (linelength() <17 || linesubstring(8,17)equals("timed out"))
pingput(ip,"false");
else
pingput(ip,"true");
//线程结束
threadCount -= 1;
}catch (IOException e){}
}
}
}
Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:
import javanet;public class NetInfo {
public static void main(String[] args) {
new NetInfo()say();
}
public void say() {
try {
InetAddress i = InetAddressgetLocalHost();
Systemoutprintln(i); //计算机名称和IP
Systemoutprintln(igetHostName()); //名称
Systemoutprintln(igetHostAddress()); //只获得IP
}
catch(Exception e){eprintStackTrace();}
}
}
也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。
try {
ip = InetAddressgetLocalHost();
NetworkInterface network = NetworkInterfacegetByInetAddress(ip);
if (network != null) {
byte[] mac = networkgetHardwareAddress();
if(mac != null) {
Systemoutprint("MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < maclength; i++) {
sbappend(Stringformat("%02X%s", mac[i], (i < maclength - 1) "-" : ""));
}
Systemoutprintln(sbtoString());
}
} catch (UnknownHostException e) {
eprintStackTrace();
} catch (SocketException e) {
eprintStackTrace();
}
这个网上很多,主要是机器必须支持ICMP和NETBIOS协议。你参考一下:
public String getIP()
{
InetAddress inet;
try {
inet =
InetAddressgetLocalHost();
InetAddressgetByName("");
return
inetgetHostAddress();
} catch (UnknownHostException e) {
// TODO
Auto-generated catch block
eprintStackTrace();
}
return "";
}
这个是获取不到的,因为客户端与你服务器一般都是经过复杂的网络连接来的,通常拿到的MAC一般是线路上某台路由器的MAC,没有多大意义。至于硬盘序列号和CPU序列号,这根本无法从一个soket连接中取到。就好像,我无法知道比如在QQ聊天中对面是人还是狗一样。
package tmp;
import javanetNetworkInterface;
import javautilEnumeration;
public class MacUtil {
public static String getMacAddress() throws Exception {
String s="";
Enumeration<NetworkInterface> ni = NetworkInterface
getNetworkInterfaces();
while (nihasMoreElements()) {
NetworkInterface netI = ninextElement();
byte[] bytes = netIgetHardwareAddress();
if (netI != null && bytes != null && byteslength == 6) {
StringBuffer sb = new StringBuffer();
for (byte b : bytes) {
// 与11110000作按位与运算以便读取当前字节高4位
sbappend(IntegertoHexString((b & 240) >> 4));
// 与00001111作按位与运算以便读取当前字节低4位
sbappend(IntegertoHexString(b & 15));
sbappend("-");
}
sbdeleteCharAt(sblength() - 1);
s+=sbtoString()toUpperCase()+"\n";
}
}
return s;
}
public static void main(String[] args) throws Exception {
Systemoutprintln(MacUtilgetMacAddress());
}
}
以上就是关于JAVA如何获取局域网内所有安卓设备的ip地址,MAC以及序列号全部的内容,包括:JAVA如何获取局域网内所有安卓设备的ip地址,MAC以及序列号、java如何查询本机ip地址和mac地址、在linux用java根据ip获得mac地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)