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
Gordon Beaton - 06 Mar 2008 07:41 GMT
> 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
--
Peter Duniho - 06 Mar 2008 07:54 GMT
> Hello,
>
[quoted text clipped - 5 lines]
> 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
BigZero - 06 Mar 2008 09:34 GMT
Thanks
i got it
InetAddress group = InetAddress.getLocalHost();
System.out.println ("Local IP : " + group.getHostAddress());
Thanks
Vijay
Wayne - 06 Mar 2008 10:14 GMT
> Hello,
>
[quoted text clipped - 8 lines]
> 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>
Roedy Green - 06 Mar 2008 12:39 GMT
On Wed, 5 Mar 2008 23:30:37 -0800 (PST), BigZero
<vijaymajagaonkar@gmail.com> wrote, quoted or indirectly quoted
someone who said :
>how do i get local machine ip,
>I tried this one the following code
see http://mindprod.com/jgloss/ip.html#OWNIP
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
BigZero - 06 Mar 2008 13:32 GMT
Thanks to one all
Regards
Vijay