「java获取主机ip」java获取当前主机ip

博主:adminadmin 2023-01-05 14:42:09 511

今天给各位分享java获取主机ip的知识,其中也会对java获取当前主机ip进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java如何获取本地计算机的IP地址和主机名

可以使用 InetAddress.getLocalHost(),代码如下:

import java.net.*;

public class App {

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

InetAddress local = InetAddress.getLocalHost();

System.out.println("计算机名:" + local.getHostName());

System.out.println("IP:" + local.getHostAddress());

}

}

java如何查询本机ip地址和mac地址

   Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:

import java.net.*;

public class NetInfo {

 public static void main(String[] args) {

    new NetInfo().say();

    }

 public void say() {

   try {

   InetAddress i = InetAddress.getLocalHost();

   System.out.println(i);                  //计算机名称和IP

   System.out.println(i.getHostName());    //名称

   System.out.println(i.getHostAddress()); //只获得IP

   }

   catch(Exception e){e.printStackTrace();}

 }

}

    也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。

java如何获取局域网内所有IP?

1.得到局域网网段,可由自己机器的IP来确定 (也可以手动获取主机IP-CMD-ipconfig /all)

2.根据IP类型,一次遍历局域网内IP地址

JAVA类,编译之后直接运行便可以得到局域网内所有IP,具体怎样使用你自己编写相应代码调用便可

代码如下::

package bean;

import java.io.*;

import java.util.*;

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(threadCount30)

Thread.sleep(50);

threadCount +=1;

PingIp p = new PingIp(ip);

p.start();

}

public void PingAll() throws Exception{

//首先得到本机的IP,得到网段

InetAddress host = InetAddress.getLocalHost();

String hostAddress = host.getHostAddress();

int k=0;

k=hostAddress.lastIndexOf(".");

String ss = hostAddress.substring(0,k+1);

for(int i=1;i =255;i++){ //对所有局域网Ip

String iip=ss+i;

Ping(iip);

}

//等着所有Ping结束

while(threadCount0)

Thread.sleep(50);

}

public static void main(String[] args) throws Exception{

Ip ip= new Ip();

ip.PingAll();

java.util.Set entries = ping.entrySet();

Iterator iter=entries.iterator();

String k;

while(iter.hasNext()){

Map.Entry entry=(Map.Entry)iter.next();

String key=(String)entry.getKey();

String value=(String)entry.getValue();

if(value.equals("true"))

System.out.println(key+"--"+value);

}

}

class PingIp extends Thread{

public String ip; // IP

public PingIp(String ip){

this.ip=ip;

}

public void run(){

try{

Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");

InputStreamReader ir = new InputStreamReader(p.getInputStream());

LineNumberReader input = new LineNumberReader (ir);

//读取结果行

for (int i=1 ; i 7; i++)

input.readLine();

String line= input.readLine();

if (line.length() 17 || line.substring(8,17).equals("timed out"))

ping.put(ip,"false");

else

ping.put(ip,"true");

//线程结束

threadCount -= 1;

}catch (IOException e){}

}

}

}

如何用 Java 获取系统 IP

import java.net.*;

public class Test6 {

public static void main(String[] args) {

// TODO Auto-generated method stub

InetAddress ia=null;

try {

ia=ia.getLocalHost();

String localname=ia.getHostName();

String localip=ia.getHostAddress();

System.out.println("本机名称是:"+ localname);

System.out.println("本机的ip是 :"+localip);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

在Java程序中获取本机IP

import java.net.InetAddress;

import java.util.Enumeration;

import java.net.NetworkInterface;

import java.util.*;

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 = NetworkInterface.getNetworkInterfaces();

while (netInterfaces.hasMoreElements()) {

NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();

Enumeration cardipaddress = ni.getInetAddresses();

InetAddress ip = (InetAddress) cardipaddress.nextElement();

if(!ip.getHostAddress().equalsIgnoreCase("127.0.0.1") )

{ ar.add(ni.getName()+":");

allipaddress=ip.getHostAddress();

while(cardipaddress.hasMoreElements())

{

ip = (InetAddress) cardipaddress.nextElement();

allipaddress=allipaddress+" , "+ip.getHostAddress();

}

ar.add(allipaddress);

}

else

continue;

}

for(int i=0;iar.size();)

{

System.out.println(ar.get(i++));

}

}

}

关于java获取主机ip和java获取当前主机ip的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。