Get ip of localmachine

B

BigZero

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
 
G

Gordon Beaton

how do i get local machine ip,

Typically there are multiple IP addresses (often 2, sometimes more).
Which one do you want?

Iterate over the available java.net.NetworkInterfaces, ask each of
them for its addresses, then choose one you like.

/gordon

--
 
P

Peter Duniho

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.....!

Try InetAddress.getLocalHost()

Yes, it's a little confusing. :)

Pete
 
B

BigZero

Thanks

i got it
InetAddress group = InetAddress.getLocalHost();
System.out.println ("Local IP : " + group.getHostAddress());

Thanks

Vijay
 
W

Wayne

BigZero said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top