I'm writing a java program which needs to get the computer's IP address(not the Internet IP, just the local one). The program worked, but when I connected to my VPN server, I noticed that the IP address I was getting was invalid. This is the code I'm using:
public static String getIP() {
try (final DatagramSocket socket = new DatagramSocket()) {
socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
return socket.getLocalAddress().getHostAddress();
} catch (IOException ex) {
ErrorHandler.handle(ex);
return "";
}
}