0% found this document useful (0 votes)
9 views7 pages

Lab 1 NW

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Lab 1 NW

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1. A program that prints the address of www.oreilly.

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

2. Find the address of the local machine


import java.net.*;
public class MyAddress {
public static void main (String[] args) {
try{
InetAddress address = InetAddress.getLocalHost();
System.out.println(address);
}catch(UnknownHostException ex){
System.out.println("Could not find this computer's address");
}
}
}
3. Given the address, find the hostname
import java.net.*;
public class ReverseTest {
public static void main (String[] args) throws UnknownHostException {
InetAddress ia = InetAddress.getByName("23.49.69.74");
System.out.println(ia.getCanonicalHostName());
}
}

4. Find the IP address of the local machine


import java.net.*;
public class MyAddress1 {
public static void main (String[] args) {
try {
InetAddress me = InetAddress.getLocalHost();
String dottedQuad = me.getHostAddress ();
System.out.println("My address is " + dottedQuad);
} catch (UnknownHostException ex) {
System.out.println("I'm sorry. I dont know my own address");
}
}

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

6. Testing the characteristics of an IP address


import java.net.*;
public class IPCharacteristics {
public static void main(String[] args) {
try{
InetAddress address = InetAddress.getByName(args[0]);
if(address.isAnyLocalAddress()){
System.out.println(address + "is a wildcard address.");
}
if(address.isLoopbackAddress()){
System.out.println(address + "is loopback address.");
}
if(address.isLinkLocalAddress()){
System.out.println(address + "is a link-local address.");
} else if(address.isSiteLocalAddress()){
System.out.println(address + "is a site-local address.");
} else{
System.out.println(address + "is a global address.");
}
if(address.isMulticastAddress()){
if(address.isMCGlobal()){
System.out.println(address + " is a global multicast address.");
} else if(address.isMCOrgLocal()){
System.out.println(address + " is an organization wide multicast address.");
} else if (address.isMCSiteLocal()){
System.out.println(address + " is a site wide multicast address.");
} else if (address.isMCLinkLocal()){
System.out.println(address + " is a subnet wide multicast address.");
} else if (address.isMCNodeLocal()){
System.out.println(address + " is an interface-local multicast address.");
} else {
System.out.println(address + " is an unknown multicast address type.");
}
} else {
System.out.println(address + " is a unicast address.");
}
} catch(UnknownHostException ex){
System.err.println("Could not resolve");
}
}
}

7. Are www.ibiblio.org and helios.ibiblio.org the same?


import java.net.*;
public class IBiblioAliases {
public static void main (String args[]) {
try {
InetAddress ibiblio = InetAddress.getByName("www.ibiblio.org");
InetAddress helios = InetAddress.getByName("helios.ibiblio.org");
if (ibiblio.equals(helios)) {
System.out.println
("www.ibiblio.org is the same as helios.ibiblio.org");
} else {
System.out.println
("www.ibiblio.org is not the same as helios.ibiblio.org");
}
} catch (UnknownHostException ex) {
System.out.println("Host lookup failed.");
}
}
}
8. A program that lists all the network interfaces
import java.net.*;
import java.util.*;
public class InterfaceLister {
public static void main(String[] args) throws SocketException {
Enumeration<NetworkInterface> interfaces =
NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
System.out.println(ni);
}
}
}
9. SpamCheck
import java.net.*;
public class SpamCheck {
public static final String BLACKHOLE = "sbl.spamhaus.org";
public static void main(String[] args) throws UnknownHostException {
for (String arg: args) {
if (isSpammer(arg)) {
System.out.println(arg + " is a known spammer.");
} else {
System.out.println(arg + " appears legitimate.");
}
}

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

You might also like