Java Tutorial/Network/Internet Addresses — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:03, 1 июня 2010
Содержание
- 1 Compare two InetAddresses
- 2 Create InetAddress by Name
- 3 Get All addresses by name
- 4 Get canonical host name
- 5 Get Host name from InetAddress
- 6 Get local host from InetAddress
- 7 Get my own address
- 8 Getting the IP Address of a Hostname
- 9 java.net.InetAddress.isReachable(int) can be used to check if a server is reachable or not.
- 10 Ping a host
- 11 Translate host names to Internet addresses
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