Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Networking: Internet Addressing and the InetAddress Class

Lesson 35 of 46 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

Networking: Internet Addressing and the InetAddress Class

Internet Addressing

Every device on a TCP/IP network is identified by an IP address:

VersionFormatExampleAddress Space
IPv44 octets, dotted decimal192.168.1.10~4.3 billion
IPv68 groups of hex digits2001:0db8::12^128

A port number (0–65535) identifies a specific process/service on a host. Together, IP address + port form a socket address (e.g., 192.168.1.10:8080).

The java.net Package

Java's java.net package provides classes for network programming: InetAddress, Socket, ServerSocket, URL, URLConnection, DatagramSocket, and DatagramPacket.

The InetAddress Class

InetAddress represents an IP address (IPv4 or IPv6) and encapsulates the corresponding hostname. It has no public constructor — objects are created only through factory methods.

Factory Methods (static)

MethodPurpose
static InetAddress getByName(String host)Resolves a hostname to an InetAddress
static InetAddress[] getAllByName(String host)Returns all addresses for a host (useful for load-balanced domains)
static InetAddress getLocalHost()Returns the address of the local machine
static InetAddress getByAddress(byte[] addr)Builds an address from raw bytes, skipping DNS lookup

Instance Methods

MethodPurpose
String getHostName()Returns the hostname
String getHostAddress()Returns the IP address as a string
byte[] getAddress()Returns the raw IP bytes
boolean isReachable(int timeout)Pings the host within a timeout
boolean isLoopbackAddress()True for 127.0.0.1 / ::1
boolean isMulticastAddress()True if the address is a multicast address

Example

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressDemo {
    public static void main(String[] args) throws UnknownHostException {
        // Factory method: resolve by hostname
        InetAddress site = InetAddress.getByName("www.example.com");
        System.out.println("Host: " + site.getHostName());
        System.out.println("IP:   " + site.getHostAddress());

        // Factory method: local machine
        InetAddress local = InetAddress.getLocalHost();
        System.out.println("Local host: " + local);

        // All addresses for a domain
        InetAddress[] all = InetAddress.getAllByName("www.example.com");
        for (InetAddress addr : all) {
            System.out.println("Resolved: " + addr.getHostAddress());
        }
    }
}
Key Takeaway: InetAddress is the foundation of Java networking — it resolves hostnames to IP addresses via factory methods (since there is no public constructor) and exposes address details via instance methods. Every socket-based class (Socket, ServerSocket, DatagramSocket) relies on it internally.