Java实现Internet地址获取
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();
}
}
}
运行截图
输入域名的结果

输入IP地址的结果

输入本机上IP地址的结果

增强版内容
- 在源程序的基础之上在输入域名时输出全部地址
- 如果查询的域名或者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地址上的域名的结果

输入IP地址的结果

Java实现Internet地址获取的更多相关文章
- Java根据ip地址获取Mac地址,Java获取Mac地址
Java根据ip地址获取Mac地址,Java获取Mac地址 >>>>>>>>>>>>>>>>>&g ...
- java根据ip地址获取详细地域信息的方法
通过淘宝IP地址库获取IP位置(也可以使用新浪的) 请求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串] 响应信息:(jso ...
- java通过IP地址获取物理位置
import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern ...
- Java根据IP地址获取MAC地址
先使用ping -n 2 10.0.0.1 命令,如果返回的结果中含有TTL字符,证明ping 10.0.0.1是能ping通的,即可达的.如果在Linux机器上请使用 ping -c 2 10.0 ...
- java通过传送地址获取坐标
package com.action; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputS ...
- Java网络编程之查找Internet地址
一.概述 连接到Internet上计算机都有一个称为Internet地址或IP地址的唯一的数来标识.由于IP很难记住,人们设计了域名系统(DNS),DNS可以将人们可以记忆的主机名与计算机可以记忆的I ...
- java工具类(一)之服务端java实现根据地址从百度API获取经纬度
服务端java实现根据地址从百度API获取经纬度 代码: package com.pb.baiduapi; import java.io.BufferedReader; import java.io. ...
- java根据地址获取百度API经纬度
java根据地址获取百度API经纬度(详细文档) public void getLarLng(String address) throws Exception { String ak = " ...
- JAVA从本机获取IP地址
JAVA从本机获取IP地址 论述: 此篇博客是在工作的时候,需要获得当前网络下面正确的ip地址,在网上查阅很多博客,网上一个比较普遍的说法是通过InetAddress.getLocalHost().g ...
随机推荐
- windows svn
1.1Svn和VisualSvn介绍 VisualSvn Server2.5.6(版本控制服务器)免费开源软件 是基于Windows平台上的Subversion服务器,它是免费的 官方下载: http ...
- ◆ 火狐浏览器去除JS方法:
◆ 火狐浏览器去除JS方法: 在火狐地址栏输入about:config 回车 在搜索地址栏中输入javascript.enabled 右键 当一行的中的,值由false变成trun,就OK了 .
- svn服务端配置
1.建立版本库 创建一个新的Subversion项目svnadmin create /var/www/svndata/njlrxx 配置允许用户jiqing访问cd /var/www/svndata/ ...
- 高效率的全组合算法(Java版实现)
博客上看到的一个算法,用Java实现了一个 算法描述: 算法说明:当n大于2时,n个数的全组合一共有(2^n)-1种. 当对n个元素进行全组合的时候,可以用一个n位的二进制数表示取法. 1表示在该位取 ...
- poj1942 Paths on a Grid
处理阶乘有三种办法:(1)传统意义上的直接递归,n的规模最多到20+,太小了,在本题不适用,而且非常慢(2)稍快一点的算法,就是利用log()化乘为加,n的规模虽然扩展到1000+,但是由于要用三重循 ...
- Android窗口为弹出框样式
1.XML android:theme="@android:style/Theme.Dialog <?xml version="1.0" encoding=&quo ...
- .net4.0注册到IIS
IIS和.netfw4.0安装顺序是从前到后,如果不小心颠倒了,无所谓. 打开程序-运行-cmd:输入一下命令重新注册IIS C:\WINDOWS\Microsoft.NET\Framework\v4 ...
- POJ C程序设计进阶 编程题#3: 发票统计
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 有一个小型的报账系统,它有如 ...
- PL/SQL Developer基本用法
一.新建存储过程
- 开始安装 ASP.NET (4.0.30319.18408)。 出现了错误: 0x8007b799 必须具有此计算机的管理员权限才能运行此工具
在Visual Studio命令提示符安装ASP.NET .出现了错误: 0x8007b799 必须具有此计算机的管理员权限才能运行此工具:如下图: 解决方案如下: 1.打开“C:\Windows\S ...