Monday 28 January 2013

Get own ip address and subnet mask in java


import java.net.*;


InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
String ipAddress = localHost.getHostAddress();
String subnetMask = "/"+networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();
System.out.println(ipAddress + subnetMask);


--------
output like :
192.168.65.161/23

No comments:

Post a Comment