Java by API/java.net/InetAddress — различия между версиями

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

Текущая версия на 14:16, 31 мая 2010

InetAddress: getAllByName(String name)

 
/*
 * 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();
    }
  }
}





InetAddress: getByName(String host)

  
 
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);
    }
  }
}





InetAddress: getCanonicalHostName()

  
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();
  }
}





InetAddress: getHostAddress()

  
 
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();
    }
  }
}





InetAddress: getHostName()

  
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.");
    }
  }
}





InetAddress: getLocalHost()

  
 
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();
    }
  }
}





InetAddress: isReachable(int timeout)

 
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");
    }
  }
}