Java provides the java.net package for developing applications that communicate over a network. The package contains classes and interfaces for working with URLs, IP addresses, sockets, and other network-related operations.
In this chapter, you will learn about the java.net package and the commonly used classes and interfaces provided for network programming in Java.
The java.net package provides classes and interfaces for implementing network applications in Java. These classes can be used to establish network connections, communicate between applications, work with Internet addresses, and access resources through URLs.
Java programs can use the classes provided by this package to perform various network operations without directly handling the low-level details of network communication.
The java.net package contains several classes for performing network-related operations. Some commonly used classes are discussed below.
| Class | Description |
|---|---|
| Authenticator | For networking applications it is first important to fetch the authentication for a connection in a network. The object of the Authenticator class is responsible for obtaining the same. The class is declared as follows: public class Authenticator extends Object The class methods are inherited from the Object class. |
| CacheRequest | We all know about the cache memory, which is used for fast retrieval of information. The class CacheRequest does somewhat similar work by storing the responses in the CacheResponse class. The class is declared as follows: public abstract class CacheRequest extends Object The class methods are inherited from the Object class. |
| CacheResponse | The class aims at the retrieval of records from the CacheResponse. The class is declared as follows: public abstract class CacheResponse extends Object The class methods are inherited from the Object class. |
| ContentHandler | The connection in a network is established with the help of URL. The class ContentHandler is the superclass of the classes which are appointed for reading the objects from the URL. The class is declared as follows: public abstract class ContentHandler extends Object The class methods are inherited from the Object class. |
| CookieHandler | The class object is implied for the association of HTTP protocol handler with the HTTP state management policy implementation. The class is declared as follows: public abstract class CookieHandler extends Object The class methods are inherited from the Object class. |
| CookieManager | The class extends the CookieHandler class i.e. The CookieManager implements the CookieHnadler. The class is declared as follows: public abstract class CookieManager extends CookieHandler The class methods are inherited from the CookieHandler and Object class. |
| DatagramPacket | A datagram packet is rendered by the class. The class is declared as follows: public final class DatagramPacket extends Object The class methods are inherited from the Object class |
| DatagramSocket | For the transfer of datagram packets in a network an entity called as socket is required. The class provides the same. The class is declared as follows: public final class DatagramSocket extends Object implements Closeable The class methods are inherited from the Object class |
| DatagramSocketImpl | The class serves as the parent for the implementation of sockets and datagram. The class is declared as: public final class DatagramSocketImp extends Object implements SocketOptions The class methods are inherited from the Object class |
| InterfaceAddress | The address of the network interface is represented by this class. The class is declared as follows: public class InterfaceAddress extends Object The class methods are inherited from the Object class. |
| JarURLConnection | The class is responsible for the establishment of the URL connection to the JAR files. The class is declared as follows: public abstract class JarURLConnection extends URLConnection The class methods are inherited from the URLConnection and Object class. |
| MulticastSocket | For the transfer of Multicast IP packets the class is implied. The class is declared as follows: public abstract class MulticastSocket extends DatagramSocket. The class methods are inherited from the DatagramSocket and Object class. |
| InetSocketAddress | IP Socket Address is a combination of the IP address and the port number. The class provides the implementation of the same. It is declared as follows: public class InetSocketAddress extends Object implements SocketAdress The class methods are inherited from the Object class |
| InetAddress | IP(Internet Protocol) address is represented by the class. The class is declared as: public final class InetAddress extends Object implements Serializable The class methods are inherited from the Object class. |
| Inet4Address | The internet address is represented as the Internet Protocol version 6 i.e Inet6 address. The class is declared as: public final class Inet6Address extends InetAddress The class methods are inherited from the Object and InetAddress class. |
| Inet6Address | The internet address is represented as the Internet Protocol version 4 i.e Inet4 address. The class is declared as: public final class Inet4Address extends InetAddress The class methods are inherited from the Object and InetAddress class. |
| IDN | The class is implied for the conversion of IDN between the ACE and Unicode representation. The class is declared as: public final class IDN extends Object The class methods are inherited from the Object class. |
| HttpURLConnection | The class is implied for obtaining the URL connections and also has support for HTTP features. The class is declared as: public final class HttpURLConnection extends URLConnection The class methods are inherited from the Object and URLConnection class. |
| HttpCookie | We all have heard about cookies in the web browsers which are appointed for carrying of information related to states between the client and the server. The class represents such HTTP cookies. The class is declared as: public final class HttpCookie extends Object implements Cloneable The class methods are inherited from the Object class. |
| NetPermission | For providing various network related permissions the class is used. The class is declared as: public final class NetPermission extends BasePermission The class methods are inherited from the BasePermission class. |
| NetworkInterface | A network interface is representation is provided by the class. The class is declared as: public final class NetworkInterface extends Object The class methods are inherited from the Object class. |
| PasswordAuthentication | The class is used by the Authenticator as a data holder. The class is declared as: public final class PasswordAuthentication extends Object The class methods are inherited from the Object class. |
| Proxy | The class is used for proxy related settings. The class is declared as: public final class Proxy extends Object The class methods are inherited from the Object class. |
| ProxySelector | The class is used for the selection for the proxy server to be used. The class is declared as: public final class ProxySelector extends Object The class methods are inherited from the Object class. |
| ResponseCache | Caches which are linked to the URL connections are represented by this class. The class is declared as: public abstract class ResponseCache extends Object The class methods are inherited from the Object class. |
| SecureCacheResponse | A cache which is obtained through the use of secure means is represented by this class. The class is declared as: public abstract class SecureCacheResponse extends CacheResponse The class methods are inherited from the Object class. |
| ServerSocket | The class is implied for the implementation of server sockets. The class is declared as: public abstract class ServerSocket extends Object implements Cloaseable. The class methods are inherited from the Object class. |
| Socket | The sockets which are linked to the client side is implemented by the class. The class is declared as: public abstract class Socket extends Object implements Cloaseable. The class methods are inherited from the Object class. |
| SocketAddress | The class is implied for the representation of Socket Address. The class is declared as: public abstract class SocketAddress extends Object implements Serializable The class methods are inherited from the Object class. |
| SocketImpl | The class is a parent class of all the socket implementing classes. The class is declared as: public abstract class SocketImpl extends Object implements SocketOptions The class methods are inherited from the Object class. |
| SocketPermission | The class is implied for the provision of network access with the help of sockets. The class is declared as: public final class SocketPermission extends Permission implements Serializable The class methods are inherited from the Object and Permission class. |
| StandardSocketOptions | As implied by the class name, the class is used to provide the standard socket options. The class is declared as: public abstract class StandardSocketOptions extends Object The class methods are inherited from the Object class. |
| URI | The class is a representation of Uniform Resource Identifier. The class is declared as: public final class URI extends Object implements Serializable, Comparable The class methods are inherited from the Object class. |
| URL | The class is a representation of Uniform Resource Locator. The class is declared as: public final class URL extends Object implements Serializable The class methods are inherited from the Object class. |
| URLClassLoader | The class is used for loading of loading of various classes and resources specified with the search path. The class is declared as: public final class URL extends SecureClassLoader implements Closeable The class methods are inherited from the SecureClassLoader class. |
| URLConnection | The class is a parent class of the ones representing a communication between the application and an URL. The class is declared as: public final class URLConnection extends Object The class methods are inherited from the Object class. |
| URLDecoder | HTML forms are decoded by this class. The class is declared as: public final class URLDecoder extends Object The class methods are inherited from the Object class. |
| URLEncoder | HTML forms are encoded by this class. The class is declared as: public final class URLEncoder extends Object The class methods are inherited from the Object class. |
| URLStreamHandler | The class is a parent class of all the Stream Protocol Handlers. The class is declared as: public final class URLStreamHandler extends Object The class methods are inherited from the Object class. |
The java.net package provides classes for working with URLs and URIs. A URL identifies the location of a resource, while a URI identifies a resource or provides a way to represent a resource identifier.
These classes can be used to parse network resource addresses and obtain information such as the protocol, host, port, path, and query.
In the following example, the URL class is used to obtain information about a web address.
Java
import java.net.URL;
public class URLExample {
public static void main(String[] args) throws Exception {
URL url = new URL("https://www.example.com/index.html");
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Host: " + url.getHost());
System.out.println("Path: " + url.getPath());
}
}
Output:
Protocol: https Host: www.example.com Path: /index.html
The java.net package provides Socket and ServerSocket classes for client-server communication. A server can use ServerSocket to listen for connection requests, while a client can use Socket to establish a connection with the server.
In the following example, a Socket is created to establish a connection with a server running on the local computer.
Java
import java.net.Socket;
public class SocketExample {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5000);
System.out.println("Connection established.");
socket.close();
} catch (Exception e) {
System.out.println("Unable to connect to the server.");
}
}
}
Output:
Unable to connect to the server.
The InetAddress class can be used to obtain information about Internet addresses. It can resolve a host name to an IP address and can also provide the host name associated with an address.
In the following example, the InetAddress class is used to obtain the host name and IP address of the local computer.
Java
import java.net.InetAddress;
public class InetAddressExample {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getLocalHost();
System.out.println("Host Name: " + address.getHostName());
System.out.println("IP Address: " + address.getHostAddress());
}
}
Output:
Host Name: MyComputer IP Address: 192.168.1.5
The host name and IP address in the output depend on the computer and network configuration.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India