Skip to content

Files

Latest commit

 

History

History

01-InternetAddress

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Internet Addresses

IP (Internet Protocol) Address

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:

  • IPv4 (32-bit)
  • IPv6 (128-bit)

Domain Names

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

The InetAddress class in Java represents an Internet Protocol (IP) address. It encapsulates both the hostname and IP address of a network interface.

Creating new InetAddress objects

  • No public constructors
  • Use static factory methods directly
  • Automatically connect to DNS server to resolve the host name

@returns InetAddress object representing the IP address

@throws UnknownHostException if the host name cannot be resolved

Static Methods

  • 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

Example

    InetAddress address = InetAddress.getByName("www.tu.edu.np");

Getter Methods

  • public String getHostName(): Returns the hostname of this IP address
  • public String getHostAddress(): Returns the IP address string in textual presentation
  • public byte[] getAddress(): Returns the raw IP address in network byte order
  • public String getCanonicalHostName(): Gets the fully qualified domain name for this IP address

Address Types

Global > Organisation-Local > Site-Local > Link-Local > Interface-Local
  • boolean isAnyLocalAddress(): Utility routine to check if the InetAddress is a wildcard address

  • boolean isLinkLocalAddress(): Utility routine to check if the InetAddress is an IP multicast address

  • boolean isSiteLocalAddress(): Utility routine to check if the InetAddress is a site local 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 isMCSiteLocal(): Utility routine to check if the multicast address has site-local scope

Testing Reachability

  • boolean isReachable(int timeout): Test whether that address is reachable

  • boolean isReachable(NetworkInterface netif, int ttl, int timeout): Test whether that address is reachable

Object Methods

java.lang.Object
  • equals(Object o)
  • hashCode()
  • toString()

Inet4Address and Inet6Address

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

Network Interface

The NetworkInterface Class

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.

Creating New NetworkInterface Objects

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

Static Factory Methods

  • 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.

Getter Methods

  • String getName(): Returns the name of this network interface.

  • Enumeration<InetAddress> getInetAddresses(): Returns an enumeration of InetAddress objects bound to this network interface.