Monday, March 23, 2009

How do I get the ip address of the machine a Java application is running on?

Use the InetAddress class.


InetAddress address = InetAddress.getLocalHost();
byte[] ip = address.getAddress();

// to get a string representation use
// the getHostAddress() method

String ipString = address.getHostAddress();

// for a string representation in
// form hostname/literal IP address
// use the toString() method

String hostAndIp = address.toString();

No comments: