Java Tutorial/Network/Internet Addresses

Материал из Java эксперт
Перейти к: навигация, поиск

Compare two InetAddresses

   <source lang="java">

import java.net.InetAddress; public class MainClass {

 public static void main(String args[]) throws Exception {
   InetAddress ca = InetAddress.getByName("www.google.ca");
   InetAddress com = InetAddress.getByName("www.google.ru");
   if (ca.equals(com)) {
     System.out.println("same");
   } else {
     System.out.println("not the same");
   }
 }

}</source>



not the same


Create InetAddress by Name

   <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>





Get All addresses by name

   <source lang="java">

import java.net.*; public class MainClass {

 public static void main (String[] args) {
   try {
     InetAddress[] addresses = InetAddress.getAllByName("www.google.ru");
     for (int i = 0; i < addresses.length; i++) {
       System.out.println(addresses[i]);
     }
   }
   catch (UnknownHostException e) {
     System.out.println("Could not find www.apple.ru");
   }
 }

}</source>



www.google.ru/72.14.253.147
www.google.ru/72.14.253.99
www.google.ru/72.14.253.103
www.google.ru/72.14.253.104


Get canonical host name

   <source lang="java">

import java.net.InetAddress; public class Main {

 public static void main(String[] argv) throws Exception {
   byte[] ipAddr = new byte[] { 127, 0, 0, 1 };
   InetAddress addr = InetAddress.getByAddress(ipAddr);
   String hostnameCanonical = addr.getCanonicalHostName();
 }

}</source>





Get Host name from InetAddress

   <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>





Get local host from InetAddress

   <source lang="java">

import java.net.InetAddress; import java.net.UnknownHostException; 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 (UnknownHostException e) {
     System.out.println("I"m sorry. I don"t know my own address.");
   }
 }

}</source>





Get my own address

   <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>





Getting the IP Address of a Hostname

   <source lang="java">

import java.net.InetAddress; public class Main {

 public static void main(String[] argv) throws Exception {
   InetAddress addr = InetAddress.getByName("www.google.ru");
   System.out.println(addr.getHostAddress());
   
 }

}</source>





java.net.InetAddress.isReachable(int) can be used to check if a server is reachable or not.

   <source lang="java">

import java.net.InetAddress; public class Main {

 public static void main(String[] argv) throws Exception {
   InetAddress address = InetAddress.getByName("web.mit.edu");
   System.out.println("Name: " + address.getHostName());
   System.out.println("Addr: " + address.getHostAddress());
   System.out.println("Reach: " + address.isReachable(3000));
 }

}</source>





Ping a host

   <source lang="java">

import java.net.InetAddress; public class Main {

 public static void main(String[] args) throws Exception {
   InetAddress address = InetAddress.getByName("17.16.15.14");
   boolean reachable = address.isReachable(10000);
   System.out.println("Is host reachable? " + reachable);
 }

}</source>





Translate host names to Internet addresses

   <source lang="java">

import java.net.InetAddress; import java.net.UnknownHostException; public class MainClass {

 public static void main(String args[]) {
   try {
     InetAddress inetAddr = InetAddress.getByName("www.jexp.ru");
     System.out.println(inetAddr.toString());
   } catch (UnknownHostException ex) {
   }
 }

}</source>



www.jexp.ru/208.109.14.69