Wednesday, August 4, 2021

How to find IP Address of localhost in Java? Example Tutorial

How to find IP address of localhost from Java program
Java networking API provides a method to find the IP address of localhost from Java program by using java.net. InetAddress class. It’s rare when you need an IP address for the local host in the Java program. Mostly I used the Unix command to find the IP address of localhost. For all practical purposes where the program doesn’t need an IP address but you need to troubleshoot any networking issues, Use DOS or windows command or Use Linux commands. Recently one of my friends faced this question in a core Java interview, where they are expecting Java developer with some socket programming experience, But until you know or you have done it before its hard to answer this fact-based question, which motivates me to write this post.

In In this Java tutorial we will see How to find the IP address of localhost from the Java program. By the way, it’s also good to remember list of Unix networking commands to troubleshoot any networking issues related to Java application in Unix environment.

IP Address of localhost from Java program

As I said InetAddress from java.net package is used to represent an IP address in Java. an IP address is a 32 or 128-bit an unsigned number used by IP protocol which is the backbone of many popular protocols like TCP and UDP. 

There are two kinds of IP address IPv4 and IPv6 and IP the address is associated with a host which can be found by the hostname resolution process. 

Hostname resolution is performed by combining local machine configuration and network naming services such as the  DNS(Domain name system) and NIS(Network Information Service). InetAddress has a method to resolve hostname and IP address and vice versa. Here is a complete code example of finding IP addresses from Java program.



import java.net.UnknownHostException;

/**
 * Simple Java program to find IP Address of localhost. This program uses
 * InetAddress from java.net package to find IP address.
 *
 * @author Javin Paul
 */

public class IPTest {
 
 
    public static void main(String args[]) throws UnknownHostException {
   
        InetAddress addr = InetAddress.getLocalHost();
     
        //Getting IPAddress of localhost - getHostAddress return IP Address
        // in textual format
        String ipAddress = addr.getHostAddress();
     
        System.out.println("IP address of localhost from Java Program: " + ipAddress);
     
        //Hostname
        String hostname = addr.getHostName();
        System.out.println("Name of hostname : " + hostname);
     
    }
 
}

Output:
IP address of localhost from Java Program: 190.12.209.123
Name of hostname: PCLOND3433


How to find IP address of localhost in Java programThat’s all on How to find the IP address of the localhost from Java. It's a nice tip to know but as I said java.net is not a common package like java.lang or java.util. The best way to learn and remember networking concepts in Java is to write some client-server program that uses these essential classes.



Other Java Fundamentals tutorials from Javarevisited

5 comments :

Unknown said...

also, if your computer have more then one IP you can find all of them with these lines of code


InetAddress localhost = InetAddress.getLocalHost();
List adresses = InetAddress.getAllByName(localhost.getCanonicalHostName())

Unknown said...

Nice example , could have completed as below
//Getting IPAddress of localhost - getHostAddress return IP Address
// in textual format
System.out.println("IP address of localhost from Java Program: " + InetAddress.getLocalHost().getHostAddress());
//Hostname
System.out.println("Name of hostname : " +InetAddress.getLocalHost().getHostName());

Anonymous said...

How to know ip address of the clients connected to a server ?

javin paul said...

@Anonymous, you can print the client Socket's InetAddress as shown here. That would you your client's IP address You can also see this Java program to connect an HTTP server to learn more about client server interaction.

Anonymous said...

The above code gives an error as follows:
IPTest.java:14: error: cannot find symbol
InetAddress addr = InetAddress.getLocalHost();
^
symbol: class InetAddress
location: class IPTest
IPTest.java:14: error: cannot find symbol
InetAddress addr = InetAddress.getLocalHost();
^
symbol: variable InetAddress
location: class IPTest
2 errors


The simple solution to it is to add the following line in the start:
import java.net.InetAddress;

Post a Comment