An Internet Protocol address (IP address) is a
numerical label assigned to each device (e.g., computer, printer)
participating in a computer network that uses the Internet Protocol for
communication. The designers of the Internet Protocol defined an IPv4
address as a 32-bit number.
In this tutorial we are going to see
how can you get the IP Address that is assigned to your own machine
inside your local network and the IP Addresses assigned to specific
Domain Names(e.g. www.google.com…).
To do that we are going to use
InetAddress
.To be more specific we are going to use:getLocalHost().getHostAddress()
method ofInetAddress
to get the IP Address of our machine in our local networkgetByName()
method ofInetAddress
to get the IP Address of a specific Domain NamegetAllByName()
method ofInetAddress
to get all the IP Address of a specific Domain Name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.miscl2; | |
import java.net.InetAddress; | |
import java.net.UnknownHostException; | |
public class GetIpAddress { | |
public static void main(String[] args) throws UnknownHostException { | |
// print the IP Address of your machine (inside your local network) | |
System.out.println(InetAddress.getLocalHost().getHostAddress()); | |
// print the IP Address of a web site | |
System.out.println(InetAddress.getByName("www.jaypthakkar.blogspot.in")); | |
// print all the IP Addresses that are assigned to a certain domain | |
InetAddress[] inetAddresses = InetAddress | |
.getAllByName("www.google.com"); | |
for (InetAddress ipAddress : inetAddresses) { | |
System.out.println(ipAddress); | |
} | |
} | |
} | |
/* | |
OUTPUT | |
---------------------------------------- | |
192.168.1.89 | |
www.jaypthakkar.blogspot.in/74.125.236.108 | |
www.google.com/74.125.236.114 | |
www.google.com/74.125.236.116 | |
www.google.com/74.125.236.112 | |
www.google.com/74.125.236.113 | |
www.google.com/74.125.236.115 | |
*/ |