Determining the IP Address & Hostname of Local Computer in Java
IP Address Stands for Internet Protocol Address, is a numeric label that is assigned uniquely to each device connected within a computer network that uses the internet protocol. An IP address serves two principal functions:
- It identifies the host, or more specifically its network interface.
- It provides the location of the host in the network, and thus the capability of establishing a path to that host.
The IP address also specifies the format of the data packets that are sent and received among routers and end systems. The IP address space is managed globally by the Internet Assigned Numbers Authority (IANA), and by five regional Internet registries (RIRs) responsible in their designated territories for assignment to local Internet registries, such as Internet service providers (ISPs), and other end users.
Two versions of IP addresses are most commonly seen in internet addressing these days. They are the IPv4 and IPv6.
- IPv4: IPv4 has a size of 32 bits, i.e., 4,294,967,296 addresses can be assigned using IPv4. IPv4 addresses are represented in dot-decimal notation, consisting of 4 decimal numbers separated by dots, each ranging from 0 to 255. Each part represents a group of 8 bits (an octet) of the address. i.e.192.168.0.118
- IPv6: In IPv6, the address size was increased from 32 bits in IPv4 to 128 bits, thus providing up to 2^128 (approximately 3.403×10^38) addresses. i.e. 2001:db8:0:1234:0:567:8:1
Host name is a name given to a device on the internet. i.e., a hostname is how a device can be referred to as on the internet (just like the names of a human). Hostnames may be simple names consisting of a single word or phrase, or they may be structured. i.e. LAPTOP-MRRIH5PC
The Java.net package provides several useful classes and interfaces to deal with networking and the internet. The IP address and hostname of any local computer can be determined using the java.net.InetAddress class.
Local HostAddress: 127.0.0.1 Local host name: localhost