How to find your IP address and computer/host name
The InetAddress class can be used to perform Domain Name Server (DNS) lookups. The host name can either be a machine name, such as "mail.yahoo.com", or a textual representation of its IP address . The java.net.InetAddress class provides methods to get the IP of any host name for example www.yahoo.com, www.facebook.com etc.
InetAddress has no public constructor, so you must obtain instances via a set of static methods.
Java InetAddress Class is used to encapsulate the two thing.
- Numeric IP Address
- The domain name for that address
1. getLocalHost(): getLocalHost method returns the InetAddress object that represents the local host contain the name and address both. If this method unable to find out the host name, it throw an UnknownHostException .
2. getByName(): getByName method returns an InetAddress for a host name passed to it as a parameter argument. If this method unable to find out the host name, it throw an UnknownHostException .