Getting IP Address information in Java
Java.net class must be used in order to learn ip address of the computer. Simply cast all information of the network interfaces to an enumeration, then pass the enumeration through a collection.
import java.net.*;
Enumeration<NetworkInterface> netints = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(netints))
{
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddr : Collections.list(inetAddresses))
{
if (inetAddr.isSiteLocalAddress())
{
String Addr = inetAddr.toString();
String ServerIp = Addr.substring(1);
}
}
}
isSiteLocalAddress() routine is to check if the InetAddress is a site local address.
Related posts: