Java获取本地连接远程时使用的地址

[codesyntax lang="java" lines="normal"]
private String getBridgeAddress()
{
	String osgiServer = "surenpi.com";

	try
	{
		Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
		while(interfaces.hasMoreElements())
		{
			NetworkInterface network = interfaces.nextElement();
			if(network.isUp() && !network.isLoopback() && !network.isVirtual())
			{
				if(InetAddress.getByName(osgiServer).isReachable(network, 0, 2000))
				{
					Enumeration<InetAddress> inetAddrs = network.getInetAddresses();
					while(inetAddrs.hasMoreElements())
					{
						InetAddress addr = inetAddrs.nextElement();
						if(addr instanceof Inet4Address)
						{
							String hostAddr = addr.getHostAddress();

							return hostAddr;
						}
					}
				}
			}
		}
	}
	catch (SocketException e)
	{
		e.printStackTrace();
	}
	catch (UnknownHostException e)
	{
		e.printStackTrace();
	}
	catch (IOException e)
	{
		e.printStackTrace();
	}

	return null;
}
[/codesyntax]

你可能感兴趣的:(java,ping)