Determining the External IP Address in Java
In this inquiry, the objective is to retrieve the external IP address of a machine as it would appear to a computer outside the network. The provided IPAddress class only retrieves the local IP address, prompting the need for an alternative solution.
Solution Through External Services
Unfortunately, obtaining the external IP address from code running on the local machine may not be possible. However, we can utilize code running on a website and employ methods that reveal the IP address from which the request originates:
Example Using AWS Service
import java.net.*; import java.io.*; URL whatismyip = new URL("http://checkip.amazonaws.com"); BufferedReader in = new BufferedReader(new InputStreamReader( whatismyip.openStream())); String ip = in.readLine(); // Extract the IP address as a string System.out.println(ip);
The above is the detailed content of How Can I Get My Machine\'s External IP Address in Java?. For more information, please follow other related articles on the PHP Chinese website!