How can i get the LAN IP-address of a computer using Java? I want the IP-address which is connected to the router and the rest of the network.
I\'ve tried something like
Try java.net.NetworkInterface
import java.net.NetworkInterface;
...
for (
final Enumeration< NetworkInterface > interfaces =
NetworkInterface.getNetworkInterfaces( );
interfaces.hasMoreElements( );
)
{
final NetworkInterface cur = interfaces.nextElement( );
if ( cur.isLoopback( ) )
{
continue;
}
System.out.println( "interface " + cur.getName( ) );
for ( final InterfaceAddress addr : cur.getInterfaceAddresses( ) )
{
final InetAddress inet_addr = addr.getAddress( );
if ( !( inet_addr instanceof Inet4Address ) )
{
continue;
}
System.out.println(
" address: " + inet_addr.getHostAddress( ) +
"/" + addr.getNetworkPrefixLength( )
);
System.out.println(
" broadcast address: " +
addr.getBroadcast( ).getHostAddress( )
);
}
}