怎么获取linux上多块网卡的IP地址?

04-05-19 snoopy
怎么获取linux上多块网卡的IP地址:

我的程序如下:

import java.net.*;

public class NetUtil {

private static InetAddress[] ias=null;

public static InetAddress[] getAllLocalInetAddress(){

if(ias!=null) return ias;

try{

String cupName = InetAddress.getLocalHost().getHostName();

ias = InetAddress.getAllByName(cupName);

}catch( UnknownHostException e){}

return ias;

}

}

在windows上没有问题, 在linux下面返回是127.0.0.1

pls help me

mlcy
2004-05-20 01:13
哦,怎么没人答

廉价劳力
2004-05-20 05:08
see workaround (need to register in sun developer)

http://developer.java.sun.com/developer/bugParade/bugs/4665037.html

import java.net.*;
import java.util.*;

public class LinuxInetAddress {
public static void main(String[] args) throws Exception {
   System.out.println(getLocalHost());
}
	
	/**
	 * Returns an InetAddress representing the address 
of the localhost.  
	 * Every attempt is made to find an address for this 
host that is not 
	 * the loopback address.  If no other address can 
be found, the 
	 * loopback will be returned.
	 * 
	 * @return InetAddress - the address of localhost
	 * @throws UnknownHostException - if there is a 
problem determing the address
	 */
	public static InetAddress getLocalHost() throws 
UnknownHostException {
		InetAddress localHost = 
InetAddress.getLocalHost();
		if(!localHost.isLoopbackAddress()) return 
localHost;
		InetAddress[] addrs = 
getAllLocalUsingNetworkInterface();
		for(int i=0; i<addrs.length; i++) {
			if(!addrs[i].isLoopbackAddress()) 
return addrs[i];
		}
		return localHost;	
	}
	
	/**
	 * This method attempts to find all InetAddresses 
for this machine in a 
	 * conventional way (via InetAddress).  If only one 
address is found 
	 * and it is the loopback, an attempt is made to 
determine the addresses 
	 * for this machine using NetworkInterface.
	 * 
	 * @return InetAddress[] - all addresses assigned to 
the local machine
	 * @throws UnknownHostException - if there is a 
problem determining addresses
	 */
	public static InetAddress[] getAllLocal() throws 
UnknownHostException {
		InetAddress[] iAddresses = 
InetAddress.getAllByName("127.0.0.1");
		if(iAddresses.length != 1) return 
iAddresses;
		if(!iAddresses[0].isLoopbackAddress()) 
return iAddresses;
		return getAllLocalUsingNetworkInterface();
	
	}
	
	/**
	 * Utility method that delegates to the methods of 
NetworkInterface to 
	 * determine addresses for this machine.
	 * 
	 * @return InetAddress[] - all addresses found from 
the NetworkInterfaces
	 * @throws UnknownHostException - if there is a 
problem determining addresses
	 */
	private static InetAddress[] 
getAllLocalUsingNetworkInterface() throws 
UnknownHostException {
		ArrayList addresses = new ArrayList();
		Enumeration e = null;
		try {
			e = 
NetworkInterface.getNetworkInterfaces();
		} catch (SocketException ex) {
			throw new UnknownHostException
("127.0.0.1");
		}
		while(e.hasMoreElements()) {
			NetworkInterface ni = 
(NetworkInterface)e.nextElement();
			for(Enumeration e2 = 
ni.getInetAddresses(); e2.hasMoreElements();) {
				addresses.add
(e2.nextElement());
			}	
		}
		InetAddress[] iAddresses = new 
InetAddress[addresses.size()];
		for(int i=0; i<iAddresses.length; i++) {
			iAddresses[i] = (InetAddress)
addresses.get(i);
		}
		return iAddresses;
	}
}

<p>

snoopy
2004-05-24 17:51
谢谢楼上的, 我知道了

猜你喜欢