Class HostNameUtil
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
Gets the address for the local machine.static InetAddress
Returns anInetAddress
object encapsulating what is most likely the machine's LAN IP address.static List<InetAddress>
Returns allInetAddress
objects encapsulating what are most likely the machine's LAN IP addresses.static NetworkInterface
On systems with multiple network interfaces and mixed IPv6/IPv4 get a valid network interface for binding to multicast
-
Constructor Details
-
HostNameUtil
public HostNameUtil()
-
-
Method Details
-
getLocalHostAddress
Gets the address for the local machine.- Returns:
- InetAddress.getLocalHost().getHostAddress()
- Throws:
UnknownHostException
-
getLocalHostLANAddress
Returns anInetAddress
object encapsulating what is most likely the machine's LAN IP address.This method is intended for use as a replacement of JDK method
InetAddress.getLocalHost
, because that method is ambiguous on Linux systems. Linux systems enumerate the loopback network interface the same way as regular LAN network interfaces, but the JDKInetAddress.getLocalHost
method does not specify the algorithm used to select the address returned under such circumstances, and will often return the loopback address, which is not valid for network communication. Details here.This method will scan all IP addresses on all network interfaces on the host machine to determine the IP address most likely to be the machine's LAN address. If the machine has multiple IP addresses, this method will prefer a site-local IP address (e.g. 192.168.x.x or 10.10.x.x, usually IPv4) if the machine has one (and will return the first site-local address if the machine has more than one), but if the machine does not hold a site-local address, this method will return simply the first non-loopback address found (IPv4 or IPv6).
If this method cannot find a non-loopback address using this selection algorithm, it will fall back to calling and returning the result of JDK method
InetAddress.getLocalHost
.- Returns:
- InetAddress
- Throws:
UnknownHostException
- If the LAN address of the machine cannot be found.- Since:
- 3.1
-
getLocalHostLANAddresses
Returns allInetAddress
objects encapsulating what are most likely the machine's LAN IP addresses.This method will scan all IP addresses on all network interfaces on the host machine to determine the IP addresses most likely to be the machine's LAN addresses.
- Returns:
- List
- Throws:
UnknownHostException
- If the LAN address of the machine cannot be found.
-
getMulticastNetworkInterface
On systems with multiple network interfaces and mixed IPv6/IPv4 get a valid network interface for binding to multicast- Returns:
- a network interface suitable for multicast
- Throws:
SocketException
- if a problem occurs while reading the network interfaces
-