java网络编程读书笔记-Ch04 05

来源:互联网 发布:视频去水印软件 编辑:程序博客网 时间:2024/05/18 02:14

Internet Addresses

链接上网的设备被称为 nodes. Nodes that are computers are called hosts.

Domain Name System (DNS)

The InetAddress Class

java.net.InetAddress

The java.net.InetAddress class is Java’s high-level representation of an IP address, both IPv4 and IPv6. It is used by most of the other networking classes, including Socket, ServerSocket, URL, DatagramSocket, DatagramPacket, and more. Usually, it includes both a hostname and an IP address.

import java.net.*;public calss OReillByName{    public static void main(String[] args){        try{            InetAddress address = InetAddress.getByName("www.oreilly.com");            System.out.println(address);        }catch(UnknownHostException ex){            System.out.println("Could not find www.oreilly.com")        }    }}

结果

JunrdeMacBook-Pro:src junr$ javac OReillyByName.java JunrdeMacBook-Pro:src junr$ java OReillyByNamewww.oreilly.com/23.42.171.129

Caching

These times can be controlled by the system properties networkaddress.cache.ttl and networkaddress.cache.negative.ttl.

Getter Methods

InetAddress class 包含了四个getter方法。

public String getHostName()public String getCanonicalHostName()public byte[] getAddress()public String getHostAddress()

There are no corresponding setHostName() and setAddress() methods, which means that packages outside of java.net can’t change an InetAddress object’s fields behind its back. This makes InetAddress immutable and thus thread safe.

Ch05 URLs and URIs

A resource is a thing that is identified by a URI.

There is no specific syntax that applies to the scheme-specific parts of all URIs. However,
many have a hierarchical form, like this:
//authority/path?query
The authority part of the URI names the authority responsible for resolving the rest of the URI. For instance, the URI http://www.ietf.org/rfc/rfc3986.txt has the scheme http, the authority www.ietf.org, and the path /rfc/rfc3986.txt (initial slash included). This means the server at www.ietf.org is responsible for mapping the path /rfc/rfc3986.txt to a resource. This URI does not have a query part. The URI http://www.powells.com/cgi- bin/biblio?inkey=62-1565928709-0 has the scheme http, the authority www.po‐ wells.com, the path /cgi-bin/biblio, and the query inkey=62-1565928709-0. The URI urn:isbn:156592870 has the scheme urn but doesn’t follow the hierarchical //authority/ path?query form for scheme-specific parts.

Although most current examples of URIs use an Internet host as an authority, future schemes may not. However, if the authority is an Internet host, optional usernames and ports may also be provided to make the authority more specific. For example, the URI ftp://mp3:mp3@ci43198-a.ashvil1.nc.home.com:33/VanHalen-Jump.mp3 has the au‐ thority mp3:mp3@ci43198-a.ashvil1.nc.home.com:33. This authority has the username mp3, the password mp3, the host ci43198-a.ashvil1.nc.home.com, and the port 33. It has the scheme ftp and the path /VanHalen-Jump.mp3. (In most cases, including the pass‐ word in the URI is a big security hole unless, as here, you really do want everyone in the universe to know the password.)

If you don’t hexadecimally encode non-ASCII characters like this, but just include them directly, then instead of a URI you have an IRI (an Internationalized Resource Identi‐ fier). IRIs are easier to type and much easier to read, but a lot of software and protocols expect and support only ASCII URIs.

URLs

A URL is a URI that, as well as identifying a resource, provides a specific network location for the resource that a client can use to retrieve a representation of that resource.

In Java, it’s the difference between the java.net.URI class that only identifies resources and the java.net.URL class that can both identify and retrieve resources.

The syntax of a URL is:
protocol://userInfo@host:port/path?query#fragment

The host part of a URL is the name of the server that provides the resource you want. It can be a hostname such as www.oreilly.com or utopia.poly.edu or an IP address, such as 204.148.40.9 or 128.238.3.21.

The userInfo is optional login information for the server. If present, it contains a user‐ name and, rarely, a password.

The port number is also optional. It’s not necessary if the service is running on its default port (port 80 for HTTP servers).

Creating New URLs

public URL(String url) throws MalformedURLException public URL(String protocol, String hostname, String file)throws MalformedURLExceptionpublic URL(String protocol, String host, int port, String file)throws MalformedURLExceptionpublic URL(URL base, String relative) throws MalformedURLException

URI

URI voice = new URI("tel:+1-800-9988-9938");URIweb =newURI("http://www.xml.com/pub/a/2003/09/17/stax.html#id=_hbc"); URI book = new URI("urn:isbn:1-565-92870-9");

System Properties

The ProxySelector Class

public abstract List<Proxy> select(URI uri)