Java IP Viewer: Getting Local and Remote IP Addresses Every device on a network relies on an Internet Protocol (IP) address to communicate. When building network-based applications in Java, you often need to identify either the machine running the code (local IP) or the client connecting to your server (remote IP).
This guide explains how to retrieve both local and remote IP addresses using Java’s built-in networking capabilities. Understanding Local vs. Remote IP Addresses
Before writing the code, it is important to understand what these two addresses represent:
Local IP Address: The address assigned to your machine within your local network (LAN), usually assigned by a Wi-Fi router (e.g., 192.168.1.5).
Remote IP Address: The public address of a client connecting to your application from outside your local network, or the address of an external server you are connecting to. 1. How to Get the Local IP Address in Java
Java provides the java.net.InetAddress class to handle network addresses. The simplest way to find your local machine’s IP address is by using InetAddress.getLocalHost(). Basic Local IP Implementation
import java.net.InetAddress; import java.net.UnknownHostException; public class LocalIPViewer { public static void main(String[] args) { try { InetAddress localHost = InetAddress.getLocalHost(); System.out.println(“Host Name: ” + localHost.getHostName()); System.out.println(“Local IP Address: ” + localHost.getHostAddress()); } catch (UnknownHostException e) { System.err.println(“Could not find the local IP address: ” + e.getMessage()); } } } Use code with caution. Handling Multiple Network Interfaces
The basic method above can sometimes return the loopback address (127.0.0.1) if your system’s host file is configured a certain way. If your machine has multiple network cards (like Wi-Fi, Ethernet, and virtual VPN adapters), you should safely iterate through them using NetworkInterface.
import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Collections; import java.util.Enumeration; public class AdvancedLocalIPViewer { public static void main(String[] args) { try { Enumeration Use code with caution. 2. How to Get the Remote IP Address in Java
To find a remote IP address, your Java application must either be acting as a server receiving client connections, or interacting with an external web service.
Scenario A: Getting a Remote Client’s IP in a Web Application
If you are building a web server or a servlet using the Jakarta/Java EE ecosystem, you can extract the remote IP address directly from the incoming HTTP request.
import jakarta.servlet.http.HttpServletRequest; public class RemoteWebIPViewer { public String getClientIP(HttpServletRequest request) { // Check if the request went through a proxy or load balancer String xForwardedFor = request.getHeader(“X-Forwarded-For”); if (xForwardedFor != null && !xForwardedFor.isEmpty()) { // If multiple proxies exist, the first IP is the original client return xForwardedFor.split(“,”)[0].trim(); } // Fallback to direct connection IP return request.getRemoteAddr(); } } Use code with caution. Scenario B: Getting Your Own Public/Remote IP Address
If your Java app runs on a local machine and you want to know its public IP address as seen by the rest of the internet, internal APIs will not work. You must query an external service.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class PublicIPViewer { public static void main(String[] args) { try { // Using a free, public IP checker service URL url = new URL(”https://amazonaws.com”); try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) { String publicIP = br.readLine().trim(); System.out.println(“Your Public/Remote IP Address is: ” + publicIP); } } catch (Exception e) { System.err.println(“Error fetching public IP: ” + e.getMessage()); } } } Use code with caution. Summary Cheat Sheet Core Class/Method Simple Local IP InetAddress.getLocalHost() Quick, but might return loopback (127.0.0.1). Accurate Local IP NetworkInterface.getNetworkInterfaces() Best for machines with multiple network cards. Web Client IP request.getRemoteAddr() Always verify X-Forwarded-For header for proxies. Your Public IP External API HTTP Request Necessary to find your address outside the router firewall.
If you want to refine this code for your project, let me know: Are you building a desktop app or a web server? Do you need to support IPv4, IPv6, or both?
Is your application deployed behind a proxy or load balancer?
I can provide the exact code block tailored to your architectural setup.
Leave a Reply