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 / Tools / March 2004

Tip: Looking for answers? Try searching our database.

How to get my both IP adresses ??

Thread view: 
Yohan - 09 Mar 2004 15:54 GMT
Hi,
I've got a PC with two Network cards. For each card, I have an IP
adress. My both adresses are differents. In fact, I want to know each
adress.
I tried this: java.net.InetAddress.getLocalHost() , but it returns me
only one adress.
Does somebody have an idea ?
Thanks,
Yohan
Michael Amling - 09 Mar 2004 16:09 GMT
> Hi,
> I've got a PC with two Network cards. For each card, I have an IP
[quoted text clipped - 3 lines]
> only one adress.
> Does somebody have an idea ?

  I think your best bet is InetAddress.getAllByName(), since it returns
an array. However, I don't know what parameter you'd have to pass it.

--Mike Amling
Michael Amling - 09 Mar 2004 16:34 GMT
>> Hi,
>> I've got a PC with two Network cards. For each card, I have an IP
[quoted text clipped - 6 lines]
>   I think your best bet is InetAddress.getAllByName(), since it returns
> an array. However, I don't know what parameter you'd have to pass it.

  On Unix at least, try calling it with the string put out by the
hostname command.

--Mike Amling
Farid - 09 Mar 2004 22:12 GMT
I am doing that in a class via different methodes which loops on themselves
using the methods:

InetAddress[] ips         = InetAddress.getAllByName(hostname);
then
loop on ip = ips[idx].getHostAddress();
etc...

I then also added this interesting method which will try first to use some
nice methods introduced in the 1.4 jdk via introspection, should this jdk be
used...

/**
 * Try to find all the network interfaces on the computer, and register all
the hostnames and
 * IPs associated with them.<BR>Note: This is done by loading a specific
class from the jdk
 * 1.4 if this jdk is present.
 *
 * @return DOCUMENT ME!
 */
private static boolean startSearchForInterfaces()
{

 try
 {

  Class  classDefinition = Class.forName("java.net.NetworkInterface");
  Method getIfcs = classDefinition.getMethod("getNetworkInterfaces", null);
  Method getIAdd = classDefinition.getMethod("getInetAddresses", null);

  for (Enumeration eNetInt = (Enumeration)getIfcs.invoke(null, null);
    eNetInt.hasMoreElements();)
  {

   Object netInt = eNetInt.nextElement();

   for (Enumeration eInetAdd = (Enumeration)getIAdd.invoke(netInt, null);
     eInetAdd.hasMoreElements();)
   {

    InetAddress inetAdd = (InetAddress)eInetAdd.nextElement();

    if (inetAdd != null)
    {
     //here to whateve you want with the inetAdd object:
     //inetAdd.getHostName()    ==> hostname
     //inetAdd.getHostAddress() ==> IP
    }
   }
  }

 }
 catch (Exception e)
 {

  return false;
 }
}

Have fun...
Farid.
> Hi,
> I've got a PC with two Network cards. For each card, I have an IP
[quoted text clipped - 5 lines]
> Thanks,
> Yohan


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.