Java网络编程 Ch4 Internet地址

IPv4 4字节长,152.19.134.132 点分四段格式

IPv6 16字节长。

区分地址和主机名

地址—>DNS<—–主机名

InetAddress类
getByName() 提供一个IP地址串作为参数,为会请求的参数创建一个InetAddress对象,而不检查DNS(可能会为实际上不存在也无法链接的主机创建InetAddress对象)。对象的主机名初始设置为这个字符串。只有通过请求主机名getHostName时,才会完成主机名的DNS查找。

package Ch4;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class OReillyByName {

    public static void main(String[] args) {
        try{
          InetAddress address = InetAddress.getLocalHost();
            System.out.println(address);
            InetAddress address = InetAddress.getByName("www.oreilly.com");
 //可以看到address输出是url/ipv4地址
            System.out.println(address);
//getByName可以是url和ipv4.
            InetAddress address1 = InetAddress.getByName("208.201.239.100");
            System.out.println(address1.getHostName());
//url有时不止一个地址。getAllByName返回所有值。          
          try {
                InetAddress[] addresses = InetAddress.getAllByName("www.oreilly.com");
                for(InetAddress address2 : addresses){
                    System.out.println(address2);
                }
            }catch (UnknownHostException ex){
                System.out.println("Could not find www.oreilly.com");
            }
        }catch (UnknownHostException ex){
            System.out.println("Could not find www.oreilly.com");
        }
    }

}
/*
PYLC/10.192.30.89
* www.oreilly.com/23.66.36.204
208.201.239.100
*/
public String getHostName()主机名+IP地址(不知道主机名才联系DNS)
public String getCanonicalHostName()与DNS联系更积极
public String getHostAddress()点分四段式ip地址
public byte[] getAddress()字节数组形式的点分四段ip地址,可以用于确定IPv4还是6

没有更改InetAddress对象的方法,因此是线程安全的。

测试可达性

public boolean isReachable(int timeout) throws IOException
public boolean isReachable(NetworkInterface interface, int ttl, int timeout) throws IOException
使用traceroute测试

Object方法

public int hashCode() //根据地址计算
public boolean equals()//因此根据地址比较。主机名可以变。
toString() // 返回  主机名/点分四段地址

Inet4Address和Inet6Address 不需要了解

public final class Inet4Address extends InetAddress
public final class Inet6Address extends InetAddress

NetworkInerface 对象表示本地一个IP地址

物理硬件和虚拟地址

一些有用程序

SpamCheck 垃圾邮件

package Ch4;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class SpamCheck {

    public static final String BLACKHOLE = "sbl.spamhaus.org";

    public static void main(String[] args) throws UnknownHostException{
        for(String arg : args){
            if(isSpammer(arg)){
                System.out.println(arg+" is a known spammer.");
            }else{
                System.out.println(arg+" appears legitimate.");
            }
        }
    }

    private static boolean isSpammer(String arg){
        try{
          //逆置这个地址的字节,增加黑洞服务的域。找到说明是spammer
            InetAddress address = InetAddress.getByName(arg);
            byte[] quad = address.getAddress();
            String query = BLACKHOLE;
            for (byte octet : quad) {
                int unsignedByte = octet<0?octet+256:octet;
                query = unsignedByte+"."+query;
            }
            InetAddress.getByName(query);
            return true;
        }catch (UnknownHostException e){
            return false;
        }
    }
}
//207.34.56.23 appears legitimate.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值