【Java网络编程】获得实验室局域网中所有开机主机名称和IP地址,InetAddress类,在java中如何获取IP地址的方法


一、InetAddress类

在网络API套接字,InetAddress类和它的子类型对象使用域名DNS系统,处理主机名到主机IPv4或IPv6地址的转换。
由于InetAddress类只有一个构造函数,且不能传递参数,所以不能直接创建该对象实例,比如下面的做法就是错误的:
InetAddress ia = new InetAddress (); ×
可通过以下5个成员方法获得InetAddress对象或InetAddress数组:
(1)getAllByName(String host)方法返回一个InetAddress对象的引用,每个对象包含一个表示相应主机名的单独的IP地址,这个IP地址是通过host参数传递的,例如:
InetAddress [] ia = InetAddress.getAllByName(“MyHost”);
(2)getByAddress(byte [] addr)方法返回一个InetAddress对象的引用,这个对象包含了一个Ipv4地址或Ipv6地址,Ipv4地址是一个4字节数组,Ipv6地址是一个16字节地址数组。
(3)getByAddress(String host, byte [] addr)方法返回一个InetAddress对象的引用,这个InetAddress对象包含了一个由host和4字节的addr数组指定的IP地址,或者是host和16字节的addr数组指定的IP地址。
(4)getByName(String host)方法返回一个InetAddress对象,该对象包含了一个与host参数指定的主机相对应的IP地址。
(5)getLocalHost()方法返回一个InetAddress对象,这个对象包含了本地机的IP地址。

以上各方法均可能产生的UnknownHostException(未知的主机名)异常。当获得了InetAddress类对象的引用就可以调用InetAddress的各种方法来获得InetAddress子类对象中的IP地址信息。例,通过调用getCanonicalHostName()从域名服务中获得标准的主机名,getHostAddress()获得IP地址,getHostName()获得主机名,isLoopbackAddress()判断IP地址是否是一个loopback环回地址。

二、获得指定IP地址和主机名的主要方法以及练习

1、主要方法

返回值类型 方法原型 方法功能
byte[] getAddress() 获取本对象的IP
static InetAddress[] getAllByName(String host) 从DNS获取域名相应的所有IP地址
static InetAddress getByAddress(byte[] addr) 通过IP地址创建InetAddress(IP地址必须是byte数组)
static InetAddress getByAddress(String host,byte[] addr) 通过IP地址创建InetAddress(IP地址必须是byte数组),host只作为一个表示addr的别名,放在’/’前面,可以是任意字符串。
static InetAddress getByName(String host) 连接本地的DNS服务器,查找名字和数字地址(有缓存会直接查缓存),没找到会抛出异常。
String getCanonicalHostName() 得到主机的主机名,是否访问DNS服务器取决于DNS服务器如何解释主机名和主机别名
String getHostAddress() 返回一个字符串,包含点分四段格式的IP地址
String getHostName() 获取主机别名,根据创建InetAddress对象的不同方式返回值也有所不同。
static InetAddress getLocalHost() 为运行这个代码的主机返回一个InetAddress对象
boolean isLoopbackAddress() 用于检查 InetAddress 是否为回环地址

2、源码

由于练习比较简单,就给大家源码自己去测试运行看结果。
代码如下:

import java.net.*;
import java.util.*;

class InetAddressDemo{
    
    
	public static void main (String [] args) throws UnknownHostException{
    
    
		//练习一,获得本地主机信息
	    InetAddress ia = InetAddress.getByName("localhost");
		System.out.println(ia.toString());
		//练习二,获得指定域名主机的信息(www.baidu.com)
	    String host = "www.baidu.com";
	    InetAddress ia1 = InetAddress.getByName(host);
		System.out.println(ia1.toString());
		//练习三,根据指定域名获得所有信息(www.baidu.com)
	    String host1= "www.baidu.com";
		InetAddress[] ia2 = InetAddress.getAllByName(host1);
		for(int i=0;i<ia2.length;i++){
    
    
		   System.out.println(ia2[i].getHostName() + " : "+ia2[i].getHostAddress());
		}
		//练习四,比较根据localhost和计算机名获得信息的不同
		String host = "localhost";    //更改localhost为你现在所使用计算机名,查看不同
	    InetAddress ia = InetAddress.getByName (host);
		System.out.println ("Canonical Host Name = " + ia.getCanonicalHostName ());
		System.out.println ("Host Address = " + ia.getHostAddress ());
		System.out.println ("Host Name = " + ia.getHostName ());
		System.out.println ("Is Loopback Address = " + ia.isLoopbackAddress ());
		//练习五,获得本地主机所有IP地址
		String host = InetAddress.getLocalHost().getHostName();
		InetAddress[] ia = InetAddress.getAllByName(host);
		for(int i = 0; i <ia.length; i++)
		    System.out.println(ia[i]);
 		//练习六,根据IP地址构造InetAddress
 		byte [] ip = new byte[] {
    
     (byte) 202, (byte) 117, (byte)128 , 7};  //可以更改数值超过255
	    InetAddress ia = InetAddress.getByAddress("xxx",ip);
	    System.out.println(ia);
		
     }
  }
}

三、获得实验室局域网中所有开机主机名称和IP地址

1、源码

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class scanhost {
    
    

	public static void main(String[] args) throws IOException {
    
    
		// TODO Auto-generated method stub
		String host=InetAddress.getLocalHost().getHostName();
		InetAddress ia=InetAddress.getByName(host);
		InetAddress address=null;
		String ip=ia.getHostAddress();
		String[]IPaddress=ip.split("\\.");
		for(int i=1;i<255;i++){
    
    
			address=InetAddress.getByName(IPaddress[0]+"."+IPaddress[1]+"."+IPaddress[2]+"."+i);
			if(address.isReachable(20)){
    
    
				System.out.println(address.getCanonicalHostName()+address);
			}else{
    
    
				System.out.println("未找到主机!");
			}
		}

}
}

2、运行结果

在这里插入图片描述


猜你喜欢

转载自blog.csdn.net/qq_51720181/article/details/125557526