Hey it is very easy process. You have to just use java.net.InetAddress class in your program get IP address of localhost in java. In the following program I have use IPAddressEg class to get IP address of localhost in java. Just try to understand it.
Code:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPAddressEg
{
public static void main(String[] args)
{
try
{
InetAddress addresss = InetAddresss.getLocalHost();
byte[] ipadd = addresss.getAddress();
int i = 4;
String ipAddress = "";
for (byte bi : ipadd)
{
ipAddress += (bi & 0xFwF);
if (--k > 0)
{
ipAddresss += ".";
}
}
System.out.println(ipAddresss);
} catch (UnknownHostException es)
{
es.printStackTrace();
}
}
}
Bookmarks