Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
50 views6 pages

UNIT 2 Internet Addresses Lab Solutions

Uploaded by

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

UNIT 2 Internet Addresses Lab Solutions

Uploaded by

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

UNIT 2 INTERNET ADDRESSES LAB SOLUTIONS, ER.

SHARAT MAHARJAN

UNIT 2
Internet Addresses Lab

1. WAP that prints the address of www.javatpoint.com.


import java.net.InetAddress;
import java.net.UnknownHostException;
public class JavaInternetAddressByName {
public static void main(String[] args) {
try {
InetAddress address =
InetAddress.getByName("www.javatpoint.com");
System.out.println(address);
} catch(UnknownHostException e) {
System.out.println("Couldn't find the host.");
}
}
}

1
UNIT 2 INTERNET ADDRESSES LAB SOLUTIONS, ER. SHARAT MAHARJAN

2. WAP to find the hostname of any address (e.g. 104.21.79.8).


import java.net.*;
public class ReverseTest {
public static void main(String[] args) {
try {
InetAddress machine =
InetAddress.getByName("104.21.79.8");
System.out.println(machine.getCanonicalHostName());
} catch (UnknownHostException e) {
System.out.println("No hostname found.");
}
}
}

3. Find the IP address of the local machine.


import java.net.*;
public class IPLocal {
public static void main(String[] args) {
try {
InetAddress machine = InetAddress.getLocalHost();
System.out.println(machine.getHostAddress());
} catch (UnknownHostException e) {
System.out.println("No hostname found.");
}}}

2
UNIT 2 INTERNET ADDRESSES LAB SOLUTIONS, ER. SHARAT MAHARJAN

4. Determining whether an IP address is v4 or v6.


import java.net.*;
public class AddressTest{
public static void main(String[] args) {
try {
InetAddress machine =
InetAddress.getByName("www.prime.edu.np");
byte[] address = machine.getAddress();
if(address.length == 4)
System.out.println("IPv4 is being used.");
else
System.out.println("IPv6 is being used.");
} catch (UnknownHostException e) {
System.out.println("No hostname found.");
}}}

5. 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)) {

3
UNIT 2 INTERNET ADDRESSES LAB SOLUTIONS, ER. SHARAT MAHARJAN

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

6. 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);
}}}

7. WAP for Spam Check.


import java.net.*;
public class SpamCheck {
public static final String BLACKHOLE = "sbl.spamhaus.org";

4
UNIT 2 INTERNET ADDRESSES LAB SOLUTIONS, ER. SHARAT MAHARJAN

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(); //bytes not string
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;
}}}

5
UNIT 2 INTERNET ADDRESSES LAB SOLUTIONS, ER. SHARAT MAHARJAN

8. WAP for processing web server logfiles.


import java.io.*;
import java.net.*;
public class Weblog {
public static void main(String[] args) {
try (FileInputStream fin = new FileInputStream(args[0]);
Reader in = new InputStreamReader(fin);
BufferedReader bin = new BufferedReader(in);) {
for (String entry = bin.readLine();
entry != null;
entry = bin.readLine()) {
// separate out the IP address
int index = entry.indexOf(' ');
String ip = entry.substring(0, index);
String theRest = entry.substring(index);
// Ask DNS for the hostname and print it out
try {
InetAddress address = InetAddress.getByName(ip);
System.out.println(address.getHostName() + theRest);
} catch (UnknownHostException ex) {
System.err.println(entry);
} } } catch (IOException ex) {
System.out.println("Exception: " + ex);
}}}

You might also like