import javanetInetAddress;
import javautilEnumeration;
import javanetNetworkInterface;
import javautil;
public class ipdisplay {
/
@param args
@throws Exception
/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String allipaddress;
ArrayList ar = new ArrayList();
Enumeration netInterfaces = NetworkInterfacegetNetworkInterfaces();
while (netInterfaceshasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfacesnextElement();
Enumeration cardipaddress = nigetInetAddresses();
InetAddress ip = (InetAddress) cardipaddressnextElement();
if(!ipgetHostAddress()equalsIgnoreCase("127001") )
{ aradd(nigetName()+":");
allipaddress=ipgetHostAddress();
while(cardipaddresshasMoreElements())
{
ip = (InetAddress) cardipaddressnextElement();
allipaddress=allipaddress+" , "+ipgetHostAddress();
}
aradd(allipaddress);
}
else
continue;
}
for(int i=0;i<arsize();)
{
Systemoutprintln(arget(i++));
}
}
}
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){}
}
}
}
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){}
}
}
}
下面有一篇文章,介绍若何读取物理网卡的地址 ,同样的
你可以用这个方法读取你所需要的本机IP地址
=======================================================
J2SE50新特性之ProcessBuilder
这个例子使用了J2SE50的ProcessBuilder类执行外部的程序,相对于 Runtimeexec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址
package comkuaffjdk5package;
import javaioIOException;
import javaioInputStream;
import javautilArrayList;
import javautilList;
public class ProcessBuilderShow
{
public static List getPhysicalAddress()
{
Process p = null;
//物理网卡列表
List address = new ArrayList();
try
{
//执行ipconfig /all命令
p = new ProcessBuilder("ipconfig", "/all")start();
}
catch (IOException e)
{
return address;
}
byte[] b = new byte[1024];
StringBuffer sb = new StringBuffer();
//读取进程输出值
InputStream in = pgetInputStream();
try
{
while (inread(b)>0)
{
sbappend(new String(b));
}
}
catch (IOException e1)
{
}
finally
{
try
{
inclose();
}
catch (IOException e2)
{
}
}
//以下分析输出值,得到物理网卡
String rtValue = sbsubstring(0);
int i = rtValueindexOf("Physical Address :");
while(i>0)
{
rtValue = rtValuesubstring(i + "Physical Address :"length());
addressadd(rtValuesubstring(0,18));
i = rtValueindexOf("Physical Address :");
}
return address;
}
public static void main(String[] args)
{
List address = ProcessBuilderShowgetPhysicalAddress();
for(String add:address)
{
Systemoutprintf("物理网卡地址:%s%n", add);
}
}
}
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){}
}
}
}
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全部的内容,包括:在Java程序中获取本机IP、java如何获取局域网内所有IP、JAVA如何获取局域网内所有安卓设备的ip地址,MAC以及序列号等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)