Java InetAddress get local host address

Description

Java InetAddress get local host address

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
  public static void main(String[] args) {
    try {//w w w.  j av a2s  .c o m
      InetAddress addr = InetAddress.getLocalHost();
      System.out.println("My computer name: " + addr.getHostName());
      System.out.println("My computer IP address: " + addr.getHostAddress());
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related