An IP(Internet Protocol) address is an identifier assigned to each computer and other 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 principle functions : host or network interface identification and local addressing. It’s role has been characterized as follows : “A name indicate what we seek. An address indicate where it is. A route indicate how to get there.”
InetAddress.getLocalHost() is used to find private IP address 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 Java implementation of above steps.
System IP Address : 10.0.8.204 Public IP Address : 22.214.171.124
Note : The above output is for 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 contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Difference Between Network Address Translation (NAT) and Port Address Translation (PAT)
- Difference between MAC Address and IP Address
- Introduction of MAC Address in Computer Network
- C Program to find IP Address, Subnet Mask & Default Gateway
- Java Program to Determine Hostname from IP Address
- Program to determine class, Network and Host ID of an IPv4 address
- C Program to display hostname and IP address
- Alternate method to find DBA from given IP address
- Pinging an IP address in Java | Set 1
- Pinging an IP address in Java | Set 2 (By creating sub-process)
- Check if email address valid or not in Java
- Finding IP address of a URL in Java
- How to validate an IP address using Regular Expressions in Java
- Java | How to create your own Helper Class?
- Learn Java on Your Own in 20 Days - Free!
- How DHCP server dynamically assigns IP address to a host?
- Types of Network Address Translation (NAT)
- How Address Resolution Protocol (ARP) works?
- Python | Remove leading zeros from an IP address
- Extracting MAC address using Python