Re: Get ip of localmachine
BigZero wrote:
Hello,
how do i get local machine ip,
I tried this one the following code
InetAddress group = InetAddress.getByName("localhost");
System.out.println("\nip"+add.getHostAddress().toString());
it returns loopback ip ie 127.0.0.1
so plz any one can help me.....!
Thanks
Vijay
<sscce>
// "Quick and Dirty" Demo to show how to list a host's IP address(es).
// Written 3/2008 by Wayne
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class ShowIPAddresses
{
public static void main ( String [] args ) throws Exception {
InetAddress addr = InetAddress.getLocalHost();
out.println( "My main IP is: " + addr.getHostAddress() + "\n" );
out.println( "----------------------------" );
Enumeration<NetworkInterface> nics =
NetworkInterface.getNetworkInterfaces();
while ( nics.hasMoreElements() ) {
NetworkInterface nic = nics.nextElement();
out.println( "IP addresses for NIC \"" + nic.getName() + "\" ("
+ nic.getDisplayName() + ")");
for ( Enumeration<InetAddress> addrs = nic.getInetAddresses();
addrs.hasMoreElements(); )
out.println( "\t" + addrs.nextElement().getHostAddress() );
}
}
}
</sscce>