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 isURL The 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 classContentHandler YesfromURLConnection ReadObject Of 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 classSocketImpl Is 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 |
ClassURL Represents 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 classURLConnection Is 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 classURLStreamHandler Is 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:
Address
In the entire java.net API, the address can be used as the host identifier or socket endpoint identifier.
InetAddress
A class is an abstraction of an IP (Internet Protocol) address. It has two subclasses:
- For IPv4 address
Inet4Address
.
- For IPv6 address
Inet6Address
.
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.getByName
Methods 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:
Socket
Is a tcp client API, which is usually used (java.net.Socket.connect(SocketAddress)
) Connect to the remote host.
ServerSocket
Is a TCP Server API, usually accept (java.net.ServerSocket.accept
) From the client socket connection.
DatagramSocket
Is a UDP endpoint API for sending and receivingjava.net.DatagramPackets
.
MulticastSocket
It 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.getInputStream
Andjava.net.Socket.getOutputStream
Method.
Interface
NetworkInterface
Class 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:
URI
Is 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.
URL
Is a class that represents a unified resource identifier. It is both an old concept of Uri and a method to access resources.
URLConnection
It 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.
HttpURLConnection
Is a subclass of urlconnection and provides some additional functions specific to the HTTP protocol.
We recommend that you useURI
Specify a resource and convert itURL
. You can obtain from this URLURLConnection
For 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.pkgs
The 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.Handler
And then, if it fails, try to load from the default locationhttp.Handler
.