There's a pretty 'dirty' way to do this assuming you have a computer that is ALWAYS on the network. Let's pretend this computer is named 'bob' on the network. You could have 'bob' be running this server:
import java.io.*;
import java.net.*;
public class Bob {
public Bob() throws IOException {
ServerSocket server = new ServerSocket(6000);
Socket client;
PrintStream out;
while (true) {
client = server.accept();
out = new PrintStream(client.getOutputStream());
out.print(client.getInetAddress().getHostAddress());
out.close();
client.close();
}
}
public static void main(String args[]) {
try {
new Bob();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
And then on your side, you could use the getIP() method demonstrated in this program:
import java.io.*;
import java.net.*;
public class Example {
public Example() {
String address = "N/A";
try {
address = getIP();
}
catch (IOException e) {
address = "N/A";
e.printStackTrace();
}
System.out.println("Address: "+address);
}
protected String getIP() throws IOException {
Socket s = new Socket("bob",6000);
BufferedReader b = new BufferedReader(new InputStreamReader(s.getInputStream()));
return b.readLine();
}
public static void main(String args[]) {
new Example();
}
}
This will work ONLY if 'bob' is a DIFFERENT computer than the one thats running the getIP(). If it's run on the same machine, getIP() will obviously return 127.0.01.