xxxxxxxxxx
import java.net.DatagramSocket;
import java.net.InetAddress;
try(final DatagramSocket socket = new DatagramSocket()){
socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
ip = socket.getLocalAddress().getHostAddress();
}
xxxxxxxxxx
import java.awt.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
class Main
{
public static void main(String args[]) throws IOException
{
InetAddress ip;
String hostname;
ip = InetAddress.getLocalHost();
hostname = ip.getHostName();
System.out.println("Current IP address : " + ip + "\n" +
"current Hostname : " + hostname);
}
}