InetAddress类的使用示例

InetAddress类的使用

package com.inetaddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.junit.Test;

/**
 * InetAddress类的使用
 * 1. 获取InetAddress类的方式:
 * 	1)getByName(String host):通过主机(IP地址)获取(掌握)
 * 	2)getLocalHost():获取本机的InetAddress对象(IP地址为127.0.0.1/localhost)
 * 2. InetAddress类的方法使用:
 * 	1)getHostAddress():获取主机IP地址(掌握)
 * 	2)getHostName():获取主机名称(掌握)
 */
public class InetAddressTest {
	@Test
	public void test2() {
		try {
			InetAddress inet = InetAddress.getByName("www.baidu.com");
			System.out.println(inet);
			
			String host = inet.getHostAddress();
			System.out.println(host);
			
			String name = inet.getHostName();
			System.out.println(name);
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}	
	
	@Test
	public void test() throws Exception {
		InetAddress addr1 = InetAddress.getByName("192.168.10.1");
		System.out.println(addr1);
		
		InetAddress addr2 = InetAddress.getByName("www.baidu.com");
		System.out.println(addr2);
		
		//InetAddress addr3 = InetAddress.getByName("127.0.0.1");
		InetAddress addr3 = InetAddress.getByName("localhost");
		System.out.println(addr3);
		
		InetAddress addr4 = InetAddress.getLocalHost();
		System.out.println(addr4);		
		
		/*
		// 下面两种不常用
		byte[] addr = {89,57,21,7};
		InetAddress addr2 = InetAddress.getByAddress(addr);
		System.out.println(addr2);
		
		//InetAddress addr3 = InetAddress.getByAddress("localhost", "127.0.0.1".getBytes());
		//System.out.println(addr3);
		*/
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
InetAddress Java 中表示 IP 地址的,它提供了一组静态方法来获取本地主机的 IP 地址和域名以及其他主机的 IP 地址和域名。InetAddress 可以表示 IPv4 地址和 IPv6 地址。 InetAddress 的常用方法: 1. `getByName(String host)`:根据主机名或 IP 地址字符串获取 InetAddress 对象。 2. `getLocalHost()`:获取本地主机地址。 3. `getHostName()`:获取主机名。 4. `getHostAddress()`:获取 IP 地址。 5. `isReachable(int timeout)`:测试是否可以到达该地址。 6. `equals(Object obj)`:判断两个 InetAddress 对象是否相等。 7. `toString()`:返回 InetAddress 对象的字符串表示形式。 下面是一个使用 InetAddress 示例: ```java import java.net.InetAddress; import java.net.UnknownHostException; public class InetAddressExample { public static void main(String[] args) { try { // 获取本机的 InetAddress 对象 InetAddress localHost = InetAddress.getLocalHost(); System.out.println("本机名:" + localHost.getHostName()); System.out.println("本机 IP 地址:" + localHost.getHostAddress()); // 根据主机名获取 InetAddress 对象 InetAddress baidu = InetAddress.getByName("www.baidu.com"); System.out.println("百度 IP 地址:" + baidu.getHostAddress()); // 测试是否可以到达该地址 boolean reachable = baidu.isReachable(5000); System.out.println("是否可达:" + reachable); } catch (UnknownHostException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 输出结果: ``` 本机名:DESKTOP-XXXXXX 本机 IP 地址:192.168.0.100 百度 IP 地址:39.156.69.79 是否可达:true ``` 以上示例中,首先使用 InetAddress 的 `getLocalHost()` 方法获取本机的 InetAddress 对象,然后使用 `getHostName()` 和 `getHostAddress()` 方法分别获取主机名和 IP 地址。 接着使用 `getByName(String host)` 方法根据主机名获取 InetAddress 对象,并使用 `getHostAddress()` 方法获取 IP 地址。 最后使用 `isReachable(int timeout)` 方法测试是否可以到达该地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值