0% found this document useful (0 votes)
59 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)
59 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

UNIT 2 INTERNET ADDRESSES LAB SOLUTIONS, ER.

SHARAT MAHARJAN

UNIT 2
Internet Addresses Lab

1. WAP that prints the address of [Link].


import [Link];
import [Link];
public class JavaInternetAddressByName {
public static void main(String[] args) {
try {
InetAddress address =
[Link]("[Link]");
[Link](address);
} catch(UnknownHostException e) {
[Link]("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. [Link]).


import [Link].*;
public class ReverseTest {
public static void main(String[] args) {
try {
InetAddress machine =
[Link]("[Link]");
[Link]([Link]());
} catch (UnknownHostException e) {
[Link]("No hostname found.");
}
}
}

3. Find the IP address of the local machine.


import [Link].*;
public class IPLocal {
public static void main(String[] args) {
try {
InetAddress machine = [Link]();
[Link]([Link]());
} catch (UnknownHostException e) {
[Link]("No hostname found.");
}}}

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

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


import [Link].*;
public class AddressTest{
public static void main(String[] args) {
try {
InetAddress machine =
[Link]("[Link]");
byte[] address = [Link]();
if([Link] == 4)
[Link]("IPv4 is being used.");
else
[Link]("IPv6 is being used.");
} catch (UnknownHostException e) {
[Link]("No hostname found.");
}}}

5. Are [Link] and [Link] the same?


import [Link].*;
public class IBiblioAliases {
public static void main (String args[]) {
try {
InetAddress ibiblio = [Link]("[Link]");
InetAddress helios = [Link]("[Link]");
if ([Link](helios)) {

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

[Link]
("[Link] is the same as [Link]");
} else {
[Link]
("[Link] is not the same as [Link]");
}} catch (UnknownHostException ex) {
[Link]("Host lookup failed.");
}}}

6. A program that lists all the network interfaces.


import [Link].*;
import [Link].*;
public class InterfaceLister {
public static void main(String[] args) throws SocketException {
Enumeration<NetworkInterface> interfaces = NetworkInterface.
getNetworkInterfaces();
while ([Link]()) {
NetworkInterface ni = [Link]();
[Link](ni);
}}}

7. WAP for Spam Check.


import [Link].*;
public class SpamCheck {
public static final String BLACKHOLE = "[Link]";

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)) {
[Link](arg + " is a known spammer.");
} else {
[Link](arg + " appears legitimate.");
} } }

private static boolean isSpammer(String arg) {


try {
InetAddress address = [Link](arg);
byte[] quad = [Link](); //bytes not string
String query = BLACKHOLE;
for (byte octet : quad) {
int unsignedByte = octet < 0 ? octet + 256 : octet;
query = unsignedByte + "." + query;
}
[Link](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 [Link].*;
import [Link].*;
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 = [Link]();
entry != null;
entry = [Link]()) {
// separate out the IP address
int index = [Link](' ');
String ip = [Link](0, index);
String theRest = [Link](index);
// Ask DNS for the hostname and print it out
try {
InetAddress address = [Link](ip);
[Link]([Link]() + theRest);
} catch (UnknownHostException ex) {
[Link](entry);
} } } catch (IOException ex) {
[Link]("Exception: " + ex);
}}}

You might also like