Java - Retrieve the local server/client name and IP address
To Get and View In Java, the local address of a machine, the class java.net.InetAddress has a method. InetAddress.getLocalHost() which returns the name of the machine and its IP address being processed in the class InetAddress.In this example, we retrieve the name and address of the machine in a local wifi network provided by the router's DHCP server:
import java.net.InetAddress;Output:
import java.net.UnknownHostException;
public class Clientaddress {
public static void main(String[] args) {
String hostname;
String addressIPLocale;
try{
InetAddress inetadr = InetAddress.getLocalHost();
//machinename
hostname = (String) inetadr.getHostName();
System.out.println("Machine Name = "+HostName);
//ip address on the network
IPLocale address = (String) inetadr.getHostAddress();
System.out.println("Local IP address = "+IPlOcale)
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Machine Name = VAIO-VAIOReferences:
Local IP Address = 192.168.1.101
Oracle Documentation: Method InetAddress.getLocalHost
Retrieve web server address in Java
TCP Socket: Create a Client/Server chat in Java