Java Tutorial/Network/Internet Addresses
Содержание
- 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
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");
}
}
}
not the same
Create InetAddress by Name
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);
}
}
}
Get All addresses by name
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");
}
}
}
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
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();
}
}
Get Host name from InetAddress
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.");
}
}
}
Get local host from InetAddress
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.");
}
}
}
Get my own address
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();
}
}
}
Getting the IP Address of a Hostname
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());
}
}
java.net.InetAddress.isReachable(int) can be used to check if a server is reachable or not.
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));
}
}
Ping a host
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);
}
}
Translate host names to Internet addresses
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) {
}
}
}
www.jexp.ru/208.109.14.69