Getting the IP Address of a Hostname - Java Network

Java examples for Network:IP Address

Description

Getting the IP Address of a Hostname

Demo Code

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

public class Main {
  public void m() {
    try {//from   www  . j a va 2 s.  c  o  m
      InetAddress addr = InetAddress.getByName("java2s.com");
      byte[] ipAddr = addr.getAddress();

      // Convert to dot representation
      String ipAddrStr = "";
      for (int i = 0; i < ipAddr.length; i++) {
        if (i > 0) {
          ipAddrStr += ".";
        }
        ipAddrStr += ipAddr[i] & 0xFF;
      }
    } catch (UnknownHostException e) {
    }
  }
}

Related Tutorials