Java Plan To Detect Ip Address Of Localhost - Representative Tutorial
How to detect IP address of local host from Java program
Java networking API provides method to detect IP address of localhost from Java plan past times using java.net. InetAddress class. It’s rare when yous need IP address for localhost inwards Java program. Mostly I used Unix ascendance to detect IP address of localhost. For all practical role where plan doesn’t need IP address exactly yous need to troubleshoot whatever networking issues, Use DOS or windows ascendance or Use Linux commands. Recently 1 of my friend faced this interrogation inwards a core Java interview, where they are expecting Java developer amongst unopen to socket programming experience, But until yous know or yous bring done it earlier its difficult to respond this fact based question, which motivates me to write this post. In this Java tutorial nosotros volition meet How to detect IP address of localhost from Java program. By the means it’s besides skilful to yell upwards list of Unix networking commands to troubleshoot whatever networking issues related to Java application inwards Unix environment.
IP Address of localhost from Java program
As I said InetAddress from java.net bundle is used to stand upwards for an IP address inwards Java. an IP address is a 32 or 128 chip unsigned number used past times IP protocol which is backbone of many pop protocols similar TCP too UDP. There are ii kinds of IP address IPv4 too IPv6 too IP address is associated amongst host which tin move detect past times host refer resolution process. Hostname resolution is performed past times combining local machine configuration too network naming services such every bit the DNS(Domain refer system) too NIS(Network Information Service). InetAddress has method to resolve hostname too IP address too vice versa. Here is a consummate code example of finding IP address from Java program.
import java.net.UnknownHostException;
/**
* Simple Java plan to detect IP Address of localhost. This plan uses
/**
* Simple Java plan to detect IP Address of localhost. This plan uses
* InetAddress from java.net bundle to detect 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 render 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 render IP Address
// inwards 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 detect IP address of localhost from Java. Its dainty tip to know exactly every bit I said java.net is non a mutual bundle similar java.lang or java.util. Best means to larn too yell upwards networking concepts inwards Java is to write unopen to customer server plan which uses these essential classes.
Further Learning
Complete Java Masterclass
How to convert String to Integer inwards Java
0 Response to "Java Plan To Detect Ip Address Of Localhost - Representative Tutorial"
Post a Comment