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地址获取的更多相关文章

  1. Java根据ip地址获取Mac地址,Java获取Mac地址

    Java根据ip地址获取Mac地址,Java获取Mac地址 >>>>>>>>>>>>>>>>>&g ...

  2. java根据ip地址获取详细地域信息的方法

    通过淘宝IP地址库获取IP位置(也可以使用新浪的) 请求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串] 响应信息:(jso ...

  3. java通过IP地址获取物理位置

    import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern ...

  4. Java根据IP地址获取MAC地址

    先使用ping -n  2 10.0.0.1 命令,如果返回的结果中含有TTL字符,证明ping 10.0.0.1是能ping通的,即可达的.如果在Linux机器上请使用 ping -c 2 10.0 ...

  5. java通过传送地址获取坐标

    package com.action; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputS ...

  6. Java网络编程之查找Internet地址

    一.概述 连接到Internet上计算机都有一个称为Internet地址或IP地址的唯一的数来标识.由于IP很难记住,人们设计了域名系统(DNS),DNS可以将人们可以记忆的主机名与计算机可以记忆的I ...

  7. java工具类(一)之服务端java实现根据地址从百度API获取经纬度

    服务端java实现根据地址从百度API获取经纬度 代码: package com.pb.baiduapi; import java.io.BufferedReader; import java.io. ...

  8. java根据地址获取百度API经纬度

    java根据地址获取百度API经纬度(详细文档) public void getLarLng(String address) throws Exception { String ak = " ...

  9. JAVA从本机获取IP地址

    JAVA从本机获取IP地址 论述: 此篇博客是在工作的时候,需要获得当前网络下面正确的ip地址,在网上查阅很多博客,网上一个比较普遍的说法是通过InetAddress.getLocalHost().g ...

随机推荐

  1. (笔记)angular material radio用法

  2. 轻松入门React和Webpack

    最近在学习React.js,之前都是直接用最原生的方式去写React代码,发现组织起来特别麻烦,之前听人说用Webpack组织React组件得心应手,就花了点时间学习了一下,收获颇丰 <!-- ...

  3. FTP操作

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...

  4. HTML5 CSS3简要教程

    Web 设计师可以使用HTML4和CSS2.1完成一些很酷的东西.我们可以在不使用陈旧的基于table布局的基础上完成文档逻辑结构并创建内容丰富的网站.我们可以在不使用内联<font>和& ...

  5. MYSQL将表名称修改成大写的存储过程

    1. 条件: 1.1 Mysql设置对大小写敏感 2. 执行下述存储过程: #call uppercase('库名') DROP PROCEDURE IF EXISTS uppercase; )) B ...

  6. js设计模式(12)---职责链模式

    0.前言 老实讲,看设计模式真得很痛苦,一则阅读过的代码太少:二则从来或者从没意识到使用过这些东西.所以我采用了看书(<js设计模式>)和阅读博客(大叔.alloyteam.聂微东)相结合 ...

  7. tomcat目录简介

    http://www.cnblogs.com/kerrycode/p/3588816.html 主目录下面有bin.lib等目录 bin 存放Tomcat启动.停止服务程序以及一些其他脚本程序 lib ...

  8. Oracle中rownum的用法

    rownum是Oracle对查询结果进行顺序编号,第一行分配1,第二行2,以此类推.rownum不能以任何表的名称作为前缀. rownum这个伪字段可以用于控制返回的记录行数. 例如表:student ...

  9. 合并多个List<T>类型并通过LINQ按指定属性排序

    后台CS代码: namespace WebFormTest.TestCollect { public partial class ListTest : System.Web.UI.Page { pro ...

  10. 官网下载Spring dist

    新版Spring官网下载Spring的dist可真是麻烦 跟着下面的贴图走吧,有些在网页的下面,需要打开相应页面后往下拉拉. 下载完后解压lib里面就是各种jar包了 真是麻烦啊,不好找,不过Spri ...