Reputation: 97
First of all my programming language is Java. I created a simple chat program using sockets. It works pretty good.
I tried it on my computer (localhost) between two terminal instances.
I want to try it out on my laptop, or on another computer. For this I need the client's internal IP address.
How to figure out client's internal IP address using Java?
I specially want to get it out with Java, not using CMD, or something like this. I mean - that's not just a constant string.
Upvotes: 1
Views: 4250
Reputation: 156
In Java, there is a class InetAddress that represents IP Adress (and its corresponding host name, in some cases).
For example, let's get my IP address and my host name:
import java.net.InetAddress;
public class Main {
public static void main(String[] args) {
try {
InetAddress i = InetAddress.getLocalHost();
System.out.println(i); // host name and IP address
System.out.println(i.getHostName()); // name
System.out.println(i.getHostAddress()); // IP address only
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output (in my case):
LLEITE/192.168.1.100
LLEITE
192.168.1.100
Upvotes: 5
Reputation: 427
Use the below code:
InetAddress ownIP=InetAddress.getLocalHost();
System.out.println("IP of my system is := "+ownIP.getHostAddress());`
Upvotes: 0