java internetaddress_Java实现Internet地址获取

该博客展示了如何使用Java实现DNS查询,包括输入域名获取IPv4地址,输入IP地址反查域名,并且在增强版中增加了检查输入是否为本地网络接口地址的功能。示例代码包括基本版和增强版,提供了命令行交互和处理多输入的能力。
摘要由CSDN通过智能技术生成

Java实现Internet地址获取

代码内容

输入域名输出IPV4地址

输入IP地址输出域名

支持命令行输入

支持交互式输入

代码实现

/* nslookup.java */

import java.net.*;

import java.util.regex.Pattern;

import java.io.*;

public class nslookup {

public static void main(String[] args) {

if (args.length > 0) {

for (int i = 0; i < args.length; i++) {

System.out.println("\n> " + args[i]);

lookup(args[i]);

}

} else {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the domain names or IP addresses. Enter \"exit\" to quit.");

try {

boolean isEmptyLine = false;

while (true) {

if (isEmptyLine){

isEmptyLine = false;

System.out.print("> ");

} else

System.out.print("\n> ");

String host = in.readLine();

if (host.equalsIgnoreCase("exit")) {

break;

} else if (host.isEmpty()){

isEmptyLine = true;

continue;

}

lookup(host);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

private static void lookup(String host) {

if(isDomain(host)) {

nat(host, true);

} else {

nat(host, false);

}

}

private static boolean isDomain(String host) {

String[] part = host.split("\\.");

if (part.length == 4) {

for (String pa : part) {

if (!isNumeric(pa)) {

return true;

}

}

return false;

} else {

return true;

}

}

public static boolean isNumeric(String str) {

Pattern pattern = Pattern.compile("[0-9]*");

//Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]*)?$");

return pattern.matcher(str).matches();

}

private static void nat(String host, boolean isDomain) {

try {

if (host.equals("127.0.0.1")){

System.out.println("Name: localhost");

return;

}

InetAddress[] address = InetAddress.getAllByName(host);

if (isDomain) {

for (int i = 0; i < address.length; i++){

System.out.println("Address: " + address[i].getHostAddress());

}

}

else if (host.equals(address[0].getHostName())){

for (int i = 0; i < address.length; i++){

System.out.println("Address: " + address[i].getHostAddress());

}

}

else {

for (int i = 0; i < address.length; i++){

System.out.println("Name: " + address[i].getHostName());

}

}

} catch (UnknownHostException e) {

e.printStackTrace();

}

}

}

运行截图

输入域名的结果

2e391754fc405f816e1ecc8870c5c0c9.png

输入IP地址的结果

adae4505ade7cd4d2bbc6f7307b5b2e2.png

输入本机上IP地址的结果

e0f59f7dbcecacf70fa376e27bbb819f.png

增强版内容

在源程序的基础之上在输入域名时输出全部地址

如果查询的域名或者IP在本主机上还要输出对应的端口号

如果不在本主机上也需要给相应的提示信息

增强版代码实现

/* nslookupAdvanced.java */

import java.net.*;

import java.util.regex.Pattern;

import java.io.*;

public class nslookupAdvanced {

public static void main(String[] args) {

if (args.length > 0) {

for (int i = 0; i < args.length; i++) {

System.out.println("\n> " + args[i]);

lookup(args[i]);

}

} else {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the domain names or IP addresses. Enter \"exit\" to quit.");

try {

boolean isEmptyLine = false;

while (true) {

if (isEmptyLine){

isEmptyLine = false;

System.out.print("> ");

} else

System.out.print("\n> ");

String host = in.readLine();

if (host.equalsIgnoreCase("exit")) {

break;

} else if (host.isEmpty()){

isEmptyLine = true;

continue;

}

lookup(host);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

private static void lookup(String host) {

if(isDomain(host)) {

nat(host, true);

decideNI(host);

} else {

nat(host, false);

decideNI(host);

}

}

private static boolean isDomain(String host) {

String[] part = host.split("\\.");

if (part.length == 4) {

for (String pa : part) {

if (!isNumeric(pa)) {

return true;

}

}

return false;

} else {

return true;

}

}

public static boolean isNumeric(String str) {

Pattern pattern = Pattern.compile("[0-9]*");

//Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]*)?$");

return pattern.matcher(str).matches();

}

private static void nat(String host, boolean isDomain) {

try {

if (host.equals("127.0.0.1")){

System.out.println("Name: localhost");

return;

}

InetAddress[] address = InetAddress.getAllByName(host);

if (isDomain || host.equals(address[0].getHostName())) {

for (int i = 0; i < address.length; i++) {

System.out.println("Address: " + address[i].getHostAddress());

}

} else {

System.out.println("Name: " + address[0].getHostName());

}

} catch (UnknownHostException e) {

e.printStackTrace();

}

}

private static void decideNI(String host) {

try {

InetAddress address = InetAddress.getByName(host);

NetworkInterface ni = NetworkInterface.getByInetAddress(address);

if (ni != null) {

String niName = ni.getName();

String[] niDisplayName = ni.getDisplayName().split(" ");

System.out.println("This is local address " + niName +

niDisplayName[niDisplayName.length - 1] + ".");

} else {

System.out.println("This is not local address.");

}

} catch (SocketException e) {

e.printStackTrace();

} catch (UnknownHostException e) {

e.printStackTrace();

}

}

}

增强版运行结果

输入一个绑定到多个IP地址上的域名的结果

7a76ae24b7edb568e66cacddef965a70.png

输入IP地址的结果

175767bcac40b95982c5f2a1adc79e80.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值