Getting the local IP address

Hello everybody!

Currently, I'm developing a program that needs to know the primary IP address the local machine uses to communicate over the net, but I have no clue how to get this IP. I thought about opening a ServerSocket and looking what IP it has, but this seems a bit exagerrated for me...

Do you have other ideas?

Thx in advance, orkano

[369 byte] By [orkanoa] at [2007-11-27 11:25:42]
# 1

Check the java.net.InetAddress methods.

baftosa at 2007-7-29 16:06:00 > top of Java-index,Java Essentials,Java Programming...
# 2

thx thats what i was looking for

orkanoa at 2007-7-29 16:06:00 > top of Java-index,Java Essentials,Java Programming...
# 3

try {

InetAddress addr = InetAddress.getLocalHost();

// Get IP Address

byte[] ipAddr = addr.getAddress();

// Get hostname

String hostname = addr.getHostName();

} catch (UnknownHostException e) {

//your code here

}

this is just a template, modify it as per your equirements, dont use it as is as it might have problems.

kilyasa at 2007-7-29 16:06:00 > top of Java-index,Java Essentials,Java Programming...