Friday, February 10, 2012

Get Your PC's IP Address in Java

This example returns IP address of your PC.

import java.net.*;

public class TellMyIP {

public static void main(String[] args) {

 InetAddress localHostIP= null;

   //creates instance of InetAddress

  try{

  localHostIP=InetAddress.getLocalHost();

  //initializes localHostIP

  System.out.println("IP ADDRESS OF MY PC IS = "+localHostIP.getHostAddress());

  //gets IP address and prints it

  }catch (Exception exception){

  System.out.println("Something that went wrong is :"+exception.getMessage());

  }

   }

}

No comments:

Post a Comment