An IP address is a unique address that identifies a device on the internet or a local network. IP addresses are typically in the format of a 32-bit number, represented as four decimal numbers separated by periods. Each decimal number represents 8 bits of the address.
Java supports two main types of IP addresses:
Resolved by DNS (Domain Name System) Servers
Domain names are human-readable names that correspond to the IP addresses of devices on the internet. Domain names are used in URLs to identify particular Web pages.
For example, in the URL http://www.example.com/index.html
, the domain name is example.com
.
The InetAddress
class in Java represents an Internet Protocol (IP) address. It encapsulates both the hostname and IP address of a network interface.
@returns
InetAddress
object representing the IP address
@throws
UnknownHostException
if the host name cannot be resolved
InetAddress.getByName(String host)
: lookup the name and the numeric address of the host
InetAddress.getAllByName(String host)
: lookup all the IP addresses of the host
InetAddress.getLocalHost()
: get the IP address of the local host
InetAddress.getLoopbackAddress()
: get the loopback address i.e. localhost/127.0.0.1
InetAddress.getByAddress(String host, byte[] addr)
: create InetAddress based on host and address
InetAddress.getByAddress(byte[] addr)
: create an InetAddress based on the raw IP address
InetAddress address = InetAddress.getByName("www.tu.edu.np");
public String getHostName()
: Returns the hostname of this IP addresspublic String getHostAddress()
: Returns the IP address string in textual presentationpublic byte[] getAddress()
: Returns the raw IP address in network byte orderpublic String getCanonicalHostName()
: Gets the fully qualified domain name for this IP addressGlobal > Organisation-Local > Site-Local > Link-Local > Interface-Local
` boolean isAnyLocalAddress()`: Utility routine to check if the InetAddress is a wildcard address
` boolean isLoopbackAddress()`: Utility routine to check if the InetAddress is a loopback address
` boolean isMulticastAddress()`: Utility routine to check if the InetAddress is a multicast address
` boolean isMCGlobal()`: Utility routine to check if the multicast address has global scope
` boolean isMCLinkLocal()`: Utility routine to check if the multicast address has link-local scope
` boolean isMCNodeLocal()`: Utility routine to check if the multicast address has node-local scope
` boolean isMCOrgLocal()`: Utility routine to check if the multicast address has organization-local scope
` boolean isReachable(int timeout)`: Test whether that address is reachable
` boolean isReachable(NetworkInterface netif, int ttl, int timeout)`: Test whether that address is reachable
java.lang.Object
equals(Object o)
hashCode()
toString()
public final class Inet4Address extends InetAddress
public final class Inet6Address extends InetAddress
Most of the time, you shouldn’t be concerned with whether an address is IPv4 or IPv6
Inet6Address.isIPv4CompatibleAddress()
: one new method
The NetworkInterface
class in Java represents a network interface, providing methods to access the details of the physical or logical interface, such as its name, addresses, and other attributes. It is used to identify and work with the local network interface to which your system is connected.
Similar to InetAddress
, the NetworkInterface
class does not have public constructors. You create instances of NetworkInterface
using static factory methods.
@returns
NetworkInterface
object representing the network interface
@throws
SocketException
if encounters a problem while locating the relevant network interface
NetworkInterface.getByName(String name)
: Returns a NetworkInterface object for the specified interface name.
NetworkInterface.getByInetAddress(InetAddress addr)
: Returns the NetworkInterface object that has the specified IP address bound to it.
NetworkInterface.getByIndex(int index)
: Returns the NetworkInterface object with the specified index.
String getName()
: Returns the name of this network interface.
Enumeration<InetAddress> getInetAddresses()
: Returns an enumeration of InetAddress objects bound to this network interface.