The Javadoc for the appropriate class is Class InetAddress
The class java.net.InetAddress
has two subclasses
java.net.Inet4Address
java.net.Inet6Address
InetAddress
can be contructed from the bytes
of the address: 4 bytes for an IPv4 address, and 16 bytes for
an IPv6 address. The method getByName()
can take
a string representation ("127.0.0.1" or "::1" for example)
and create an InetAddress
.
You can tell which type of InetAddress
you have
using instanceof
.
There are methods in each class to tell what type of address you have.
For IPv4, these are isLinkLocalAddress()
,
isMulticastAddress()
and others.
For IPv6 these are isLinkLocalAddress()
,
isMulticastAddress()
and others. There is no
isUniqueLocal()
but there is
IsSiteLocalAddress()
which shouldn't really be there.
Java has the class InetAddress
in package
java.net
. This class has the static method
getByName()
which will create an
InetAddress
object from a host name.
From there, other methods will return the host name and host
address. A sample program is
GetInetInfo.java:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetInetInfo{
public static void main(String[] args){
if (args.length != 1) {
System.err.println("Usage: GetInetInfo address");
// System.exit(1);
return;
}
InetAddress address = null;
try {
address = InetAddress.getByName(args[0]);
} catch(UnknownHostException e) {
e.printStackTrace();
// System.exit(2);
return;
}
System.out.println("Host name: " + address.getHostName());
System.out.println("Host address: " + address.getHostAddress());
// System.exit(0);
return;
}
} // GetInetInfo
This can be run by e.g. java jan.newmarch.name
to give
Host name: jan.newmarch.name
Host address: 103.79.105.27
Note that it is only giving one address, with my setup (and probably yours)
an IPv4 address. if you want to get all addresses registered with DNS,
you need to use the method getAllByName()
as in
GetAllInetInfo.java:
import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.UnknownHostException;
public class GetAllInetInfo{
public static void main(String[] args){
if (args.length != 1) {
System.err.println("Usage: GetInetInfo address");
// System.exit(1);
return;
}
InetAddress[] addresses = null;
try {
addresses = InetAddress.getAllByName(args[0]);
} catch(UnknownHostException e) {
e.printStackTrace();
// System.exit(2);
return;
}
if (addresses.length > 0)
System.out.println("Host name: " + addresses[0].getHostName());
for (int n = 0; n < addresses.length; n++) {
InetAddress address = addresses[n];
if (address instanceof Inet4Address)
System.out.print("IPv4 address is ");
else
System.out.print("Ipv6 address is ");
System.out.println(address.getHostAddress());
}
// System.exit(0);
return;
}
} // GetAllInetInfo
Then a typical run gives
$java GetAllInetInfo jan.newmarch.name
Host name: jan.newmarch.name
IPv4 address is 103.79.105.27
Ipv6 address is 2400:3740:200:d900:0:0:0:260
An interface (such as eth0) may have multiple IPv6 addresses. There will usually be a link local address, probably a site local address and there could be many global addresses. A program to list them all is ListNets.java:
// From https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class ListNets {
public static void main(String args[]) throws SocketException {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
out.printf("Display name: %s\n", netint.getDisplayName());
out.printf("Name: %s\n", netint.getName());
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s\n", inetAddress);
}
out.printf("\n");
}
}
Copyright © Jan Newmarch, jan@newmarch.name
" Network Programming using Java, Go, Python, Rust, JavaScript and Julia"
by
Jan Newmarch
is licensed under a
Creative Commons Attribution-ShareAlike 4.0 International License
.
Based on a work at
https://jan.newmarch.name/NetworkProgramming/
.