java.net.NetworkInterface

Erik Poupaert erik.poupaert@chello.be
Sat Jun 21 01:19:00 GMT 2003


I finally found a way to figure out network ip address for localhost:

	protected static String getHostIpAddress()
	{
		try
		{
			Enumeration networkInterfaces=NetworkInterface.getNetworkInterfaces();
			for (Enumeration e = NetworkInterface.getNetworkInterfaces() ;
				e.hasMoreElements() ;)
			{
				NetworkInterface netIf=(NetworkInterface)e.nextElement();
				for(Enumeration e2=netIf.getInetAddresses(); e2.hasMoreElements();)
				{
					InetAddress address=(InetAddress)e2.nextElement();
					String ip=address.getHostAddress();
					//grab the first non-loopback address
					if(!ip.equals("127.0.0.1")) return ip;
				}
			}
			throw new RuntimeException("error resolving localhost ip address");
		}
		catch(Exception e)
		{
			throw new RuntimeException(e.getMessage());
		}
	}

It works fine on linux: gcc version 3.3 release.

On Windows, however (gcc version 3.3 20030409 (prerelease),
NetworkInterface.getNetworkInterfaces() indiscriminately throws a
java.net.SocketException.

Does anybody know if this problem has been solved after 20030409 in Windows? Could I
avoid this by downloading Mohan's latest build?


-- 



More information about the Java mailing list