import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Collections; import java.util.Enumeration; /** * Program to display current computer name, IP Address and network interfaces * * @author sahir maredia (Kotia Solutions) * */ public class ComputerName { /** * @param args */ public static void main(String[] args) { try { InetAddress ipaddress = InetAddress.getLocalHost(); System.out.println("Computer Host Name : " + ipaddress.getHostName()); System.out.println("IP Address of Localhost : " + ipaddress.getHostAddress()); // getting list of network interfaces Enumeration<NetworkInterface> interfaces = NetworkInterface .getNetworkInterfaces(); System.out.println(); System.out .println("================ Following are the available network interfaces ==============="); System.out.println(); if (interfaces == null) { System.out.println("No network interfaces found"); } else { for (NetworkInterface netIf : Collections.list(interfaces)) { System.out.println("Display Name : " + netIf.getDisplayName()); System.out.println("Name : " + netIf.getName()); System.out.println(); } } } catch (UnknownHostException ex) { System.out.println("Error Occured : " + ex.getMessage()); } catch (SocketException e) { System.out.println("Error Occured : " + e.getMessage()); } } }
Run the program as follows :
sahir@sahir-laptop:~/temp/networklab$ javac ComputerName.java
sahir@sahir-laptop:~/temp/networklab$ java ComputerName
Computer Host Name : sahir-laptop
IP Address of Localhost : 127.0.1.1
================ Following are the available network interfaces ===============
Display Name : wlan0
Name : wlan0
Display Name : lo
Name : lo
sahir@sahir-laptop:~/temp/networklab$
sahir@sahir-laptop:~/temp/networklab$ java ComputerName
Computer Host Name : sahir-laptop
IP Address of Localhost : 127.0.1.1
================ Following are the available network interfaces ===============
Display Name : wlan0
Name : wlan0
Display Name : lo
Name : lo
sahir@sahir-laptop:~/temp/networklab$
Sahir
ReplyDeleteNeed ur help .....i want to get the ip address list of a lan using java code ..plzz help me out its urgent and am stucked into it