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