19mis0018 CN Lab 2

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

Winter Semester – 2020-21

Computer Networks Laboratory

DIGITAL ASSIGNMENT-2

Name : ESWAR G
Reg.No: 19MIS0018
Course Code : SWE2002
Faculty : Dr.C.NAVANEETHAN
Slot : VL2020210503313
1. Write a program to display the name and IP address of device that you
are currently working on.
CODE :

import java.net.*;

import java.io.*;

import java.util.*;

import java.net.InetAddress;

public class NameIp{

public static void main(String[] args) throws Exception {

InetAddress localhost = InetAddress.getLocalHost();

System.out.println("System IP Address : " +

(localhost.getHostAddress()).trim());

String systemipaddress = "";

}}

OUTPUT:

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 .

CODE:

import java.net.InetAddress;

import java.net.UnknownHostException;
public class IpAdd{

public static void main(String[] args) throws

UnknownHostException {

System.out.println(InetAddress.getByName("www.vit.ac.in"));

System.out.println(InetAddress.getByName("www.google.com"));

InetAddress[] inetAddresses
=InetAddress.getAllByName("www.microsoft.com");

for(InetAddress ipAddress : inetAddresses) {

System.out.println(ipAddress);

break;

}}}

OUTPUT:

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

CODE:
import java.io.*;

import java.net.*;

import java.util.*;

import static java.lang.System.out;

public class LocalHost {

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 {

System.out.println("Display name:Localhost %s\n" +


netint.getDisplayName());

System.out.println("Name:Localhost %s\n" + netint.getName());

Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();

for (InetAddress inetAddress : Collections.list(inetAddresses)) {

System.out.println("InetAddress: %s\n" + inetAddress);

System.out.println("\n");

}}

OUTPUT:
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.

CODE:

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Nslookup{

public static void main(String[] args) {


try {

InetAddress ipaddress = InetAddress.getByName("www.google.com");

System.out.println("Name : " + ipaddress.getHostName());

System.out.println("Address : " + ipaddress.getHostAddress());

catch (UnknownHostException e){

System.out.println("ERROR IS : " + e.getMessage());

}}}

OUTPUT:

5. Write a program to download the contents associated with a HTTP


URL and save it in a file.

CODE:

import java.net.*;

import java.util.*;

class Url{

public static void main(String args[]){

try{

URL aurl=new
URL("https://www.codejava.net/java-
se/networking/usehttpurlconnection-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();

}}}

OUTPUT:

6. Write an Java program to illustrate TRACEROUTE specifying the


maximum number of hops in the path.

CODE:

import java.io.*;import java.net.*;

import java.lang.*;
class Traceroute{

public static void main(String args[]){

BufferedReader in;

try{

Runtime r = Runtime.getRuntime();

Process p = r.exec("tracert www.google.com");

in = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;

if(p==null)

System.out.println("could not connect");

while((line=in.readLine())!=null){

System.out.println(line);

}catch(IOException e){

System.out.println(e.toString());

}}}

OUTPUT:
7. Write a Program to illustrate the loopback address for testing TCP/IP
configuration.

CODE:

import java.net.InetAddress;

public class LoopbackAddress { public static void main(String[] args)


throws Exception {

InetAddress addr = InetAddress.getLoopbackAddress();

System.out.println("Address is: "+addr.getHostAddress());

}}

OUTPUT:

8. Write a program to display IP address of host and local host with cache
tables for all interfaces.

CODE:
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 is : " + ex.getMessage());

catch (SocketException e) {

System.out.println("ERROR is : " + e.getMessage());

}}}
OUTPUT:
9. Write a program to display active TCP Connections and illustrate the
possible state of TCP Connection.

CODE:

import java.io.*;

import java.net.*;

class TCPClients {

public static void main (String argv[]) throws Exception {


String sentence;

String modifiedSentence;

BufferedReader inFromUser = new BufferedReader (new

InputStreamReader(System.in));

Socket clientSocket = new Socket("blue.cs.yorku.ca", 5555);

DataOutputStream outToServer =

new DataOutputStream (clientSocket.getOutputStream());

BufferedReader inFromServer = new BufferedReader (new

InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence);

modifiedSentence = inFromServer.readLine();

System.out.println("FROM SERVER:" + modifiedSentence);

clientSocket.close();

}
OUTPUT:
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.

ANSWER:

Transmission Control Protocol (TCP) – a connection-oriented


communications protocol that facilitates the exchange of messages between
computing devices in a network. It is the most common protocol in networks
that use the Internet Protocol (IP); together they are sometimes referred to
as TCP/IP.

Internet Protocol (IP) – a set of rules that dictate how data should be delivered
over the public network (Internet). Often works in conjunction with the
transmission control protocol (TCP), which divides traffic into packets for
efficient transport through the Internet; together they are referred to as TCP/IP.

User Datagram Protocol (UDP) – a communications protocol that facilitates


the exchange of messages between computing devices in a network. It's an
alternative to the transmission control protocol (TCP).

ICMP is a transport level protocol within TCP/IP which communicates


information about network connectivity issues back to the source of the
compromised transmission. It sends control messages such as destination
network unreachable, source route failed, and source quench

TCP-IPv6 (PING Command)" to check whether the network settings are


correct.

 Netstat [-a] [-b] [-e] [-n] [-0] [-p <Protocol>] [-r] [-s]

The -p parameter can be used to specify a set of protocols. Displays the


contents of the IP routing table. This is equivalent to the route print command

You might also like