InetAddress相关笔记

InetAddress的实例对象包含以数字形式保存的IP地址,同时还可能包含主机名(如果使用主机名来获取InetAddress的实例,或者使用数字来构造,并且启用了反向主机名解析的功能)。InetAddress类提供了将主机名解析为IP地址(或反之)的方法。其生成InetAddress对象的方法(getLocalHost(),getByName(),getAllByName(),getByAddress()等)

 1 import java.net.UnknownHostException;
 2
 3 /*
 4  * To change this license header, choose License Headers in Project Properties.
 5  * To change this template file, choose Tools | Templates
 6  * and open the template in the editor.
 7  */
 8 import java.net.*;
 9 import java.util.*;
10 /**
11  *
12  * @author silianbo
13  */
14 public class InetAddressDemo  {
15     public static void main(String[] args) throws UnknownHostException{
16         //此处添加相关的代码段
17
18     }
19
20 }

获得本地主机信息

1 InetAddress localAddress = InetAddress.getLocalHost();
2         System.out.println(localAddress);    

指定域名主机的信息

String host = "www.google.com";
		InetAddress address = InetAddress.getByName(host);
		System.out.println(address);

根据指定域名获得所有信息

String host = "www.baidu.com";     //注意获得的所有IP地址
		InetAddress [] addresses = InetAddress.getAllByName(host);
		for(InetAddress address : addresses)
			System.out.println(address);

比较根据localhost和计算机名获得信息的不同

String host = "localhost";    //更改localhost为你现在所使用计算机名,查看不同
	    InetAddress ia = InetAddress.getByName (host);
		System.out.println ("Canonical Host Name = " + ia.getCanonicalHostName ());
		System.out.println ("Host Address = " + ia.getHostAddress ());
		System.out.println ("Host Name = " + ia.getHostName ());
		System.out.println ("Is Loopback Address = " + ia.isLoopbackAddress ());

获得本地主机所有IP地址

Enumeration<NetworkInterface> netInterfaces = null;
 		try {
     		netInterfaces = NetworkInterface.getNetworkInterfaces();
     		while (netInterfaces.hasMoreElements()) {
         		NetworkInterface ni = netInterfaces.nextElement();
         		System.out.println("DisplayName:" + ni.getDisplayName());
         		System.out.println("Name:" + ni.getName());
         		Enumeration<InetAddress> ips = ni.getInetAddresses();
         		while (ips.hasMoreElements()) {
             		System.out.println("IP:" + ips.nextElement().getHostAddress());
         		}
     		}
 		} catch (Exception e) {
     		e.printStackTrace();
 		}

  

run:
DisplayName:Software Loopback Interface 1
Name:lo
IP:127.0.0.1
IP:0:0:0:0:0:0:0:1
DisplayName:Microsoft 内核调试网络适配器
Name:eth0
DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter
Name:wlan0
IP:fe80:0:0:0:21c7:474b:810b:997d%wlan0
DisplayName:Realtek PCIe GBE 系列控制器
Name:eth1
IP:10.128.195.6
IP:fe80:0:0:0:21f7:f5fa:71a7:e3d1%eth1
DisplayName:Bluetooth 设备(个人区域网)
Name:eth2
IP:fe80:0:0:0:5d4a:7ad0:3b6f:29af%eth2
DisplayName:Bluetooth 设备(RFCOMM 协议 TDI)
Name:net0
DisplayName:Microsoft Wi-Fi Direct 虚拟适配器
Name:wlan1
DisplayName:Microsoft ISATAP Adapter
Name:net1
IP:fe80:0:0:0:0:5efe:a80:c306%net1
DisplayName:Teredo Tunneling Pseudo-Interface
Name:net2
IP:2001:0:9d38:90d7:149e:221a:8adf:279a
IP:fe80:0:0:0:149e:221a:8adf:279a%net2
DisplayName:WAN 微型端口(SSTP)
Name:net3
DisplayName:WAN 微型端口(L2TP)
Name:net4
DisplayName:WAN 微型端口(PPPOE)
Name:ppp0
DisplayName:WAN 微型端口(PPTP)
Name:net5
DisplayName:WAN Miniport (IKEv2)
Name:net6
DisplayName:WAN 微型端口(IP)
Name:eth3
DisplayName:WAN 微型端口(IPv6)
Name:eth4
DisplayName:WAN 微型端口(网络监视器)
Name:eth5
DisplayName:RAS 同步适配器
Name:ppp1
DisplayName:Microsoft ISATAP Adapter #2
Name:net7
IP:fe80:0:0:0:0:5efe:c0a8:3401%net7
DisplayName:Microsoft ISATAP Adapter #3
Name:net8
IP:fe80:0:0:0:0:5efe:c0a8:c701%net8
DisplayName:Microsoft 托管网络虚拟适配器
Name:wlan2
IP:fe80:0:0:0:6c29:b7c6:d687:691c%wlan2
DisplayName:VMware Virtual Ethernet Adapter for VMnet1
Name:eth6
IP:192.168.52.1
IP:fe80:0:0:0:20d1:2461:f68:f35%eth6
DisplayName:VMware Virtual Ethernet Adapter for VMnet8
Name:eth7
IP:192.168.199.1
IP:fe80:0:0:0:1859:4837:4f9e:32dd%eth7
DisplayName:Microsoft ISATAP Adapter #4
Name:net9
IP:fe80:0:0:0:0:5efe:a0b:b6ed%net9
DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #2
Name:wlan3
DisplayName:Microsoft ISATAP Adapter #5
Name:net10
DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #3
Name:wlan4
DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4
Name:wlan5
IP:fe80:0:0:0:95a5:ed42:ab58:12c4%wlan5
DisplayName:Realtek PCIe GBE 系列控制器-WFP Native MAC Layer LightWeight Filter-0000
Name:eth8
DisplayName:Realtek PCIe GBE 系列控制器-Liebao Wifi NAT Driver-0000
Name:eth9
DisplayName:Realtek PCIe GBE 系列控制器-PPPoe Performance Enhancer-0000
Name:eth10
DisplayName:Realtek PCIe GBE 系列控制器-QoS Packet Scheduler-0000
Name:eth11
DisplayName:Realtek PCIe GBE 系列控制器-WFP 802.3 MAC Layer LightWeight Filter-0000
Name:eth12
DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-WFP Native MAC Layer LightWeight Filter-0000
Name:wlan6
DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-Virtual WiFi Filter Driver-0000
Name:wlan7
DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-Native WiFi Filter Driver-0000
Name:wlan8
DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-Liebao Wifi NAT Driver-0000
Name:wlan9
DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-PPPoe Performance Enhancer-0000
Name:wlan10
DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-QoS Packet Scheduler-0000
Name:wlan11
DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000
Name:wlan12
DisplayName:Microsoft 托管网络虚拟适配器-WFP Native MAC Layer LightWeight Filter-0000
Name:wlan13
DisplayName:Microsoft 托管网络虚拟适配器-Native WiFi Filter Driver-0000
Name:wlan14
DisplayName:Microsoft 托管网络虚拟适配器-Liebao Wifi NAT Driver-0000
Name:wlan15
DisplayName:Microsoft 托管网络虚拟适配器-PPPoe Performance Enhancer-0000
Name:wlan16
DisplayName:Microsoft 托管网络虚拟适配器-QoS Packet Scheduler-0000
Name:wlan17
DisplayName:Microsoft 托管网络虚拟适配器-WFP 802.3 MAC Layer LightWeight Filter-0000
Name:wlan18
DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-WFP Native MAC Layer LightWeight Filter-0000
Name:wlan19
DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-Native WiFi Filter Driver-0000
Name:wlan20
DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-Liebao Wifi NAT Driver-0000
Name:wlan21
DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-PPPoe Performance Enhancer-0000
Name:wlan22
DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-QoS Packet Scheduler-0000
Name:wlan23
DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-WFP 802.3 MAC Layer LightWeight Filter-0000
Name:wlan24
DisplayName:WAN 微型端口(IP)-WFP Native MAC Layer LightWeight Filter-0000
Name:eth13
DisplayName:WAN 微型端口(IP)-Liebao Wifi NAT Driver-0000
Name:eth14
DisplayName:WAN 微型端口(IP)-PPPoe Performance Enhancer-0000
Name:eth15
DisplayName:WAN 微型端口(IP)-QoS Packet Scheduler-0000
Name:eth16
DisplayName:WAN 微型端口(IPv6)-WFP Native MAC Layer LightWeight Filter-0000
Name:eth17
DisplayName:WAN 微型端口(IPv6)-Liebao Wifi NAT Driver-0000
Name:eth18
DisplayName:WAN 微型端口(IPv6)-PPPoe Performance Enhancer-0000
Name:eth19
DisplayName:WAN 微型端口(网络监视器)-WFP Native MAC Layer LightWeight Filter-0000
Name:eth20
DisplayName:WAN 微型端口(IPv6)-QoS Packet Scheduler-0000
Name:eth21
DisplayName:WAN 微型端口(网络监视器)-Liebao Wifi NAT Driver-0000
Name:eth22
DisplayName:WAN 微型端口(网络监视器)-PPPoe Performance Enhancer-0000
Name:eth23
DisplayName:WAN 微型端口(网络监视器)-QoS Packet Scheduler-0000
Name:eth24
DisplayName:ChinaNetSNWide
Name:ppp2
IP:10.11.182.237
BUILD SUCCESSFUL (total time: 1 second)

  

根据IP地址构造InetAddress

byte [] ip = new byte[] { (byte) 202, (byte) 117, (byte)128 , 7};  //可以更改数值超过255
		InetAddress address1 = InetAddress.getByAddress(ip);
		InetAddress address2 = InetAddress.getByAddress("www.baidu.com", ip);
		System.out.println(address1);
		System.out.println(address2);

利用InetAddress.getByName()按计算机名称获局域网中所有开机主机名称和IP

  for (int i = 0; i <= 100; i++) {
            int s = i + 1;
            String host = "192.168.52." + s;
            try {
                InetAddress a = InetAddress.getByName(host);
                System.out.println("The IP is:" + a.getHostAddress());
                System.out.println("The localhost  is:" + a.getHostName());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

  

利用InetAddress.getByName()按IP地址获得指定网络段所有开机主机名称/域名和IP地址,结合方法isReachable()

for (int i = 0; i <= 155; i++) {
            int s = i + 100;
            String host = "192.168.52." + s;
            try {
                InetAddress a = InetAddress.getByName(host);
                System.out.println("The IP is:" + a.getHostAddress());

                System.out.println("The localhost  is:" + a.getHostName());
                a.isReachable(3000);
            } catch (UnknownHostException e) {
            }

        }

InetAddress()  相关的操作就这么多!

时间: 2024-10-13 19:29:11

InetAddress相关笔记的相关文章

Android在线升级相关笔记一(解析服务器版本与当前版本比较)

大概流程:Android客户端去访问服务器上的封装了版本号等信息的xml文件,对服务器上的版本和当前版本进行比较, 如果低于服务器的版本,则下载服务器上的新版软件,进行安装替换,完成升级. 一.首先用tomcat搭建服务器,用于开发测试. 下载tomcat请参考:http://blog.csdn.net/only_tan/article/details/25110625 1.在tomcat中新建自己的项目: \apache-tomcat-6.0.39\webapps 目录下新建自己的项目文件夹,

Linux 下面java编译环境相关笔记

1.如果自己编写的java文件依赖于很多第三方库(比如adir1和dir2下面都有很多jar包),加入自己的文件名叫foo.java那么可以用如下指令编译 javac -Djava.ext.dirs=dir1;dir2 foo.java 2.打包一个可执行的jar包的过程 a.写一个完整的java文件HelloWorld.java b.执行命令:javac HelloWorld.java生成HelloWorld.class c.执行命令 jar cvf HelloWorld.jar HelloW

软件安装相关笔记

程序的安装 rpm :redhat packagemanager早期 rpm is package manager现在 rpm:/var/lib/rpm数据库 rpm命名规则: 主包:bind-version-el5.i386.rpm 子包:bind-libs-version-el5.i386.rpm 格式: name-version-release.arch.rpm bind-major.minor.release.arch.rpm 主版本号:重大改进 次版本好:某个子功能发生重大改变 发行号

shell脚本相关笔记

书写一个shell脚本并使之执行的步骤: 1. 打开一个文件,以 #! bin/bash作为开头 2.在文件中写入一些正确可执行的shell语句 3.保存文件 4.更改文件权限,执行文件 一些小技巧: 如果想在系统中添加自己的命令,比如执行自己建好的shell脚本语句这样一条命令 实例: 1.创建文件cd.sh 内容为: #! bin/bash cd xxx/yyy/zzz 2.保存文件 3.执行文件./cd.sh 现在发现并没有能够打开我们想要打开的目录并进去. 原因是shell还有分父she

神经网络与深度学习相关笔记

说明: 本文是关于http://neuralnetworksanddeeplearning.com/的相关笔记 问题一:神经元在错误的值上饱和导致学习速率的下降 解决: 1.输出层: 因使用二次代价函数时,学习在神经元犯了明显错误时却比学习快接近真实值时慢, 可以使用交叉熵代价函数 使得在神经元犯明显错误时学习得更快 注: 输出层:具有交叉熵代价的S型(sigmoid)输出层或具有对数似然代价的柔性最大值(softmax)输出层 后者更加适用于那些需要将输出激活值解释为概率的场景 2.隐藏层:

05 神经网络与深度学习相关笔记

说明: 本文主要是关于http://neuralnetworksanddeeplearning.com/的相关笔记 问题一:神经元在错误的值上饱和导致学习速率的下降 解决: 1.输出层: 因使用二次代价函数时,学习在神经元犯了明显错误时却比学习快接近真实值时慢, 可以使用交叉熵代价函数 使得在神经元犯明显错误时学习得更快 注: 输出层:具有交叉熵代价的S型(sigmoid)输出层或具有对数似然代价的柔性最大值(softmax)输出层 后者更加适用于那些需要将输出激活值解释为概率的场景 2.隐藏层

ViewPage相关笔记

只有步骤,备用 ViewPage+PagerAdapter 布局控件 在activity的布局文件中添加控件,控件标记名称为: android.support.v4.view.ViewPager 有两个子控件: android.support.v4.view.PagerTitleStrip(会在tab标题的上面,但是不知道怎么随着页面的左右滚动切换,所以暂时不知道如何使用这个控件) android.support.v4.view.PagerTabStrip(每个tab页面的标题,会随着页面的左右

linux 下安装mysql相关笔记

安装mysqlyum -y install mysql-serveryum -y install mysql mysql-devel 都显示完成就可以了 查看mysql的版本号rpm -qi mysql-server 数据库目录/var/lib/mysql/配置文件/usr/share /mysql(mysql.server命令及配置文件)相关命令/usr/bin(mysqladmin mysqldump等命令)启动脚本/etc/rc.d/init.d/(启动脚本文件mysql的目录) 启动my

LoadRunner简单培训相关笔记

学校花了20万买了LoadRunner,因此作为幸运的一届,赶上了这场培训.以下为笔记以及培训后制作的简易思维导图. Loadrunner思维导图源码(Xmind): http://pan.baidu.com/s/1dFMjtN7 LoadRunner上课时记得笔记(较乱): http://pan.baidu.com/s/1dFKh4hN