adsense


Showing posts with label JAVA EXAMPLES DETERMINE LOCAL IP ADDRESS. Show all posts
Showing posts with label JAVA EXAMPLES DETERMINE LOCAL IP ADDRESS. Show all posts

Saturday, 31 October 2015

JAVA EXAMPLES DETERMINE LOCAL IP ADDRESS PAGE 181

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES DETERMINE LOCAL IP ADDRESS







Java Examples - Determine Local IP Address



Problem Description:
How to determine IP Address & hostname of Local Computer?
Solution:
Following example shows how to find local IP Address & Hostname of the system using getLocalAddress() method of InetAddress class.
import java.net.InetAddress;

public class Main {
   public static void main(String[] args) 
   throws Exception {
      InetAddress addr = InetAddress.getLocalHost();
      System.out.println("Local HostAddress: 
      "+addr.getHostAddress());
      String hostname = addr.getHostName();
      System.out.println("Local host name: "+hostname);
   }
}
Result:
The above code sample will produce the following result.
Local HostAddress: 192.168.1.4
Local host name: harsh