Getting the IP Address and Hostname of the Local Machine - Java Network

Java examples for Network:IP Address

Description

Getting the IP Address and Hostname of the Local Machine

Demo Code

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

public class Main {
  public static void main(String[] args) {
    try {/*from  ww  w .  j  a  v a 2  s  .  c  o m*/
      InetAddress addr = InetAddress.getLocalHost();

      // Get IP Address
      byte[] ipAddr = addr.getAddress();

      // Get hostname
      String hostname = addr.getHostName();
    } catch (UnknownHostException e) {
    }
  }
}

Related Tutorials