author | chegar |
Fri, 18 Oct 2019 21:25:01 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58697 | e3ff12d14d43 |
parent 52700 | b206bdfb9fe2 |
permissions | -rw-r--r-- |
19069 | 1 |
/* |
2 |
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
2 | 25 |
|
19069 | 26 |
/** |
27 |
* Provides the classes for implementing networking applications. |
|
28 |
* |
|
29 |
* <p> The java.net package can be roughly divided in two sections:</p> |
|
30 |
* <ul> |
|
31 |
* <li><p><i>A Low Level API</i>, which deals with the |
|
32 |
* following abstractions:</p> |
|
33 |
* <ul> |
|
34 |
* <li><p><i>Addresses</i>, which are networking identifiers, |
|
35 |
* like IP addresses.</p></li> |
|
36 |
* <li><p><i>Sockets</i>, which are basic bidirectional data communication |
|
37 |
* mechanisms.</p></li> |
|
38 |
* <li><p><i>Interfaces</i>, which describe network interfaces. </p></li> |
|
39 |
* </ul></li> |
|
40 |
* <li> <p><i>A High Level API</i>, which deals with the following |
|
41 |
* abstractions:</p> |
|
42 |
* <ul> |
|
43 |
* <li><p><i>URIs</i>, which represent |
|
44 |
* Universal Resource Identifiers.</p></li> |
|
45 |
* <li><p><i>URLs</i>, which represent |
|
46 |
* Universal Resource Locators.</p></li> |
|
47 |
* <li><p><i>Connections</i>, which represents connections to the resource |
|
48 |
* pointed to by <i>URLs</i>.</p></li> |
|
49 |
* </ul></li> |
|
50 |
* </ul> |
|
51 |
* <h2>Addresses</h2> |
|
52 |
* <p>Addresses are used throughout the java.net APIs as either host |
|
53 |
* identifiers, or socket endpoint identifiers.</p> |
|
54 |
* <p>The {@link java.net.InetAddress} class is the abstraction representing an |
|
55 |
* IP (Internet Protocol) address. It has two subclasses: |
|
56 |
* <ul> |
|
57 |
* <li>{@link java.net.Inet4Address} for IPv4 addresses.</li> |
|
58 |
* <li>{@link java.net.Inet6Address} for IPv6 addresses.</li> |
|
59 |
* </ul> |
|
60 |
* <p>But, in most cases, there is no need to deal directly with the subclasses, |
|
61 |
* as the InetAddress abstraction should cover most of the needed |
|
62 |
* functionality.</p> |
|
63 |
* <h3><b>About IPv6</b></h3> |
|
64 |
* <p>Not all systems have support for the IPv6 protocol, and while the Java |
|
65 |
* networking stack will attempt to detect it and use it transparently when |
|
66 |
* available, it is also possible to disable its use with a system property. |
|
67 |
* In the case where IPv6 is not available, or explicitly disabled, |
|
68 |
* Inet6Address are not valid arguments for most networking operations any |
|
69 |
* more. While methods like {@link java.net.InetAddress#getByName} are |
|
70 |
* guaranteed not to return an Inet6Address when looking up host names, it |
|
71 |
* is possible, by passing literals, to create such an object. In which |
|
72 |
* case, most methods, when called with an Inet6Address will throw an |
|
73 |
* Exception.</p> |
|
74 |
* <h2>Sockets</h2> |
|
75 |
* <p>Sockets are means to establish a communication link between machines over |
|
76 |
* the network. The java.net package provides 4 kinds of Sockets:</p> |
|
77 |
* <ul> |
|
78 |
* <li>{@link java.net.Socket} is a TCP client API, and will typically |
|
79 |
* be used to {@linkplain java.net.Socket#connect(SocketAddress) |
|
80 |
* connect} to a remote host.</li> |
|
81 |
* <li>{@link java.net.ServerSocket} is a TCP server API, and will |
|
82 |
* typically {@linkplain java.net.ServerSocket#accept accept} |
|
83 |
* connections from client sockets.</li> |
|
84 |
* <li>{@link java.net.DatagramSocket} is a UDP endpoint API and is used |
|
85 |
* to {@linkplain java.net.DatagramSocket#send send} and |
|
86 |
* {@linkplain java.net.DatagramSocket#receive receive} |
|
87 |
* {@linkplain java.net.DatagramPacket datagram packets}.</li> |
|
88 |
* <li>{@link java.net.MulticastSocket} is a subclass of |
|
89 |
* {@code DatagramSocket} used when dealing with multicast |
|
90 |
* groups.</li> |
|
91 |
* </ul> |
|
92 |
* <p>Sending and receiving with TCP sockets is done through InputStreams and |
|
93 |
* OutputStreams which can be obtained via the |
|
94 |
* {@link java.net.Socket#getInputStream} and |
|
95 |
* {@link java.net.Socket#getOutputStream} methods.</p> |
|
96 |
* <h2>Interfaces</h2> |
|
97 |
* <p>The {@link java.net.NetworkInterface} class provides APIs to browse and |
|
98 |
* query all the networking interfaces (e.g. ethernet connection or PPP |
|
99 |
* endpoint) of the local machine. It is through that class that you can |
|
100 |
* check if any of the local interfaces is configured to support IPv6.</p> |
|
101 |
* <p>Note, all conforming implementations must support at least one |
|
102 |
* {@code NetworkInterface} object, which must either be connected to a |
|
103 |
* network, or be a "loopback" interface that can only communicate with |
|
104 |
* entities on the same machine.</p> |
|
105 |
* |
|
106 |
* <h2>High level API</h2> |
|
107 |
* <p>A number of classes in the java.net package do provide for a much higher |
|
108 |
* level of abstraction and allow for easy access to resources on the |
|
109 |
* network. The classes are: |
|
110 |
* <ul> |
|
111 |
* <li>{@link java.net.URI} is the class representing a |
|
112 |
* Universal Resource Identifier, as specified in RFC 2396. |
|
113 |
* As the name indicates, this is just an Identifier and doesn't |
|
114 |
* provide directly the means to access the resource.</li> |
|
115 |
* <li>{@link java.net.URL} is the class representing a |
|
116 |
* Universal Resource Locator, which is both an older concept for |
|
117 |
* URIs and a means to access the resources.</li> |
|
118 |
* <li>{@link java.net.URLConnection} is created from a URL and is the |
|
119 |
* communication link used to access the resource pointed by the |
|
120 |
* URL. This abstract class will delegate most of the work to the |
|
121 |
* underlying protocol handlers like http or https.</li> |
|
122 |
* <li>{@link java.net.HttpURLConnection} is a subclass of URLConnection |
|
123 |
* and provides some additional functionalities specific to the |
|
49765 | 124 |
* HTTP protocol. This API has been superseded by the newer |
125 |
* {@linkplain java.net.http HTTP Client API}.</li> |
|
19069 | 126 |
* </ul> |
127 |
* <p>The recommended usage is to use {@link java.net.URI} to identify |
|
128 |
* resources, then convert it into a {@link java.net.URL} when it is time to |
|
129 |
* access the resource. From that URL, you can either get the |
|
130 |
* {@link java.net.URLConnection} for fine control, or get directly the |
|
21334 | 131 |
* InputStream. |
19069 | 132 |
* <p>Here is an example:</p> |
21334 | 133 |
* <pre> |
52700
b206bdfb9fe2
8213911: Use example.com in java.net and other examples
darcy
parents:
49765
diff
changeset
|
134 |
* URI uri = new URI("http://www.example.com/"); |
19069 | 135 |
* URL url = uri.toURL(); |
136 |
* InputStream in = url.openStream(); |
|
137 |
* </pre> |
|
138 |
* <h2>Protocol Handlers</h2> |
|
139 |
* As mentioned, URL and URLConnection rely on protocol handlers which must be |
|
140 |
* present, otherwise an Exception is thrown. This is the major difference with |
|
141 |
* URIs which only identify resources, and therefore don't need to have access |
|
142 |
* to the protocol handler. So, while it is possible to create an URI with any |
|
143 |
* kind of protocol scheme (e.g. {@code myproto://myhost.mydomain/resource/}), |
|
144 |
* a similar URL will try to instantiate the handler for the specified protocol; |
|
145 |
* if it doesn't exist an exception will be thrown. |
|
146 |
* <p>By default the protocol handlers are loaded dynamically from the default |
|
29097 | 147 |
* location. It is, however, possible to deploy additional protocols handlers |
148 |
* as {@link java.util.ServiceLoader services}. Service providers of type |
|
149 |
* {@linkplain java.net.spi.URLStreamHandlerProvider} are located at |
|
150 |
* runtime, as specified in the {@linkplain |
|
151 |
* java.net.URL#URL(String,String,int,String) URL constructor}. |
|
19069 | 152 |
* <h2>Additional Specification</h2> |
153 |
* <ul> |
|
154 |
* <li><a href="doc-files/net-properties.html"> |
|
155 |
* Networking System Properties</a></li> |
|
156 |
* </ul> |
|
157 |
* |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
21334
diff
changeset
|
158 |
* @since 1.0 |
19069 | 159 |
*/ |
160 |
package java.net; |