Java program to find IP address of your computer

09 Nov, 2021
An IP(Internet Protocol) address is an identifier assigned to each computer and another device (e.g., router, mobile, etc) connected to a TCP/IP network that is used to locate and identify the node in communication with other nodes on the network. IP addresses are usually written and displayed in human-readable notation such as 192.168.1.35 in IPv4(32-bit IP address).
An IP address serves two principal functions: host or network interface identification and local addressing. Its role has been characterized as follows: “A name indicates what we seek. An address indicates where it is. A route indicates how to get there.”
Prerequisites : Networking in Java | Set 1 (InetAddress class), trim() in Java.InetAddress.getLocalHost() is used to find the private IP addresses used in LAN or any other local network.
To find public IP, we use http://bot.whatismyipaddress.com (An online utility to find your public IP), we open the URL, read a line and print the line.
Below is the Java implementation of the above steps. 
Java
// Java program to find IP address of your computer// java.net.InetAddress class provides method to get// IP of any host nameimport java.net.*;import java.io.*;import java.util.*;import java.net.InetAddress; public class JavaProgram{    public static void main(String args[]) throws Exception    {        // Returns the instance of InetAddress containing        // local host name and address        InetAddress localhost = InetAddress.getLocalHost();        System.out.println("System IP Address : " +                      (localhost.getHostAddress()).trim());         // Find public IP address        String systemipaddress = "";        try        {            URL url_name = new URL("http://bot.whatismyipaddress.com");             BufferedReader sc =            new BufferedReader(new InputStreamReader(url_name.openStream()));             // reads system IPAddress            systemipaddress = sc.readLine().trim();        }        catch (Exception e)        {            systemipaddress = "Cannot Execute Properly";        }        System.out.println("Public IP Address: " + systemipaddress +"\n");    }}
Output: 
 System IP Address : 10.0.8.204
 Public IP Address : 35.166.48.97
Note: The above output is for a machine that is used by GeeksforGeeks online compiler, ide.geeksforgeeks.org
This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
marcosarcticseal
Computer Networks
Java
Java
Computer Networks
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Differences between TCP and UDP
Types of Network Topology
RSA Algorithm in Cryptography
TCP Server-Client implementation in C
Socket Programming in Python
Arrays in Java
Arrays.sort() in Java with examples
Split() String method in Java with examples
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java