19mis0349 VL2020210503313 Ast02

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

SCHOOL OF INFORMATION TECHNOLOGY AND ENGINEERING

Winter Semester – 2020-21


Course Name:-Computer Networks Laboratory

Slot : L59+L60
Faculty : Dr.C.NAVANEETHAN
Course Code: SWE2002

LAB ASSESSMENT –2
NAME: K.B.NEERAJ KUMAR
REG NO: 19MIS0349

BASIC NETWORK PROGRAMS

1.Write a program to display the name and IP address of


device that you are currently working on.
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Computername {
public static void main(String[] args) {
try {
InetAddress ipaddress = InetAddress.getLocalHost();
System.out.println("Computer Host Name : "+
ipaddress.getHostName());
System.out.println("IP Address of Localhost : "+
ipaddress.getHostAddress());
}
catch (UnknownHostException ex) {
System.out.println("Error Occured : " + ex.getMessage());
}
}
}

2. Write a program to print the IP address of


www.vit.ac.in,, www.google.com and all IP addresses of
www.microsoft.com .
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Address {
public static void main(String[] args) {
InetAddress address = null;
InetAddress address1 = null;
try {
address = InetAddress.getByName("www.vit.ac.in");
System.out.println(address.getHostName() + "=" +
address.getHostAddress());
address1 = InetAddress.getByName("www.google.com");
System.out.println(address1.getHostName() + "=" +
address1.getHostAddress());
InetAddress[] addresses =
InetAddress.getAllByName("www.microsoft.com");
for (int i = 0; i < addresses.length; i++) {
System.out.println(addresses[i].getHostName() + "=" +
addresses[i].getHostAddress());
}
}
catch (UnknownHostException e) {
System.exit(2);
}
}
}

3. Write a program to print “localhost” to all Network


Interfaces.
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class Listofnetworks {
public static void main(String args[]) throws
SocketException {
Enumeration<NetworkInterface> nets =
NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface
netint) throws SocketException {
out.printf("Display name:Localhost %s\n",
netint.getDisplayName());
out.printf("Name:Localhost %s\n", netint.getName());
Enumeration<InetAddress> inetAddresses =
netint.getInetAddresses();
for (InetAddress inetAddress :
Collections.list(inetAddresses)) { out.printf("InetAddress:
%s\n", inetAddress);
}
out.printf("\n");
}
}
import java.net.*;
import java.util.*;
public class InterfaceList
{
public static void main(String args[]) throws Exception
{
Enumeration interfaces= NetworkInterface.getNetworkInterfaces();
while(interfaces.hasMoreElements() )
{
NetworkInterface ni=(NetworkInterface)interfaces.nextElement();
System.out.println(ni.getIndex());
System.out.println("Display name:"+ni.getDisplayName());
System.out.println("Name:"+ni.getName());
byte[] hwaddr=ni.getHardwareAddress(); System.out.println("Hw Address:" +hwaddr);
System.out.println("List of Inet Address"); Enumeration addresses=ni.getInetAddresses();
while(addresses.hasMoreElements())
{ System.out.println(addresses.nextElement());
}} } }
4. Write a program to implement the “nslookup” utility.

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

8. Write a program to display IP address of host and local


host with cache tables for all interfaces.
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;import
java.net.UnknownHostException;
import java.util.Collections;
import java.util.Enumeration;
public class Interfaces {
public static void main(String[] args) {
try {
InetAddress ipaddress = InetAddress.getLocalHost();
System.out.println("Computer Host Name : " +
ipaddress.getHostName());
System.out.println("IP Address of Localhost : " +
ipaddress.getHostAddress());

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.

ICMP protocol: The Internet Control Message Protocol is an internet


layer protocol used by network devices to diagnose network
communication issues. ICMP is mainly used to determine whether
ornot data is reaching its intended destination in a timely manner.
Commonly, the ICMP protocol is used on network devices, such as
routers.

IP protocol: It defines the format of the packet, called a datagram at


the network layer. It also defines the format and the structure of
addresses used in this layer. It is also responsible for routing a packet
from its source to its destination, which is achieved by each router
forwarding the datagram to the next router in its path.
The -p parameter can be used to specify a set of protocols. Displays
the contents of the IP routing table.

You might also like