如何用java实现,实时获取局域网内所有接入设备的IP地址

如何用java实现,实时获取局域网内所有接入设备的IP地址,第1张

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){}

}

}

}

代码亲测可用:

import javanetInetAddress;

import javanetUnknownHostException;

public class TestInetAddress {

InetAddress myIpAddress = null;

InetAddress[] myServer = null;

public static void main(String args[]) {

TestInetAddress address = new TestInetAddress();

Systemoutprintln("Your host IP is: " + addressgetLocalhostIP());

String domain = ">

public static void main(String[] args) { try { // 获取计算机名 String name = InetAddressgetLocalHost()getHostName(); // 获取IP地址 String ip = InetAddressgetLocalHost()getHostAddress(); Systemoutprintln("计算机名:"+name); Systemoutprintln("IP地址:"+ip); } catch (UnknownHostException e) { Systemoutprintln("异常:" + e); eprintStackTrace(); } }

是否可以解决您的问题?

所以javascript可以通过ajax技术来获得后端语言得到的IP,并返回到前端。 1:通过java的servlet获取IP: public String getRemoteAddress(>

第一种:获取本机的IP

Enumeration<NetworkInterface>

netInterfaces

=

null;

try

{

netInterfaces

=

NetworkInterfacegetNetworkInterfaces();

while

(netInterfaceshasMoreElements())

{

NetworkInterface

ni

=

netInterfacesnextElement();

Systemoutprintln("DisplayName:"

+

nigetDisplayName());

Systemoutprintln("Name:"

+

nigetName());

Enumeration<InetAddress>

ips

=

nigetInetAddresses();

while

(ipshasMoreElements())

{

Systemoutprintln("IP:"

+

ipsnextElement()getHostAddress());

ipTemp=

nigetInetAddresses()nextElement()getHostAddress();

if(ipTemp!="127001"

&&

!"127001"equals(ipTemp))

{

ip=ipTemp;

}

}

}

}catch(Exception

ee)

{

eeprintStackTrace();

}

第二种:也是本机的:

InetAddress

addr

=

InetAddressgetLocalHost();

ip=addrgetHostAddress()toString();//获得本机IP

import javanetInet4Address;

import javanetInetAddress;

import javanetNetworkInterface;

import javanetSocketException;

import javautilArrayList;

import javautilEnumeration;

import javautilList;

/

@author Becolette

@description TODO

@date 2015-11-5 下午01:58:46

/

public class IpAddress {

public static String find() {

List<String> ips = new ArrayList<String>();

// 返回所有网络接口的一个枚举实例

Enumeration<> allNetInterfaces = null;

try {

allNetInterfaces = NetworkInterfacegetNetworkInterfaces();

} catch (SocketException e) {

eprintStackTrace();

}

InetAddress ip = null;

while (allNetInterfaceshasMoreElements()) {

NetworkInterface netInterface = (NetworkInterface) allNetInterfacesnextElement();

Enumeration<InetAddress> addresses = netInterfacegetInetAddresses();

while (addresseshasMoreElements()) {

// 获得当前网络接口

ip = (InetAddress) addressesnextElement();

if (ip != null && ip instanceof Inet4Address && ipgetHostAddress()indexOf("") != -1) {

ipsadd(ipgetHostAddress());

}

}

}

if (ipssize() == 1) {

return ipsget(0);

} else {

for (String ipa : ips) {

if (!"127001"equals(ipa)) {

return ipa;

}

}

}

return MacAddressfind();

}

}

import javanetInetAddress;

import javanetUnknownHostException;

public class Test

{

public static void main(String[] args) throws UnknownHostException

{

InetAddress inet = InetAddressgetLocalHost();

Systemoutprintln("本机的ip=" + inetgetHostAddress());

}

}

以上就是关于如何用java实现,实时获取局域网内所有接入设备的IP地址全部的内容,包括:如何用java实现,实时获取局域网内所有接入设备的IP地址、java通过域名怎么获取本机ip、JAVA获取IP地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9610818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存