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.
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.
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
/**
* 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
*
* @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
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
That’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
also, if your computer have more then one IP you can find all of them with these lines of code
ReplyDeleteInetAddress localhost = InetAddress.getLocalHost();
List adresses = InetAddress.getAllByName(localhost.getCanonicalHostName())
Nice example , could have completed as below
ReplyDelete//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());
How to know ip address of the clients connected to a server ?
ReplyDelete@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.
ReplyDeleteThe above code gives an error as follows:
ReplyDeleteIPTest.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;