Java.net package is composed of (i) classes, (ii) exceptions, and (iii) interfaces that offer a commanding infrastructure for Java networking.
Uniform Resource Locator (URL) signifies a source on the WWW (World Wide Web). A URL can be split up into protocol (http, https, etc), host (authority), port, path (filename), reference and query.
The URL class offers:
- Methods for obtaining specific parts (protocol, port, etc) of the provided URL.
- Methods for deciding whether 2 URL element references have the identical content.
- Also openConnection( ) method delivers a URLConnection that offers a link to the distant content.
The URLConnection class recovers the parts stated by URL elements.
- The getContent( ) method is similar to URL class method.
- URLConnection class also offers methods for obtaining header field estimate. Ex: (i) getContentType( ), (ii) guessContentTypeFromStream( ).
- Methods getInputStream ( ) that reads information and getOutputStream ( ) for output are offered.
- Other methods handle the fixing or recovering operations of variables.
A Socket class is the Java depiction of a TCP link. When a Socket is developed, a link is created to a particular destination.
Socket constructors support 2 arguments:
- Host name and
- Port value.
- Methods (i) getInputStream( ), (ii) getOutputStream( ), deliver stream elements that exchange information via socket.
- Close method orders the primary operating system to shutdown.
- Few methods obtain information about the link.
ServerSocket denotes a listening TCP link. When a link is requested, this class will deliver a Socket component representing the link.
The constructors get the port number as argument for listening the link requests.
- The accept( ) method stops the requesting thread until a link is accepted.
- The close( ) method instructs the system to prevent listening to requests on the socket.
- There are also methods to recover the host name and port number.
The DatagramSocket signifies a linkless datagram socket. Along with DatagramPacket, this class enables communication utilizing the UDP protocol.
As UDP is linkless, port number alone is required and host name is not required for developing a DatagramSocket. There is another constructor that does not obtain arguments.
Every method acquires a properly created DatagramPacket.
- send( ) – sends the packet data to the given host and port.
- receive( ) – stops processing until a packet is obtained by the socket.
- close( ) – requests the socket to shutdown.
- getLocalPort( ) – submits the local port number linked with the socket.
DatagramPacket signifies a data packet transmitted by means of UDP protocol (with a DatagramSocket).
The DatagramPacket offers 2 constructors for:
- Incoming packets that picks a byte array as argument and
- Outgoing packets that picks distant host name and port number as arguments.
There are 4 methods in the class, they are:
- getData( ) – retrieves data
- getLength( ) – retrieves datagram length
- getAddress( ) – retrieves address
- getPort( ) – retrieves port number
The InetAddress signifies a host name and its IP values. The class also offers the functionality for acquiring IP value.
This class has no clear constructors. Alternatively, static methods getByName( ) and getAllByName( ), are used.
- getHostName( ) – delivers a host name that the InetAddress specifies
- getAddress( ) – delivers an array of the raw bytes of address
- equals( ) – compares the address objects
- toString( ) – prints host name and IP address