Mega Code Archive

 
Categories / Java / Network Protocol
 

Retrieve the hostname of an IP Address

import java.net.InetAddress; public class Main {   public static void main(String[] argv) throws Exception {     InetAddress addr = InetAddress.getByName("127.0.0.1");     byte[] ipAddr = new byte[] { 127, 0, 0, 1 };     addr = InetAddress.getByAddress(ipAddr);     // Get the host name     String hostname = addr.getHostName();     // Get canonical host name     String canonicalhostname = addr.getCanonicalHostName();   } }