Java.net package is composed of (i) classes, (ii) exceptions, and (iii) interfaces that offer a commanding infrastructure for Java networking.
- URL
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.
Methods
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.
- URLCONNECTION
The URLConnection class recovers the parts stated by URL elements.
Methods
- 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.
- Socket
A Socket class is the Java depiction of a TCP link. When a Socket is developed, a link is created to a particular destination.
Constructors
Socket constructors support 2 arguments:
- Host name and
- Port value.
Methods
- 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
ServerSocket denotes a listening TCP link. When a link is requested, this class will deliver a Socket component representing the link.
Constructors
The constructors get the port number as argument for listening the link requests.
Methods
- 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.
- DATAGRAMSOCKET
The DatagramSocket signifies a linkless datagram socket. Along with DatagramPacket, this class enables communication utilizing the UDP protocol.
Constructors
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.
Methods
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
DatagramPacket signifies a data packet transmitted by means of UDP protocol (with a DatagramSocket).
Constructors
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.
Methods
There are 4 methods in the class, they are:
- getData( ) – retrieves data
- getLength( ) – retrieves datagram length
- getAddress( ) – retrieves address
- getPort( ) – retrieves port number
- INETADDRESS
The InetAddress signifies a host name and its IP values. The class also offers the functionality for acquiring IP value.
Constructors
This class has no clear constructors. Alternatively, static methods getByName( ) and getAllByName( ), are used.
Methods
- 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
Leave a Reply