
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find IP Address of the Client in Java
To find the IP Address of the client, the Java code is as follows −
Example
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.
Advertisements