Java.net API document

Source: Internet
Author: User
Software Package java.net

Provides classes for network applications.

See:
Description

Interface Abstract
Contenthandlerfactory This interface defines the factory of the content processing program.
Datagramsocketimplfactory This interface defines the factory used for implementing the datagram socket.
Filenamemap Provides a simple interface for ing between file names and mime-type strings.
Socketimplfactory This interface defines the factory used for socket implementation.
Socketoptions Interface for obtaining/setting SOCKET options.
Urlstreamhandlerfactory This interface isURLThe stream protocol handler defines a factory.

Class Abstract
Authenticator The Authenticator class indicates how to obtain the object for network connection authentication.
Cacherequest The channel for storing resources in responsecache.
Cacheresponse The channel for retrieving resources from responsecache.
Contenthandler Abstract classContentHandlerYesfromURLConnectionReadObjectOf all classes.
Cookiehandler The cookiehandler object provides a callback mechanism to hook HTTP status management policies to HTTP processing programs.
Datagrampacket This class indicates a data packet.
Datagramsocket This class indicates the socket used to send and receive the datagram packet.
Datagramsocketimpl The abstract base class implemented by the datagram and multicast socket.
Httpurlconnection Supports urlconnection with specific HTTP functions.
Inet4address This class indicates the Internet Protocol Version 4 (IPv4) address.
Inet6address This type indicates the Internet Protocol version 6th (IPv6) address.
Inetaddress This type indicates the Internet Protocol (IP) address.
Inetsocketaddress This class implements the IP socket address (IP address + port number ).
Jarurlconnection Connect to the URL Connection of entries in the Java archive (jar) file or jar file.
Multicastsocket The multicast datagram socket class is used to send and receive IP multicast packets.
Netpermission This class can be used for various network permissions.
Networkinterface This class represents a network interface consisting of a name and a list of IP addresses assigned to this interface.
Passwordauthentication The passwordauthentication class is the data holder for authenticator.
Proxy This class indicates the proxy settings, usually the type (HTTP, socks) and socket address.
Proxyselector When connecting to the network resource referenced by the URL, select the proxy server to be used (if any ).
Responsecache Indicates the implementation of the urlconnection cache.
Securecacheresponse Indicates the cache response initially obtained through security methods (such as TLS.
Serversocket This class implements server sockets.
Socket This kind of client socket can also be called "socket ").
Socketaddress This class indicates the socket address without any protocol attachment.
Socketimpl Abstract classSocketImplIs the General superclass of all classes actually implementing sockets.
Socketpermission This class Indicates access to the network through a socket.
Uri Indicates a Uniform Resource Identifier (URI) reference.
URL ClassURLRepresents a unified resource identifier, which is a pointer to the Internet "resource.
Urlclassloader This class loader is used to load classes and resources from the search path pointing to the JAR file and directory URL.
Urlconnection Abstract classURLConnectionIs the superclass of all classes, which represents the communication link between the application and the URL.
Urldecoder A utility class for HTML Format Decoding.
Urlencoder A utility class for HTML format encoding.
Urlstreamhandler Abstract classURLStreamHandlerIs the generic superclass of all stream protocol processors.

Enumeration Summary
Authenticator. requestortype Type of the entity requesting authentication.
Proxy. Type Indicates the proxy type.

Exception Summary
Bindexception This exception occurs when you try to bind a socket to a local address and port.
Connectexception This exception is thrown when an error occurs when you try to connect the socket to the remote address and port.
Httpretryexception If this exception is thrown, the HTTP request needs to be retried. However, the HTTP request cannot be retried automatically because the stream mode is enabled.
Malformedurlexception If this exception is thrown, the URL is incorrect.
Noroutetohostexception This exception is thrown when an error occurs when you try to connect the socket to the remote address and port.
Portunreachableexception This exception is thrown when the connection data report receives the ICMP port Unreachable message.
Protocolexception Throw this exception to indicate an error in the underlying protocol, such as a TCP error.
Socketexception Throw this exception to indicate an error in the underlying protocol, such as a TCP error.
Sockettimeoutexception If a timeout occurs when the socket is read or accepted, this exception is thrown.
Unknownhostexception An exception that indicates that the Host IP address cannot be determined.
Unknownserviceexception If this exception is thrown, an unknown service exception occurs.
Urisyntaxexception Throw an exception that indicates that the string cannot be parsed as a URI reference.

Description of the software package java.net

Provides classes for network applications.

The java.net package can be roughly divided into two parts:

  • Low-level APITo process the following Abstraction:

    • Address, That is, the network identifier, such as the IP address.

    • SocketThat is, the basic two-way data communication mechanism.

    • InterfaceDescribes network interfaces.

  • Advanced APIsTo process the following Abstraction:

    • UriIndicates the uniform resource identifier.

    • URLIndicates a uniform resource identifier.

    • Connection,URLThe connection to the resource.

Address

In the entire java.net API, the address can be used as the host identifier or socket endpoint identifier.

InetAddressA class is an abstraction of an IP (Internet Protocol) address. It has two subclasses:

  • For IPv4 addressInet4Address.
  • For IPv6 addressInet6Address.

However, in most cases, classes do not have to be processed directly, because the inetaddress abstraction should overwrite most of the required functions.

About IPv6

Not all systems support the IPv6 protocol. When the Java Network Connection stack tries to detect it and use it transparently when it is available, it can also be disabled using the system attribute. When IPv6 is unavailable or explicitly disabled, inet6address is no longer a valid parameter for most network connection operations. Although it can be ensured thatjava.net.InetAddress.getByNameMethods like this do not return inet6address, but they may still be created by passing the literal value. In this case, an exception is thrown when most methods are called using inet6address.

Socket

A socket is used to establish a communication link between machines on the network. The java.net package provides four sockets:

  • SocketIs a tcp client API, which is usually used (java.net.Socket.connect(SocketAddress)) Connect to the remote host.
  • ServerSocketIs a TCP Server API, usually accept (java.net.ServerSocket.accept) From the client socket connection.
  • DatagramSocketIs a UDP endpoint API for sending and receivingjava.net.DatagramPackets.
  • MulticastSocketIt is a subclass of datagramsocket and used when processing multicast groups.

The inputstream and outputstream must be used to send and receive TCP sockets.java.net.Socket.getInputStreamAndjava.net.Socket.getOutputStreamMethod.

Interface

NetworkInterfaceClass provides APIs to browse and query all network interfaces of local machines (for example, Ethernet connections or PPP endpoints ). Only through this class can you check whether all local interfaces are configured to support IPv6.

Advanced APIs

Many classes in the java.net package can provide more advanced abstraction, allowing convenient access to resources on the network. These classes are:

  • URIIs a class that represents the Unified Data identifier specified in RFC 2396. As the name suggests, it is just an identifier and does not directly provide access to resources.
  • URLIs a class that represents a unified resource identifier. It is both an old concept of Uri and a method to access resources.
  • URLConnectionIt is created based on the URL and is used to access the communication link of the resource pointed to by the URL. This abstract class delegates most of the work to underlying protocol handlers, such as HTTP or FTP.
  • HttpURLConnectionIs a subclass of urlconnection and provides some additional functions specific to the HTTP protocol.

We recommend that you useURISpecify a resource and convert itURL. You can obtain from this URLURLConnectionFor better control, you can also directly obtain inputstream.

 

The following is an example:

URI uri = new URI("http://java.sun.com/");
URL url = uri.toURL();
InputStream in = url.openStream();

Protocol handler

As mentioned above, both URL and urlconnection depend on the protocol handler, so the protocol handler must exist; otherwise, an exception is thrown. This is the main difference with Uri. Uri only identifies resources, so you do not need to access protocol handlers. Therefore, although any protocol scheme (for example,myproto://myhost.mydomain/resource/) Create a URI, but similar URLs will still try to instantiate the handler of the specified protocol; if the handler of the specified protocol does not exist, an exception is thrown.

 

By default, protocol handlers are dynamically loaded from the default location. However, by settingjava.protocol.handler.pkgsThe search path may also be added to system properties. For example, if you set itmyapp.protocols, Then the URL code will first try to load (for HTTP)myapp.protocols.http.HandlerAnd then, if it fails, try to load from the default locationhttp.Handler.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.