Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

Get ip of localmachine

Thread view: 
BigZero - 06 Mar 2008 07:30 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.