fork download
  1. import java.net.InetAddress;
  2.  
  3. import java.net.UnknownHostException;
  4.  
  5.  
  6. public class Inet {
  7.  
  8.  
  9. public static void main(String[] args) {
  10.  
  11. try {
  12.  
  13. InetAddress address = InetAddress.getLocalHost();
  14.  
  15. showInformations(address, "Hôte local");
  16.  
  17.  
  18.  
  19. address = InetAddress.getByAddress(
  20.  
  21. new byte[]{(byte)192, (byte)168, 2, 44}
  22.  
  23. );
  24.  
  25. showInformations(address, "192.168.2.44");
  26.  
  27.  
  28.  
  29. address = InetAddress.getByName("localhost");
  30.  
  31. showInformations(address, "locahost");
  32.  
  33.  
  34.  
  35. address = InetAddress.getByName("127.0.0.1");
  36.  
  37. showInformations(address, "127.0.0.1");
  38.  
  39.  
  40.  
  41. } catch (UnknownHostException e) {
  42.  
  43. e.printStackTrace();
  44.  
  45. }
  46.  
  47. }
  48.  
  49.  
  50.  
  51. public static void showInformations(InetAddress address, String name){
  52.  
  53. System.out.println("-----------------------------------------------");
  54.  
  55. System.out.println("INFORMATIONS DE " + name);
  56.  
  57. System.out.println("-----------------------------------------------");
  58.  
  59. System.out.println("Nom : " + address.getHostName());
  60.  
  61. System.out.println("Adresse : " + address.getHostAddress());
  62.  
  63. System.out.println("Nom canonique : " + address.getCanonicalHostName());
  64.  
  65. //Cette méthode nous retourne un tableau de byte
  66.  
  67. byte[] bAddress = address.getAddress();
  68.  
  69. String ip = "";
  70.  
  71. for(byte b : bAddress)
  72.  
  73. ip +=(b & 0xFF) + ".";//L'instruction & 0xFF permet d'avoir la valeur non signée
  74.  
  75.  
  76. System.out.println("Adresse IP depuis tableau de byte : " + ip);
  77.  
  78. }
  79.  
  80. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:6: error: class Inet is public, should be declared in a file named Inet.java
public class Inet {
       ^
1 error
stdout
Standard output is empty