Skip to main content

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.

Comments

Popular posts from this blog

Find and replace with sed command in Linux

Find and replace feature is always handy. It can turn into a torture when it comes to change or delete a simple constant string in a text file. There is a handy tool in linux for doing these kind of tihngs. Actually sed is not a text editor but it is used outside of the text file to make changes.