基础知识

1.InetAddress类

在网络API套接字,InetAddress类和它的子类型对象使用域名DNS系统,处理主机名到主机IPv4或IPv6地址的转换。如图1-1所示。

由于InetAddress类只有一个构造函数,且不能传递参数,所以不能直接创建该对象实例,比如下面的做法就是错误的:

InetAddress ia = new InetAddress ();             ×

  可通过以下5个成员方法获得InetAddress对象或InetAddress数组:

l getAllByName(String host)方法返回一个InetAddress对象的引用,每个对象包含一个表示相应主机名的单独的IP地址,这个IP地址是通过host参数传递的,例如:

InetAddress [] ia = getAllByName(“MyHost”);

l getByAddress(byte [] addr)方法返回一个InetAddress对象的引用,这个对象包含了一个Ipv4地址或Ipv6地址,Ipv4地址是一个4字节数组,Ipv6地址是一个16字节地址数组。

l getByAddress(String host, byte [] addr)方法返回一个InetAddress对象的引用,这个InetAddress对象包含了一个由host和4字节的addr数组指定的IP地址,或者是host和16字节的addr数组指定的IP地址。

l getByName(String host)方法返回一个InetAddress对象,该对象包含了一个与host参数指定的主机相对应的IP地址。

l getLocalHost()方法返回一个InetAddress对象,这个对象包含了本地机的IP地址。

以上各方法均可能产生的UnknownHostException(未知的主机名)异常。当获得了InetAddress类对象的引用就可以调用InetAddress的各种方法来获得InetAddress子类对象中的IP地址信息。例,通过调用getCanonicalHostName()从域名服务中获得标准的主机名,getHostAddress()获得IP地址,getHostName()获得主机名,isLoopbackAddress()判断IP地址是否是一个loopback环回地址。

2.URL类

IP地址唯一标识了Internet上的计算机,而URL则标识了这些计算机上的资源。通常,URL是一个包含了传输协议、主机名称、服务端口号、文件路径名称,以及间隔符号“://”、“:”、“/” 等信息的字符串,例:https://www.cnblogs.com/ftl1012/p/9346858.html

为了方便程序员编程,JDK中提供了URL类,该类的全名是java.net.URL,该类用于使用它的各种方法来对URL对象进行分割、合并等处理,如图1-2所示。

URL有6种构造方法,通常使用了绝对URL路径构造方法,其中的URL参数是一个完整的URL字符串,而且必须要包含传输协议,如:

URL  raceHtml=new URL("https://www.cnblogs.com/ftl1012/p/9346858.html");

四、常用方法

1. InetAddress类主要方法

byte[]

getAddress() 返回该对象的原始IP。

static InetAddress[]

getAllByName(String host) 获得指定主机域名的所有IP地址。

static InetAddress

getByAddress(byte[] addr) 获得指定IP地址对象。

static InetAddress

getByAddress(String host, byte[] addr) 根据指定主机名和IP地址创建对象。

static InetAddress

getByName(String host) 根据指定主机名获得对象。

String

getCanonicalHostName() 获得指定域名的法定信息。

String

getHostAddress() 返回当前IP地址字符串。

String

getHostName() 获得当前IP地址的主机名。

static InetAddress

getLocalHost() 获得本地主机。

boolean

isLoopbackAddress() 判断IP是否为环回地址。

2. URL类主要方法

String

getAuthority() 获得URL的认证部分。

Object

getContent() 获得URL的内容。

Object

getContent(Class[] classes) 获得URL的内容

int

getDefaultPort()获得URL默认的端口号

String

getFile() 获得URL的文件名

String

getHost()获得该URL的主机名

String

getPath() 获得该URL的路径

int

getPort() 获得该URL的端口

String

getProtocol()获得该URL的协议。

String

getQuery()获得该URL的查询部分

String

getRef()获得该URL的锚部分

String

getUserInfo()获得该URL的用户信息

URLConnection

openConnection() 返回URL的连接。

InputStream

openStream()打开URL的输入读取流

代码示例

根据主机名查找IP

 import java.net.InetAddress;
import java.util.*; public class My
{
public static void main(String[] args)
{
String local = null ;
for(int i =18;i<20;i++){
local = "FF109Lx-"+i ;
try{
InetAddress inet = InetAddress.getByName(local) ;
byte[] ip = inet.getAddress();
System.out.print("FF109Lx-"+i);
for(int x=0;x<ip.length;x++){
int ips = (ip[x]>=0?ip[x]:(ip[x]+256)) ;
System.out.print(" "+ips+"." ) ;
}
}catch(Exception e ){
System.err.println("FF109Lx-"+i+" 获得本地信息失败..." );
System.out.println();
}
}
}
}

根据IP查找主机

 package com.hhh.ftl;
import java.net.InetAddress;
import java.util.*; public class Test
{
public static void main(String[] args)
{
String local = null ;
for(int i =100;i<102;i++){
local = "192.168.1."+i ;
try{
InetAddress inet = InetAddress.getByName(local) ;
byte[] ip = inet.getAddress();
System.out.print("FF109Lx-"+i);
for(int x=0;x<ip.length;x++){
int ips = (ip[x]>=0?ip[x]:(ip[x]+256)) ;
System.out.print(" "+ips+"." ) ;
}
System.out.println("");
}catch(Exception e ){
System.err.println("FF109Lx-"+i+" 获得本地信息失败..." );
}
}
}
}

采用多线程方式探测某主机上所有的TCP端口开放情况

 package com.hhh.ftl;

 import java.net.*;
import java.io.*;
import java.net.*; class Sum { // 共享资源,计数器count
private int count;// 共享资源 public int add() {
synchronized (this) { // 代码段1,共享锁,修饰程序段或者方法
count = count + 1;
System.out.println("add:" + count);
return count;
}
}
} class ScanThread implements Runnable {// 定义线程
private Sum sd;
private int sum = 0;
private String host; public ScanThread(String name, Sum sd, String host) {
this.sd = sd;
this.host = host;
} public void run() {// 必需的重写
int port;
try {
for (int i = 0; i < 65535; i++) {
port = sd.add();
if (port > 65535)
break;
try {
Socket cs = new Socket(host, port);
System.out.println("port" + port + "出于开放状态");
cs.close();
} catch (Exception e) {
System.err.println("port" + port + "不能连接");
}
Thread.sleep(100);
}
Thread.sleep(1000);
} catch (Exception e) {
System.err.println("sleep异常");
}
}
} public class ScanD1emo {
public static void main(String[] args) {
Sum sd = new Sum();// 代表共享资源的变量
ScanThread s1 = new ScanThread("线程1", sd, "localhost");// 创建完毕
ScanThread s2 = new ScanThread("线程2", sd, "localhost");
ScanThread s3 = new ScanThread("线程3", sd, "localhost");
Thread st1 = new Thread(s1);
Thread st2 = new Thread(s2);
Thread st3 = new Thread(s3);
try {
long begin = System.currentTimeMillis();
st1.start();// 使线程运行
st2.start();
st3.start(); st1.join();
st2.join();
st3.join();
long end = System.currentTimeMillis();
System.out.println("探测localhost的TCP端口,共耗时" + (end - begin) + "毫秒");
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}

使用InetAddress类,用于获得指定的计算机名称和IP地址

采用了URL类,用于解析在单行编辑框中输入的URL地址,分解出访问协议、主机名称、端口号以及文件路径名称等。

 package com.hhh.ftl;
import java.net.URL;
import java.net.MalformedURLException;
public class myURL{
URL url;
public myURL(String urlStr){
try{
url = new URL(urlStr);
}catch(Exception e){
System.err.println(e.getMessage());
}
}
public void resolve(){
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
System.out.println(url.getRef());
}
public static void main(String [] args){
myURL my = new myURL("https://www.cnblogs.com/ftl1012/p/9346858.html");
my.resolve();
}
}

图形化的URL输入和分析

 package com.hhh.ftl;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.*;
import java.awt.event.*;
public class getURLInformation extends Frame{
TextField tUrl = new TextField("输入URL地址");
List lItems = new List(20);
Button bOk = new Button("解 析");
Font f = new Font("Serif",Font.BOLD,30);
public getURLInformation(){
this.setLayout(new BorderLayout());
this.add(tUrl, BorderLayout.NORTH);
this.add(lItems, BorderLayout.CENTER);
this.add(bOk, BorderLayout.SOUTH);
tUrl.setFont(f);
lItems.setFont(f);
bOk.setFont(f);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
bOk.addActionListener(new Listener());
}
public static void main(String args[]){
System.out.println("Starting New...");
getURLInformation mainFrame = new getURLInformation();
mainFrame.setSize(400, 400);
mainFrame.setTitle("解析URL");
mainFrame.setVisible(true);
}
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
URL getURL = null; try{
getURL = new URL(tUrl.getText());
}catch(MalformedURLException e1){
System.out.println("URL解析错误" + e1);
}
lItems.add("Reference:" + getURL.getRef(), 0);
lItems.add("File Name:" + getURL.getFile(), 0);
lItems.add("File Path:" + getURL.getPath(), 0);
lItems.add("Host Port:" + getURL.getPort(), 0);
lItems.add("Host Name:" + getURL.getHost(), 0);
lItems.add("Protocol :" + getURL.getProtocol(), 0);
} } }

 通过URL获得网页上文本资源

 package com.hhh.ftl;
import java.io.*;
import java.net.*;
public class Test{
public static void main(String [] args) throws Exception{
String url = "https://www.cnblogs.com/ftl1012/p/9346858.html"; //自行输入URL,例如https://www.cnblogs.com/ftl1012/p/9346858.html;
InputStream in = (new URL(url)).openStream(); //获得指定URL的字节输入流
int c = -1;
while((c = in.read()) != -1){ //按字节的方式输入数据和输出数据
System.out.write(c);
} } }

 通过URL获得网页上图像资源

 import java.io.*;
import java.net.*;
class exp_4_8{
public static void main(String [] args){
try{
InputStream imageSource = new URL(网络图片的URL).openStream();
FileOutputStream myFile = new FileOutputStream("c://myLogo.gif");
BufferedOutputStream myBuffer = new BufferedOutputStream(myFile);
DataOutputStream myData = new DataOutputStream(myBuffer);
int ch;
while((ch = imageSource.read()) > -1){ //由网络输入数据
myData.write(ch); //将数据输出到文件中
}
myBuffer.flush(); //将文件缓存中数据写入文件
imageSource.close();
myFile.close();
myBuffer.close();
myData.close();
}catch(Exception e){
System.err.print(e.toString());
} } }

获得JVM系统信息,记录在当前运行该程序的JVM信息

利用URL类型实现从指定地址获得文本格式的HTML源文件

实现利用InetAddress.getByName()按计算机名称获得实验室局域网中所有开机主机名称和IP地址

实现利用InetAddress.getByName()按IP地址获得指定网络段所有开机主机名称/域名和IP地址

 package com.mc.test1;

 import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
public class Main { public static void main(String[] args) throws Exception { System.out.println("\n"+"------------------------------------------");
System.out.println("PIng时间为2秒") ;
for(int i=18;i<30;i++){
String local = "FF109Lx-"+i ;
try{
InetAddress inet = InetAddress.getByName(local);
System.out.println(inet);
System.out.println("局域网内的主机名称:"+inet.getHostName()); //获得主机名称
byte [] ip = inet.getAddress(); //获得主机IP地址
for(int x=0;x<ip.length;x++){
int ips = (ip[x] >=0) ? ip[x] : (ip[i] + 256); //将byte数值转换为int数值
System.out.print(ips + ".");
}
inet.isReachable(2000) ;
}catch(Exception e){
System.err.println("获得信息失败" + e.toString());
}
} }
}

实现1-30的累计,其中共享资源池为1-30的数字的递增,采用了同步锁synchronized【待修改】

 class Sum{ //共享资源,计数器count
private int count;//共享资源
public int add(){
synchronized(this){ //代码段1,共享锁,修饰程序段或者方法
count = count + 1;
System.out.println("add:" + count);
return count;
}
}
}
class SumThread implements Runnable{//定义线程
private Sum sd;
private int sum = 0;
private int [] a = new int[10]; public SumThread(String name, Sum sd){
super(name);
this.sd = sd;
}
public void run(){//必需的重写
try{
for(int i=0;i<10;i++){
a[i] = sd.add();
sum += a[i];
Thread.sleep(100);
}
Thread.sleep(1000);
}catch(Exception e){} System.out.println(getName() + "累加和:" + sum);
}
public void showData(){
System.out.print(getName() + "获得的数为");
for(int i=0;i<10;i++){
if(i%10==0)System.out.println();
System.out.print(a[i] + "+\t");
}
}
public int getSum(){
return sum;
}
}
public class SumDemo{
public static void main(String [] args){
Sum sd = new Sum();//代表共享资源的变量
SumThread s1 = new SumThread("线程1",sd);//创建完毕
SumThread s2 = new SumThread("线程2",sd);
SumThread s3 = new SumThread("线程3",sd);
Thread st1 = new Thread(s1);
Thread st2 = new Thread(s2);
Thread st3 = new Thread(s3);
st1.setPriority(Thread.MAX_PRIORITY); //代码段2
st2.setPriority(10);
st3.setPriority(1);
long begin = System.currentTimeMillis();
st1.start();//使线程运行
st2.start(); st3.start();
St1.join(); st2.join(); st3.join();
st1.showData();
st2.showData();
st3.showData();
System.out.println("总和为:" + (st1.getSum() + st2.getSum() + st3.getSum()));
long end = System.currentTimeMillis();
System.out.println(“探测localhost的TCP端口,共耗时” +
( end - begin)+"毫秒");
} }

Java学习---InetAddress类的学习的更多相关文章

  1. java中Inetaddress类

    InetAddress类 InetAddress类用来封装我们前面讨论的数字式的IP地址和该地址的域名. 你通过一个IP主机名与这个类发生作用,IP主机名比它的IP地址用起来更简便更容易理解. Ine ...

  2. IP和java.net.InetAddress类的使用

    一.IP 1.地址格式 互联网上每一台计算机都有一个唯一标示自己的标记,这个标记就是IP地址.IP 地址使用32 位长度二进制数据标示,一般在实际中看到的大部分IP地址都是以十进制的数据形式标示的,如 ...

  3. Android(java)学习笔记79:java中InetAddress类概述和使用

    要想让网络中的计算机能够互相通信,必须为每台计算机指定一个标识号,通过这个标识号来指定要接受数据的计算机和识别发送的计算机. 在TCP/IP协议中,这个标识号就是IP地址. 那么,我们如果获取和操作I ...

  4. Android(java)学习笔记19:Java中InetAddress类概述和使用

    1. 要想让网络中的计算机能够互相通信,必须为每台计算机指定一个标识号,通过这个标识号来指定要接受数据的计算机和识别发送的计算机. 在TCP/IP协议中,这个标识号就是IP地址. 那么,我们如果获取和 ...

  5. Java中Properties类的学习总结

    学习目标: 1.认识properties文件,理解其含义,会正确创建properties文件. 2.会使用java.util.Properties类来操作properties文件. 一.认识prope ...

  6. java中大数类的学习

    java中提供了大数类BigInteger和BigDecimal分别表示大整数类和大浮点数类,这两个类都在java.math.*包中,因此每次必须在开头处引用该包. 一.BigInteger构造函数: ...

  7. Java Socket InetAddress类 Socket DatagramPacket TCP、UDP示例

    java.net :为实现网络应用程序提供类. InetAddress类 方法摘要 方法摘要 boolean equals(Object obj) : 将此对象与指定对象比较. byte[] getA ...

  8. 关于java中的类的学习

    设计模式应该牵扯到类的分布排列了,尽管现在我只能这么表达. 下面来自段帅发来的视频课程中的整理: 类与类之间的关系 每天进步一点点 类是java程序中最小组成单位,要理解后才可以更能理解类继承,重载, ...

  9. [java语言]——InetAddress类的getByName()方法

    InetAddress---表示互联网协议(IP)地址 ---InetAddress.getByName("www.163.com")----在给定主机名的情况下确定主机的IP地址 ...

随机推荐

  1. .net core 多平台部署

    首先下载地址 https://dotnet.microsoft.com/download 下载.net core 和  .net core runtime  然后安装他们 控制台运行项目: 找到你的工 ...

  2. latex排版(CTeX winEdit输出“系统找不到指定的文件”的终极解决办法)

    WinEdit的菜单栏中:Options->Execution Modes 弹出的界面见下面的图: 看上图中“1”所示位置,后面3项出现了“?”号(出现“系统找不到指定的文件”的错误时,所有项后 ...

  3. Diskrete Mathematik

    1.Aussagenlogik 1.1 Gleichwertiges Kalkül 1.2 Normalform Einfache Disjunktion besteht aus Disjunktio ...

  4. Java - 用静态工厂方法代替构造器

    Effective Item - 考虑用静态工厂方法代替构造器我们有两种常见的方法获得一个类的实例: 公有的构造器 提供静态工厂方法(static factory method) 相对公有的构造器,静 ...

  5. JAVA注释方式

    1.单行(single-line)注释    //…… 2.块(block)注释                /*……*/ 3.文档注释                      /**……*/

  6. Spring动态注册bean实现动态多数据源

    Spring动态注册bean实现动态多数据源 http://blog.csdn.net/littlechang/article/details/8071882

  7. HTML是什么与基础格式

    html 又称 超文本标记语言. 网页的本质其实就是html代码,通过浏览器,将html转换翻译成用户可以看得懂的展现丰富的页面. 所以制作网站的本质,其实就是编写html代码. HTML基础格式 & ...

  8. js延迟加载优化页面响应速度

    网页打开速度是衡量网站性能的一个极为重要的指标,今天就来说说如何通过JS延迟加载的方式提高页面响应速度: JS延迟加载的 含义:即等页面加载完成之后再加载 JavaScript 文件.作用:JS延迟加 ...

  9. 自己写的一个nodejs查找文件模块-node-find-all-files

    最近在折腾着用node-webkit搭建一个工具,其中要查找路径下的所有文件然后再进行压缩等操作,于是进写了这样的一个模块.代码如下: /* 输入目录找出目录下的所有文件,包括文件夹 */ /* 依赖 ...

  10. P1025[SCOI2009]游戏

    windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按 顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们对应的数字.然后又在新的 ...