Getting IP Address information in Java

29/07/2009

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:

  1. Running java program as a linux service
  2. Linux Commands in Java

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*