Lab 1 NW
Lab 1 NW
com
import java.net.*;
public class OReillyByName {
public static void main (String[] args){
try {
InetAddress address = InetAddress.getByName("www.oreilly.com");
System.out.println(address);
} catch (UnknownHostException ex) {
System.out.println("Could not find www.oreilly.com");
}
}
}
}
5. Determining whether an IP address is v4 or v6
import java.net.*;
public class AddressTests {
public static void main(String[] args) {
try{
InetAddress ia = InetAddress.getByName("28.49.69.74");
byte[] address = ia.getAddress();
if(address.length == 4){
System.out.println("4");
}
else if(address.length == 16){
System.out.println("6");
}
else{
System.out.println("-1");
}
}
catch(UnknownHostException ex){
System.out.println("could not find");
}
}
}
}
private static boolean isSpammer(String arg) {
try {
InetAddress address = InetAddress.getByName(arg);
byte[] quad = address.getAddress();
String query = BLACKHOLE;
for (byte octet : quad) {
int unsignedByte = octet < 0 ? octet + 256 : octet;
query = unsignedByte + "." + query;
}
InetAddress.getByName(query);
return true;
} catch (UnknownHostException e) {
return false;
}
}
}