Java program to find IP Address of the client


To find the IP Address of the client, the Java code is as follows −

Example

 Live Demo

import java.net.*;
import java.io.*;
import java.util.*;
import java.net.InetAddress;
public class Demo{
   public static void main(String args[]) throws Exception{
      InetAddress my_localhost = InetAddress.getLocalHost();
      System.out.println("The IP Address of client is : " + (my_localhost.getHostAddress()).trim());
      String my_system_address = "";
      try{
         URL my_url = new URL("http://bot.whatismyipaddress.com");
         BufferedReader my_br = new BufferedReader(new
         InputStreamReader(my_url.openStream()));
         my_system_address = my_br.readLine().trim();
      }
      catch (Exception e){
         my_system_address = "Cannot Execute Properly";
      }
   }
}

Output

The IP Address of client is : 127.0.0.1

A class named Demo contains the main function. In this main function, an instance of the InetAddress class is created and the ‘getHostAddress’ function is used to get the IP address of the client. This is displayed on the console.

Updated on: 08-Jul-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements