java.net

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » net » java.net 
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 NameTypeComment
AbstractPlainDatagramSocketImpl.javaClass Abstract datagram and multicast socket implementation base class.
AbstractPlainSocketImpl.javaClass Default Socket Implementation.
Authenticator.javaClass The class Authenticator represents an object that knows how to obtain authentication for a network connection.
BindException.javaClass Signals that an error occurred while attempting to bind a socket to a local address and port.
CacheRequest.javaClass Represents channels for storing resources in the ResponseCache.
CacheResponse.javaClass Represent channels for retrieving resources from the ResponseCache.
ConnectException.javaClass Signals that an error occurred while attempting to connect a socket to a remote address and port.
ContentHandler.javaClass The abstract class ContentHandler is the superclass of all classes that read an Object from a URLConnection.
ContentHandlerFactory.javaInterface This interface defines a factory for content handlers.
CookieHandler.javaClass A CookieHandler object provides a callback mechanism to hook up a HTTP state management policy implementation into the HTTP protocol handler.
CookieManager.javaClass CookieManager provides a concrete implementation of CookieHandler , which separates the storage of cookies from the policy surrounding accepting and rejecting cookies.
CookiePolicy.javaInterface CookiePolicy implementations decide which cookies should be accepted and which should be rejected.
CookieStore.javaInterface A CookieStore object represents a storage for cookie.
DatagramPacket.javaClass This class represents a datagram packet.
DatagramSocket.javaClass 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.javaClass Abstract datagram and multicast socket implementation base class.
DatagramSocketImplFactory.javaInterface This interface defines a factory for datagram socket implementations.
FileNameMap.javaInterface 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.javaClass An HttpCookie object represents an http cookie, which carries state information between server and user agent.
HttpRetryException.javaClass Thrown to indicate that a HTTP request needs to be retried but cannot be retried automatically, due to streaming mode being enabled.
HttpURLConnection.javaClass A URLConnection with support for HTTP-specific features.
IDN.javaClass 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.javaClass 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 IP addresses

Textual representation of IPv4 address used as input to methods takes one of the following forms:
d.d.d.d
d.d.d
d.d
d

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.javaClass
Inet6Address.javaClass This class represents an Internet Protocol version 6 (IPv6) address. Defined by RFC 2373: IP Version 6 Addressing Architecture.

Textual representation of IP addresses

Textual representation of IPv6 address used as input to methods takes one of the following forms:
  1. 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.javaClass
InetAddress.javaClass 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.javaInterface
InetSocketAddress.javaClass 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.javaClass This class represents a Network Interface address.
JarURLConnection.javaClass 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.javaClass Thrown to indicate that a malformed URL has occurred.
MulticastSocket.javaClass The multicast datagram socket class is useful for sending and receiving IP multicast packets.
NetPermission.javaClass 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.javaClass 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.javaClass Signals that an error occurred while attempting to connect a socket to a remote address and port.
PasswordAuthentication.javaClass The class PasswordAuthentication is a data holder that is used by Authenticator.
PortUnreachableException.javaClass Signals that an ICMP Port Unreachable message has been received on a connected datagram.
ProtocolException.javaClass Thrown to indicate that there is an error in the underlying protocol, such as a TCP error.
Proxy.javaClass This class represents a proxy setting, typically a type (http, socks) and a socket address.
ProxySelector.javaClass Selects the proxy server to use, if any, when connecting to the network resource referenced by a URL.
ResponseCache.javaClass Represents implementations of URLConnection caches.
SecureCacheResponse.javaClass Represents a cache response originally retrieved through secure means, such as TLS.
ServerSocket.javaClass This class implements server sockets.
Socket.javaClass This class implements client sockets (also called just "sockets").
SocketAddress.javaClass This class represents a Socket Address with no protocol attachment.
SocketException.javaClass Thrown to indicate that there is an error creating or accessing a Socket.
SocketImpl.javaClass The abstract class SocketImpl is a common superclass of all classes that actually implement sockets.
SocketImplFactory.javaInterface This interface defines a factory for socket implementations.
SocketInputStream.javaClass This stream extends FileInputStream to implement a SocketInputStream.
SocketOptions.javaInterface Interface of methods to get/set socket options.
SocketOutputStream.javaClass This stream extends FileOutputStream to implement a SocketOutputStream.
SocketPermission.javaClass 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.javaClass Signals that a timeout has occurred on a socket read or accept.
SocksConsts.javaInterface Constants used by the SOCKS protocol implementation.
SocksSocketImpl.javaClass SOCKS (V4 & V5) TCP socket implementation (RFC 1928).
UnknownHostException.javaClass Thrown to indicate that the IP address of a host could not be determined.
UnknownServiceException.javaClass Thrown to indicate that an unknown service exception has occurred.
URI.javaClass 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.javaClass Checked exception thrown to indicate that a string could not be parsed as a URI reference.
URL.javaClass Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web.
URLClassLoader.javaClass This class loader is used to load classes and resources from a search path of URLs referring to both JAR files and directories.
URLConnection.javaClass The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL.
URLDecoder.javaClass Utility class for HTML form decoding.
URLEncoder.javaClass Utility class for HTML form encoding.
URLStreamHandler.javaClass The abstract class URLStreamHandler is the common superclass for all stream protocol handlers.
URLStreamHandlerFactory.javaInterface This interface defines a factory for URL stream protocol handlers.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.