java.net |
Provides the classes for implementing networking applications.
The java.net package can be roughly divided in two sections:
-
A Low Level API, which deals with the following abstractions:
Addresses, which are networking identifiers, like IP addresses.
Sockets, which are basic bidirectional data communication mechanisms.
Interfaces, which describe network interfaces.
-
A High Level API, which deals with the following abstractions:
URIs, which represent Universal Resource Identifiers.
URLs, which represent Universal Resource Locators.
Connections, which represents connections to the resource pointed to by URLs.
Addresses
Addresses are used throughout the java.net APIs as either host identifiers, or socket endpoint identifiers.
The {@link java.net.InetAddress} class is the abstraction representing an IP (Internet Protocol) address. It has two subclasses:
- {@link java.net.Inet4Address} for IPv4 addresses.
- {@link java.net.Inet6Address} for IPv6 addresses.
But, in most cases, there is no need to deal directly with the subclasses, as the InetAddress abstraction should cover most of the needed functionality.
About IPv6
Not all systems have support for the IPv6 protocol, and while the Java networking stack will attempt to detect it and use it transparently when available, it is also possible to disable its use with a system property. In the case where IPv6 is not available, or explicitly disabled, Inet6Address are not valid arguments for most networking operations any more. While methods like {@link java.net.InetAddress#getByName} are guaranteed not to return an Inet6Address when looking up host names, it is possible, by passing literals, to create such an object. In which case, most methods, when called with an Inet6Address will throw an Exception.
Sockets
Sockets are means to establish a communication link between machines over the network. The java.net package provides 4 kinds of Sockets:
- {@link java.net.Socket} is a TCP client API, and will typically be used to {@linkplain java.net.Socket#connect(SocketAddress) connect} to a remote host.
- {@link java.net.ServerSocket} is a TCP server API, and will typically {@linkplain java.net.ServerSocket#accept accept} connections from client sockets.
- {@link java.net.DatagramSocket} is a UDP endpoint API and is used to {@linkplain java.net.DatagramSocket#send send} and {@linkplain java.net.DatagramSocket#receive receive} {@linkplain java.net.DatagramPacket datagram packets}.
- {@link java.net.MulticastSocket} is a subclass of {@code DatagramSocket} used when dealing with multicast groups.
Sending and receiving with TCP sockets is done through InputStreams and OutputStreams which can be obtained via the {@link java.net.Socket#getInputStream} and {@link java.net.Socket#getOutputStream} methods.
Interfaces
The {@link java.net.NetworkInterface} class provides APIs to browse and query all the networking interfaces (e.g. ethernet connection or PPP endpoint) of the local machine. It is through that class that you can check if any of the local interfaces is configured to support IPv6.
High level API
A number of classes in the java.net package do provide for a much higher level of abstraction and allow for easy access to resources on the network. The classes are:
- {@link java.net.URI} is the class representing a Universal Resource Identifier, as specified in RFC 2396. As the name indicates, this is just an Identifier and doesn't provide directly the means to access the resource.
- {@link java.net.URL} is the class representing a Universal Resource Locator, which is both an older concept for URIs and a means to access the resources.
- {@link java.net.URLConnection} is created from a URL and is the communication link used to access the resource pointed by the URL. This abstract class will delegate most of the work to the underlying protocol handlers like http or ftp.
- {@link java.net.HttpURLConnection} is a subclass of URLConnection and provides some additional functionalities specific to the HTTP protocol.
The recommended usage is to use {@link java.net.URI} to identify resources, then convert it into a {@link java.net.URL} when it is time to access the resource. From that URL, you can either get the {@link java.net.URLConnection} for fine control, or get directly the InputStream.
Here is an example:
URI uri = new URI("http://java.sun.com/");
URL url = uri.toURL();
InputStream in = url.openStream();
Protocol Handlers
As mentioned, URL and URLConnection rely on protocol handlers which must be present, otherwise an Exception is thrown. This is the major difference with URIs which only identify resources, and therefore don't need to have access to the protocol handler. So, while it is possible to create an URI with any kind of protocol scheme (e.g. myproto://myhost.mydomain/resource/ ), a similar URL will try to instantiate the handler for the specified protocol; if it doesn't exist an exception will be thrown.
By default the protocol handlers are loaded dynamically from the default location. It is, however, possible to add to the search path by setting the java.protocol.handler.pkgs system property. For instance if it is set to myapp.protocols , then the URL code will try, in the case of http, first to load myapp.protocols.http.Handler , then, if this fails, http.Handler from the default location.
Note that the Handler class has to be a subclass of the abstract class {@link java.net.URLStreamHandler}.
@since JDK1.0
|
Java Source File Name | Type | Comment |
AbstractPlainDatagramSocketImpl.java | Class | Abstract datagram and multicast socket implementation base class. |
AbstractPlainSocketImpl.java | Class | Default Socket Implementation. |
Authenticator.java | Class | The class Authenticator represents an object that knows how to obtain
authentication for a network connection. |
BindException.java | Class | Signals that an error occurred while attempting to bind a
socket to a local address and port. |
CacheRequest.java | Class | Represents channels for storing resources in the
ResponseCache. |
CacheResponse.java | Class | Represent channels for retrieving resources from the
ResponseCache. |
ConnectException.java | Class | Signals that an error occurred while attempting to connect a
socket to a remote address and port. |
ContentHandler.java | Class | The abstract class ContentHandler is the superclass
of all classes that read an Object from a
URLConnection . |
ContentHandlerFactory.java | Interface | This interface defines a factory for content handlers. |
CookieHandler.java | Class | A CookieHandler object provides a callback mechanism to hook up a
HTTP state management policy implementation into the HTTP protocol
handler. |
CookieManager.java | Class | CookieManager provides a concrete implementation of
CookieHandler ,
which separates the storage of cookies from the policy surrounding accepting
and rejecting cookies. |
CookiePolicy.java | Interface | CookiePolicy implementations decide which cookies should be accepted
and which should be rejected. |
CookieStore.java | Interface | A CookieStore object represents a storage for cookie. |
DatagramPacket.java | Class | This class represents a datagram packet. |
DatagramSocket.java | Class | This class represents a socket for sending and receiving datagram packets.
A datagram socket is the sending or receiving point for a packet
delivery service. |
DatagramSocketImpl.java | Class | Abstract datagram and multicast socket implementation base class. |
DatagramSocketImplFactory.java | Interface | This interface defines a factory for datagram socket implementations. |
FileNameMap.java | Interface | A simple interface which provides a mechanism to map
between a file name and a MIME type string.
version: 1.21, 05/05/07 author: Steven B. |
HttpCookie.java | Class | An HttpCookie object represents an http cookie, which carries state
information between server and user agent. |
HttpRetryException.java | Class | Thrown to indicate that a HTTP request needs to be retried
but cannot be retried automatically, due to streaming mode
being enabled. |
HttpURLConnection.java | Class | A URLConnection with support for HTTP-specific features. |
IDN.java | Class | Provides methods to convert internationalized domain names (IDNs) between
a normal Unicode representation and an ASCII Compatible Encoding (ACE) representation.
Internationalized domain names can use characters from the entire range of
Unicode, while traditional domain names are restricted to ASCII characters.
ACE is an encoding of Unicode strings that uses only ASCII characters and
can be used with software (such as the Domain Name System) that only
understands traditional domain names.
Internationalized domain names are defined in RFC 3490.
RFC 3490 defines two operations: ToASCII and ToUnicode. |
Inet4Address.java | Class | This class represents an Internet Protocol version 4 (IPv4) address.
Defined by
RFC 790: Assigned Numbers,
RFC 1918: Address Allocation for Private Internets,
and RFC 2365:
Administratively Scoped IP Multicast
Textual representation of IPv4 address used as input to methods
takes one of the following forms:
When four parts are specified, each is interpreted as a byte of
data and assigned, from left to right, to the four bytes of an IPv4
address.
When a three part address is specified, the last part is
interpreted as a 16-bit quantity and placed in the right most two
bytes of the network address. |
Inet4AddressImpl.java | Class | |
Inet6Address.java | Class | This class represents an Internet Protocol version 6 (IPv6) address.
Defined by
RFC 2373: IP Version 6 Addressing Architecture.
Textual representation of IPv6 address used as input to methods
takes one of the following forms:
The preferred form is x:x:x:x:x:x:x:x,
where the 'x's are
the hexadecimal values of the eight 16-bit pieces of the
address. |
Inet6AddressImpl.java | Class | |
InetAddress.java | Class | This class represents an Internet Protocol (IP) address.
An IP address is either a 32-bit or 128-bit unsigned number
used by IP, a lower-level protocol on which protocols like UDP and
TCP are built. |
InetAddressImpl.java | Interface | |
InetSocketAddress.java | Class | This class implements an IP Socket Address (IP address + port number)
It can also be a pair (hostname + port number), in which case an attempt
will be made to resolve the hostname. |
InterfaceAddress.java | Class | This class represents a Network Interface address. |
JarURLConnection.java | Class | A URL Connection to a Java ARchive (JAR) file or an entry in a JAR
file.
The syntax of a JAR URL is:
jar:<url>!/{entry}
for example:
jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class
Jar URLs should be used to refer to a JAR file or entries in
a JAR file. |
MalformedURLException.java | Class | Thrown to indicate that a malformed URL has occurred. |
MulticastSocket.java | Class | The multicast datagram socket class is useful for sending
and receiving IP multicast packets. |
NetPermission.java | Class | This class is for various network permissions.
A NetPermission contains a name (also referred to as a "target name") but
no actions list; you either have the named permission
or you don't.
The target name is the name of the network permission (see below). |
NetworkInterface.java | Class | This class represents a Network Interface made up of a name,
and a list of IP addresses assigned to this interface.
It is used to identify the local interface on which a multicast group
is joined. |
NoRouteToHostException.java | Class | Signals that an error occurred while attempting to connect a
socket to a remote address and port. |
PasswordAuthentication.java | Class | The class PasswordAuthentication is a data holder that is used by
Authenticator. |
PortUnreachableException.java | Class | Signals that an ICMP Port Unreachable message has been
received on a connected datagram. |
ProtocolException.java | Class | Thrown to indicate that there is an error in the underlying
protocol, such as a TCP error. |
Proxy.java | Class | This class represents a proxy setting, typically a type (http, socks) and
a socket address. |
ProxySelector.java | Class | Selects the proxy server to use, if any, when connecting to the
network resource referenced by a URL. |
ResponseCache.java | Class | Represents implementations of URLConnection caches. |
SecureCacheResponse.java | Class | Represents a cache response originally retrieved through secure
means, such as TLS. |
ServerSocket.java | Class | This class implements server sockets. |
Socket.java | Class | This class implements client sockets (also called just
"sockets"). |
SocketAddress.java | Class | This class represents a Socket Address with no protocol attachment. |
SocketException.java | Class | Thrown to indicate that there is an error creating or accessing a Socket. |
SocketImpl.java | Class | The abstract class SocketImpl is a common superclass
of all classes that actually implement sockets. |
SocketImplFactory.java | Interface | This interface defines a factory for socket implementations. |
SocketInputStream.java | Class | This stream extends FileInputStream to implement a
SocketInputStream. |
SocketOptions.java | Interface | Interface of methods to get/set socket options. |
SocketOutputStream.java | Class | This stream extends FileOutputStream to implement a
SocketOutputStream. |
SocketPermission.java | Class | This class represents access to a network via sockets.
A SocketPermission consists of a
host specification and a set of "actions" specifying ways to
connect to that host. |
SocketTimeoutException.java | Class | Signals that a timeout has occurred on a socket read or accept. |
SocksConsts.java | Interface | Constants used by the SOCKS protocol implementation. |
SocksSocketImpl.java | Class | SOCKS (V4 & V5) TCP socket implementation (RFC 1928). |
UnknownHostException.java | Class | Thrown to indicate that the IP address of a host could not be determined. |
UnknownServiceException.java | Class | Thrown to indicate that an unknown service exception has
occurred. |
URI.java | Class | Represents a Uniform Resource Identifier (URI) reference.
Aside from some minor deviations noted below, an instance of this
class represents a URI reference as defined by
RFC 2396: Uniform
Resource Identifiers (URI): Generic Syntax, amended by RFC 2732: Format for
Literal IPv6 Addresses in URLs. |
URISyntaxException.java | Class | Checked exception thrown to indicate that a string could not be parsed as a
URI reference. |
URL.java | Class | Class URL represents a Uniform Resource
Locator, a pointer to a "resource" on the World
Wide Web. |
URLClassLoader.java | Class | This class loader is used to load classes and resources from a search
path of URLs referring to both JAR files and directories. |
URLConnection.java | Class | The abstract class URLConnection is the superclass
of all classes that represent a communications link between the
application and a URL. |
URLDecoder.java | Class | Utility class for HTML form decoding. |
URLEncoder.java | Class | Utility class for HTML form encoding. |
URLStreamHandler.java | Class | The abstract class URLStreamHandler is the common
superclass for all stream protocol handlers. |
URLStreamHandlerFactory.java | Interface | This interface defines a factory for URL stream
protocol handlers. |