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 ...
随机推荐
- 【HTML/XML 10】XML文档中的Schema文件
导读:DTD是对XML文档进行有效性验证的方法之一,事实上,继DTD之后,出现了用来规范和描述XML文档的第二代标准:Schema.Schema是DTD的继承,但是也有其不同的地方,它是真正的以独立的 ...
- wcscpy wcscpy_s strcpy strcpy_s的区别
原型声明:extern char *strcpy(char *dest,const char *src); 头文件:string.h 功能:把从src地址开始且含有NULL结束符的字符串赋值到以des ...
- 百度编辑器Ueditor自动换行,添加<p>的问题
百度编辑器Ueditor其实蛮好用的,后来使用了一段时间发现,每次打开后又保存,发现都会往内容的前后都增加一个空白的<p></p>.刚开始以后是百度编辑器的问题,找了很长时间也 ...
- oracle 游标示例
declare iCount int:=0; sPath nvarchar2(200); tdzsh nvarchar2(50);begin for x in (select c.imgpath fr ...
- .NET 命名规范 代码示例
class Person { /// <summary> /// 公有字段.属性 首字母大写 /// </summary> public string FirstName; p ...
- vim的.vimrc文件设置
set nocompatibleset autowriteset autoreadset nobackupset noswapfile " --- syntax and indent --- ...
- Sqlserver数据库存储路径的修改
Sqlserver数据库存储路径的修改 Sqlserver数据库存储路径问题:本系统sqlserver路径默认是存储在C盘目录下的,由于数据会慢慢变大和避免重装系统数据丢失等问题,最好手动将路径设置在 ...
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的
异常原因: 1.写了一个死循环,这个可能性最大. 2.进行大数据量的运算,导致假死状况. 解决办法:Debug -> Exceptions -> Managed Debug Assista ...
- 《第一行代码--Android》阅读笔记之Activity
1.BaseActivity里面可以干什么 定义一个Context定义一个TAG 记录当前的Activity名字getClass().getSimpleName(); 2.Activity里面的几个重 ...
- Knockout.Js官网学习(数组observable)
前言 如果你要探测和响应一个对象的变化,你应该用observables. 如果你需要探测和响应一个集合对象的变化,你应该用observableArray . 在很多场景下,它都非常有用,比如你要在UI ...