In this Example we want to describe you a code that helps you in understanding a code how to 'Get Local Host Name'. For this we have a class name GetLocalHostName. Inside the main method we have -
For this we have a class name GetLocalHost,Inside the main method we have a class NetworkInterface,this class represents a Network Interface made up of a name and a list of IP addresses assigned to this interface. This is used to identify the list of multicast group joined to identify the local interface.
1)getNetworkInterface( ) - This method returns you the interface used on this machine.
The System.outprintln print the interface name by calling the get Display Name ( ) method.
2)getDisplayName ( ) - This method returns you name of the network interface.
3)getInet Addresses( ) - This method returns you Enumeration with all the subset of the Inet addresses bound to this network interface.
4)get Host Name( ) - This method returns you the information about the hostname for the current machine.
5)get Host Address( ) -This method return you the address of the host.
The inet Address class provides you the IP address of the current system.
On the execution of code, the code display the host name of the current system.
GetLocalHostName.java|
import java.net.*; import java.util.*; public class GetLocalHostName { public static void main(String args[]) throws Exception { Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); 0 for (NetworkInterface netint : Collections.list(nets)) { System.out.println("\nInterface Name : " + netint.getDisplayName()); Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { System.out.println("Local host Name\t: " + inetAddress.getHostName()); System.out.println("Host Address\t: " + inetAddress.getHostAddress()); } } } } |
|
Interface name : MS TCP Loopback interface |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Get Local Host Name View All Comments
Post your Comment