Java by API/java.net/InetAddress
Содержание
InetAddress: getAllByName(String name)
<source lang="java">
/*
* Output: * */
import java.net.InetAddress; public class MainClass {
public static void main(String args[]) { try { // Get an address InetAddress ias[] = InetAddress.getAllByName("www.google.ru"); for (int i = 0; i < ias.length; i++) { System.out.println(ias[i].getHostName()); System.out.println(ias[i].getHostAddress()); byte bytes[] = ias[i].getAddress(); for (int j = 0; j < bytes.length; j++) { if (j > 0) System.out.print("."); if (bytes[j] >= 0) System.out.print(bytes[j]); else System.out.print(bytes[j] + 256); } System.out.println(""); } } catch (Exception e) { e.printStackTrace(); } }
}
</source>
InetAddress: getByName(String host)
<source lang="java">
import java.net.InetAddress; public class MainClass {
public static void main(String[] args) { try { InetAddress ia = InetAddress.getByName("64.21.29.37"); System.out.println(ia.getHostName()); } catch (Exception ex) { System.err.println(ex); } }
}
</source>
InetAddress: getCanonicalHostName()
<source lang="java">
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(); }
}
</source>
InetAddress: getHostAddress()
<source lang="java">
import java.net.InetAddress; public class MainClass {
public static void main(String[] args) { try { InetAddress me = InetAddress.getLocalHost(); String dottedQuad = me.getHostAddress(); System.out.println("My address is " + dottedQuad); } catch (Exception ex) { ex.printStackTrace(); } }
}
</source>
InetAddress: getHostName()
<source lang="java">
import java.net.InetAddress; import java.net.UnknownHostException; public class MainClass {
public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost(); System.out.println("My name is " + address.getHostName()); } catch (UnknownHostException e) { System.out.println("I"m sorry. I don"t know my own name."); } }
}
</source>
InetAddress: getLocalHost()
<source lang="java">
import java.net.InetAddress; public class MainClass {
public static void main(String[] args) { try { InetAddress me = InetAddress.getLocalHost(); String dottedQuad = me.getHostAddress(); System.out.println("My address is " + dottedQuad); } catch (Exception ex) { ex.printStackTrace(); } }
}
</source>
InetAddress: isReachable(int timeout)
<source lang="java">
import java.io.IOException; import java.net.InetAddress; public class MainClass {
public static void main(String[] args) throws IOException { try { int timeout = 2000; InetAddress[] addresses = InetAddress.getAllByName("www.jexp.ru"); for (InetAddress address : addresses) { if (address.isReachable(timeout)) System.out.printf("%s is reachable%n", address); else System.out.printf("%s could not be contacted%n", address); } } catch (Exception e) { System.out.printf("Unknown host: %s%n", "www.jexp.ru"); } }
}
</source>