19mis0349 VL2020210503313 Ast02
19mis0349 VL2020210503313 Ast02
19mis0349 VL2020210503313 Ast02
Slot : L59+L60
Faculty : Dr.C.NAVANEETHAN
Course Code: SWE2002
LAB ASSESSMENT –2
NAME: K.B.NEERAJ KUMAR
REG NO: 19MIS0349
import java.net.InetAddress;
import java.net.UnknownHostException;
public class nslookup{
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Please provide valid arguments");
return;
}
try {
InetAddress ipaddress = InetAddress.getByName(args[0]);
System.out.println("Name : " + ipaddress.getHostName());
System.out.println("Address : " + ipaddress.getHostAddress());
}
catch (UnknownHostException e) {
System.out.println("Error occured : " + e.getMessage());
}
}
}
5. Write a program to download the contents associated
with a HTTP URL and save it in a file.
import java.net.*;
import java.util.*;
public class urlparts
{
public static void main(String args[]){ try{
URL aurl=new
URL("https://www.codejava.net/java-se/networking/use-
httpurlconnection-to-download-file-from-an-http-url");
System.out.println("protocol="+aurl.getProtocol());
System.out.println("authority="+aurl.getAuthority());
System.out.println("host= "+aurl.getHost()); System.out.println("port=
"+aurl.getPort());
System.out.println("path= "+aurl.getPath()); System.out.println("query=
"+aurl.getQuery()); System.out.println("filename= "+aurl.getFile());
System.out.println("ref= "+aurl.getRef());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class Url
{
public static void main(String args[])
{try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
File f=new File("test.txt");
Filewriter fw=new Filewriter(f);
System.out.println("Enter the url which you want to download:");
String str=br.readLine();
URL u=new URL(str);
InputStream in=u.openStream();
BufferedReader brl=new BufferedReader(new
InputStreamReader(in));
string strl;
while((strl=brl.readLine())!=null){
fw.write(strl+"\r\n");
}
System.out.println("the content of the url:"+str+" is download and
saved in
text.txt");
fw.close();
}catch(Exeption e){System.out.println(e);
}}}
6. Write an Java program to illustrate TRACEROUTE
specifying the maximum number of hops in the path.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class pingIP {
public static void runSystemCommand(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String s = "";
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String ip = "www.vit.ac.in";
runSystemCommand("tracert -h 15 " + ip);
}
}
7. Write a Program to illustrate the loopback address for
testing TCP/IP configuration.
import java.io.*;
import java.net.*;
public class pingIP1
{
public static void runSystemCommand(String command)
{
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String s = "";
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String ip = "www.google.com";
runSystemCommand("ping " + ip);
}
}
Enumeration<NetworkInterface> interfaces =
NetworkInterface
.getNetworkInterfaces();
System.out.println();
System.out
.println("Following are the available network interfaces");
System.out.println();
if (interfaces == null) {
System.out.println("No network interfaces found");
}
else {
for (NetworkInterface netIf : Collections.list(interfaces)) {
System.out.println("Display Name : "+
netIf.getDisplayName());
System.out.println("Name : " +
netIf.getName());System.out.println();
}
}
} catch (UnknownHostException ex) {
System.out.println("Error Occured : " + ex.getMessage());
}
catch (SocketException e) {
System.out.println("Error Occured : " + e.getMessage());
}
}
}
9. Write a program to display active TCP Connections
and illustrate the possible state of TCP Connection.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TCP {
public static void runSystemCommand(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String s = "";
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
runSystemCommand("netstat -an");
}
}
10. Explain the protocols TCP, UDP, ICMP, and IP
Protocols. Illustrate TCP, UDP, ICMP for IPv6 protocol.
Identify the command that can be used to specify a set of
protocols.
Ans: TCP protocol: It is a connection-oriented protocol that first
establishes a logical connection between transport layers at two
hosts before transferring data. It creates a logical pipe between two
TCPs for transferring a stream of bytes.
UDP protocol: Is a connection-less protocol that transmits user
datagrams without first creating a logical connection. In UDP , each
user datagram is an independent entity without being related to the
previous or the next one. UDP is a simple protocol that does not
provide flow, error, or congestion control.