CS8381 - Data Structures Laboratory
CS8381 - Data Structures Laboratory
Lab Manual
A B Prameela SVCT
2
Exercise No: 1
Learn to use commands like tcpdump, netstat, ifconfig, nslookup, and traceroute.
Capture ping and traceroute PDUs using a network protocol analyzer and examine.
Aim:
To use commands like tcpdump, netstat, ifconfig, nslookup, and traceroute. Capture ping and
traceroute PDUs using a network protocol analyzer and examine.
1. Tcpdump
Tcpdump is a command-line utility that allows you to capture and analyze network traffic going
through your system.
Procedure
bash
$ which tcpdump
/usr/sbin/tcpdump
bash
bash
$ su
(and password 123456)
$ sudo -i # Change to root
3. Capturing packets with tcpdump: Use the command tcpdump -D to see which interfaces are
available for capture.
bash
A B Prameela SVCT
3
7. lo [Loopback]
bash
bash
bash
Output:
bash
bash
$ ping opensource.com
2. Netstat
Netstat (network statistics) is a command-line tool for monitoring network connections (both
incoming and outgoing) as well as viewing routing tables and interface statistics.
bash
A B Prameela SVCT
4
3. Ifconfig
Ifconfig displays details of a network interface card like IP address, MAC address, and the status of
the network interface card.
bash
4. Nslookup
Nslookup (stands for “Name Server Lookup”) is a useful command for getting information from DNS
servers. It queries the Domain Name System (DNS) to obtain domain name or IP address mapping.
bash
Non-authoritative answer:
Name: annauniv.edu
Address: 103.70.60.38
5. Traceroute
The traceroute command is used in Linux to map the journey that a packet of information undertakes
from its source to its destination.
bash
Wireshark is a free and open-source network packet analyzer used for network analysis,
troubleshooting, etc. Wireshark has a graphical interface with built-in filtering options, making it easy
to use.
A B Prameela SVCT
5
bash
# sudo apt install wireshark
To Open Wireshark:
bash
# sudo wireshark
bash
# ping www.sudo.com
# traceroute www.google.com
A B Prameela SVCT
6
Result:
Thus commands like tcpdump, netstat, ifconfig, nslookup and traceroute was used. Ping and
traceroute PDUs using a network protocol analyzer was captured and examined.
A B Prameela SVCT
7
Exercise No. 2
Write an HTTP Web Client Program to Download a Web Page Using TCP Sockets
Aim:
To write a program in Java to create an HTTP web client that downloads a web page using TCP
sockets.
Algorithm:
Program: Download.java
import java.io.*;
import java.net.URL;
inputStream.close();
outputStream.close();
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
Output
bash
A B Prameela SVCT
8
F:\Project\Lab>javac Download.java
F:\Project\Lab>java Download
Downloading File From:
http://tutorialspoint.com/java_dip/images/digital_image_processing.jpg
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1097
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1744
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1440
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 548
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1863
Result:
Thus created a program in java for a HTTP web client program to download a web page using TCP
sockets is written and executed successfully.
A B Prameela SVCT
9
Exercise No. 3a
Aim:
To write a program in Java to implement applications using TCP sockets, specifically an echo client
and an echo server.
Algorithm:
Program: Server.java
java
import java.net.*;
import java.lang.*;
import java.io.*;
try {
sersock = new ServerSocket(PORT);
System.out.println("Server Started: " + sersock);
try {
sock = sersock.accept();
System.out.println("Client Connected: " + sock);
DataInputStream ins = new DataInputStream(sock.getInputStream());
System.out.println(ins.readLine());
PrintStream ios = new PrintStream(sock.getOutputStream());
ios.println("Hello from server");
ios.close();
A B Prameela SVCT
10
sock.close();
} catch (SocketException se) {
System.out.println("Server Socket problem: " + se.getMessage());
}
} catch (Exception e) {
System.out.println("Couldn't start: " + e.getMessage());
}
System.out.println("Connection from: " + sock.getInetAddress());
}
}
Program: Client.java
java
import java.lang.*;
import java.io.*;
import java.net.*;
class Client {
public static void main(String args[]) {
Socket sock = null;
DataInputStream dis = null;
PrintStream ps = null;
System.out.println("Trying to connect");
try {
sock = new Socket(InetAddress.getLocalHost(), Server.PORT);
ps = new PrintStream(sock.getOutputStream());
ps.println("Hi from client");
Output
In Server Window:
bash
F:\Project\Lab>javac Server.java
F:\Project\Lab>java Server
Server Started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=4000]
Client Connected: Socket[addr=/192.168.1.18,port=1815,localport=4000]
Hi from client
Connection from: /192.168.1.18
A B Prameela SVCT
11
In Client Window:
bash
F:\Project\Lab>javac Client.java
F:\Project\Lab>java Client
Trying to connect
Hello from server
Result:
Thus, a program in Java was implemented to create applications using TCP sockets, including an echo
client and an echo server.
A B Prameela SVCT
12
Exercise No. 3b
Aim:
To write a program in Java to implement an application using TCP sockets for chat functionality.
Algorithm:
Program: tcpchatserver.java
java
import java.io.*;
import java.net.*;
class tcpchatserver {
public static void main(String args[]) throws Exception {
PrintWriter toClient;
BufferedReader fromUser, fromClient;
try {
ServerSocket Srv = new ServerSocket(4000);
System.out.print("\nServer started\n");
Socket Clt = Srv.accept();
System.out.println("Client connected");
A B Prameela SVCT
13
while (true) {
CltMsg = fromClient.readLine();
if (CltMsg.equals("end"))
break;
else {
System.out.println("Server: " + CltMsg);
System.out.print("Message to Client: ");
SrvMsg = fromUser.readLine();
toClient.println(SrvMsg);
}
}
System.out.println("\nClient Disconnected");
fromClient.close();
toClient.close();
fromUser.close();
Clt.close();
Srv.close();
} catch (Exception E) {
System.out.println(E.getMessage());
}
}
}
Program: tcpchatclient.java
java
import java.io.*;
import java.net.*;
class tcpchatclient {
public static void main(String args[]) throws Exception {
Socket Clt;
PrintWriter toServer;
BufferedReader fromUser, fromServer;
try {
Clt = new Socket(InetAddress.getLocalHost(), 4000);
toServer = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())), true);
fromServer = new BufferedReader(new
InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print("Message to Server: ");
CltMsg = fromUser.readLine();
toServer.println(CltMsg);
if (CltMsg.equals("end"))
break;
SrvMsg = fromServer.readLine();
System.out.println("Client: " + SrvMsg);
}
} catch (Exception E) {
System.out.println(E.getMessage());
}
}
}
A B Prameela SVCT
14
Output
In Server Window:
bash
F:\Project\Lab>javac tcpchatserver.java
F:\Project\Lab>java tcpchatserver
Server started
Client connected
Server: hai
Message to Client: hello
Client Disconnected
In Client Window:
bash
F:\Project\Lab>javac tcpchatclient.java
F:\Project\Lab>java tcpchatclient
Type "end" to Quit
Message to Server: hai
Client: hello
Message to Server: end
Result:
Thus, a program in Java was implemented to create an application using TCP sockets for chat.
A B Prameela SVCT
15
Exercise .No. 4
Aim:
Algorithm:
Program: dnsclient.java
java
import java.io.*;
import java.net.*;
if (args.length == 0)
ipaddress = InetAddress.getLocalHost();
else
ipaddress = InetAddress.getByName(args[0]);
A B Prameela SVCT
16
Program: dnsserver.java
java
import java.io.*;
import java.net.*;
while (true) {
DatagramSocket serversocket = new DatagramSocket(8080);
byte[] senddata = new byte[1021];
byte[] receivedata = new byte[1021];
senddata = capsent.getBytes();
DatagramPacket pack = new DatagramPacket(senddata, senddata.length,
ipaddress, port);
serversocket.send(pack);
A B Prameela SVCT
17
serversocket.close();
}
}
}
Output
In Server Window:
bash
F:\Project\Lab>javac dnsserver.java
F:\Project\Lab>java dnsserver
Press Ctrl + C to Quit
Request for host yahoo.com
In Client Window:
bash
F:\Project\Lab>javac dnsclient.java
F:\Project\Lab>java dnsclient
Enter the hostname: yahoo.com
IP Address: 68.180.206.184
Result:
A B Prameela SVCT
18
Exercise No. 5
Aim: To use a tool like Wireshark to capture packets and examine them.
Procedure:
Program:
A network packet analyzer captures network packets and displays the packet data in detail, identifying
and analyzing protocols, as well as identifying the source and destination of traffic.
python
import sys
from scapy.all import *
Output:
makefile
A B Prameela SVCT
19
chksum = 0x9d6c
urgptr = 0
options = [('MSS', 1460), ('SAckOK', ''), ('Timestamp', (12576855, 2413)), ('NOP',
None), ('WScale', 7)]
###[ Raw ]###
load = ''
Result:
Thus, the program was executed successfully using Wireshark to capture and examine packets.
A B Prameela SVCT
20
Exercise No. 6a
Aim:
Algorithm:
Program: Clientarp.java
java
import java.io.*;
import java.net.*;
class Clientarp {
public static void main(String args[]) {
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
Socket clsct = new Socket("127.0.0.1", 139);
DataInputStream din = new DataInputStream(clsct.getInputStream());
DataOutputStream dout = new DataOutputStream(clsct.getOutputStream());
A B Prameela SVCT
21
Program: Serverarp.java
java
import java.io.*;
import java.net.*;
class Serverarp {
public static void main(String args[]) {
try {
ServerSocket obj = new ServerSocket(139);
Socket obj1 = obj.accept();
while (true) {
DataInputStream din = new DataInputStream(obj1.getInputStream());
DataOutputStream dout = new
DataOutputStream(obj1.getOutputStream());
Output
bash
Result:
A B Prameela SVCT
22
Exercise No. 6b
Aim:
Algorithm:
Program: clientrarp.java
java
import java.io.*;
import java.net.*;
A B Prameela SVCT
23
} catch (Exception e) {
System.out.println(e);
}
}
}
Program: serverrarp.java
java
import java.io.*;
import java.net.*;
String[] ip = {"10.0.3.186"};
String[] mac = {"D4:3D:7E:12:A3:D9"};
Output:
bash
Result:
Thus, a program in Java simulated RARP protocols.
A B Prameela SVCT
24
Exercise No. 7a
AIM:
INTRODUCTION:
Network Simulator (Version 2), widely known as NS2, is an event-driven simulation tool that has
proven useful in studying the dynamic nature of communication networks. Simulation of both wired
and wireless network functions and protocols (e.g., routing algorithms, TCP, UDP) can be performed
using NS2.
In general, NS2 provides users with a means to specify network protocols and simulate their
corresponding behaviors. Due to its flexibility and modular nature, NS2 has gained consistent
popularity in the networking research community since its inception in 1989. Over the years, several
revisions and enhancements have contributed to the growing maturity of the tool, thanks to substantial
contributions from key players in the field.
Notably, the University of California and Cornell University developed the REAL network simulator,
which serves as the foundation for NS. Since 1995, the Defense Advanced Research Projects Agency
(DARPA) has supported the development of NS through the Virtual InterNetwork Testbed (VINT)
project. Currently, the National Science Foundation (NSF) has also joined in the development efforts.
Moreover, a dedicated group of researchers and developers in the community are continuously
working to keep NS2 robust and versatile.
BASIC ARCHITECTURE :
The figure shows the basic architecture of NS2. NS2 provides users with an executable command ns,
which takes as input the name of a Tcl simulation scripting file. Users supply the name of a Tcl
simulation script (which sets up a simulation) as an argument to the ns executable command.
In most cases, a simulation trace file is created, which is used to plot graphs and/or create animations.
NS2 consists of two key languages: C++ and Object-oriented Tool Command Language (OTcl). The
C++ part defines the internal mechanisms (backend) of the simulation objects, while OTcl is used to
A B Prameela SVCT
25
set up the simulation by assembling and configuring the objects, as well as scheduling discrete events
(frontend).
The C++ and OTcl components are linked using TclCL. Mapped to a C++ object, variables in the OTcl
domain are often referred to as handles. Conceptually, a handle (e.g., n as a Node handle) is simply a
string (e.g., _o10) in the OTcl domain and does not contain any functionality. Instead, the functionality
(e.g., receiving a packet) is defined in the corresponding C++ object (e.g., of class Connector). In the
OTcl domain, a handle acts as a frontend that interacts with users and other OTcl objects. It may define
its own procedures and variables to facilitate this interaction. Note that member procedures and
variables in the OTcl domain are referred to as instance procedures (instprocs) and instance variables
(instvars), respectively. Readers are encouraged to learn C++ and OTcl languages before proceeding
further.
NS2 provides a wide range of built-in C++ objects. It is advisable to use these objects to set up a
simulation with a Tcl simulation script. However, advanced users may find these objects insufficient
and may need to develop their own C++ objects, using an OTcl configuration interface to assemble
these objects. After simulation, NS2 outputs either text-based or animation-based results. Tools such as
NAM (Network AniMator) and XGraph can be used to interpret these results graphically and
interactively. Users can extract relevant subsets of text-based data to transform it into a more
understandable presentation.
CONCEPT OVERVIEW
NS2 employs two languages because the simulator has two different types of tasks it needs to perform.
On one hand, detailed simulations of protocols require a systems programming language that can
efficiently manipulate bytes, packet headers, and implement algorithms running over large data sets.
For these tasks, runtime speed is important, while turnaround time (i.e., run simulation, find bug, fix
bug, recompile, re-run) is less critical.
On the other hand, much of network research involves slightly varying parameters or configurations,
or quickly exploring numerous scenarios. In these cases, iteration time (i.e., change the model and re-
run) is more important. Since configuration runs only once (at the beginning of the simulation), the
runtime of this part of the task is less critical. NS2 addresses both of these needs with C++ and OTcl.
tcl
tcl
Nodes:
A B Prameela SVCT
26
tcl
tcl
tcl
Link Failures:
tcl
tcl
tcl
Post-Processing Procedures:
tcl
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
A B Prameela SVCT
27
Schedule Events:
tcl
tcl
$ns run
RESULT:
Thus the study of NS was done successfully.
A B Prameela SVCT
28
Aim:
Algorithm:
Program:
tcl
#===================================
# Simulation parameters setup
#===================================
#===================================
# Initialization
#===================================
# Create a ns simulator
set ns [new Simulator]
A B Prameela SVCT
29
#===================================
# Nodes Definition
#===================================
# Create 2 nodes
set n0 [$ns node]
set n1 [$ns node]
#===================================
# Links Definition
#===================================
# Create links between nodes
$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail
$ns queue-limit $n0 $n1 10
#===================================
# Agents Definition
#===================================
# Setup a TCP connection
set tcp1 [new Agent/TCP]
$tcp1 set windowInit_ 4
$tcp1 set maxcwnd_ 4
#===================================
# Applications Definition
#===================================
# Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp1
$ns at 0.0 "$ns trace-annotate \"Sliding Window with window size 4 (normal
operation)\""
$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""
$ns at 0.11 "$ns trace-annotate \"Send Packet 0,1,2,3\""
$ns at 0.34 "$ns trace-annotate \"Receive Ack 0,1,2,3\""
$ns at 0.56 "$ns trace-annotate \"Send Packet 4,5,6,7\""
$ns at 0.79 "$ns trace-annotate \"Receive Ack 4,5,6,7\""
$ns at 0.99 "$ns trace-annotate \"Send Packet 8,9,10,11\""
$ns at 1.23 "$ns trace-annotate \"Receive Ack 8,9,10,11\""
$ns at 1.43 "$ns trace-annotate \"Send Packet 12,13,14,15\""
$ns at 1.67 "$ns trace-annotate \"Receive Ack 12,13,14,15\""
$ns at 1.88 "$ns trace-annotate \"Send Packet 16,17,18,19\""
$ns at 2.11 "$ns trace-annotate \"Receive Ack 16,17,18,19\""
$ns at 2.32 "$ns trace-annotate \"Send Packet 20,21,22,23\""
$ns at 2.56 "$ns trace-annotate \"Receive Ack 24,25,26,27\""
$ns at 2.76 "$ns trace-annotate \"Send Packet 28,29,30,31\""
A B Prameela SVCT
30
#===================================
# Termination
#===================================
# Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam sliding.nam &
exit 0
}
Steps to Execute:
bash
admin@CS03C037 ~ $ cd ..
admin@CS03C037 /home $ cd ..
admin@CS03C037 / $ ls
Cygwin.bat Cygwin.ico Project bin cygdrive dev etc home lib opt proc
tmp usr var
admin@CS03C037 / $ cd Project
admin@CS03C037 /Project $ ls
sliding.tcl
A B Prameela SVCT
31
bash
Result:
Thus, the simulation of Congestion Control Algorithms (sliding window) using NS has been
performed.
A B Prameela SVCT
32
Exercise. No: 8:
Aim:
Algorithm:
Program:
tcl
A B Prameela SVCT
33
A B Prameela SVCT
34
Result:
Thus studied the performance of TCP/UDP using Simulation Tool (NS2)
A B Prameela SVCT
35
Exercise No.9a:
Aim:
Algorithm:
A B Prameela SVCT
36
Output
The output will include the results of the simulation based on the implemented Distance Vector
Routing protocol.
A B Prameela SVCT
37
Result:
Thus performed Simulation of Distance Vector Routing algorithm.
A B Prameela SVCT
38
Aim:
Algorithm:
# Create nodes
for {set i 0} {$i < 12} {incr i} {
set n($i) [$ns node]
}
A B Prameela SVCT
39
# Schedule events
$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"
$ns at 45 "finish"
A B Prameela SVCT
40
Output:
Result:
Thus performed Simulation of Link State Routing algorithm.
A B Prameela SVCT
41
Aim:
To write a program in Java to implement the Simulation of Error Correction Code (CRC).
Algorithm:
At Sender Side:
At Receiver Side:
1. Perform modulo-2 division again. If the remainder is 0, then there are no errors.
Modulo-2 Division:
In each step, a copy of the divisor (or data) is XORed with the kkk bits of the dividend.
The result of the XOR operation (remainder) is (n−1)(n-1)(n−1) bits, which is used for the next step
after pulling down 1 extra bit to make it nnn bits long.
When there are no bits left to pull down, we have a result. The (n−1)(n-1)(n−1)-bit remainder is
appended at the sender side.
class CRC {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
A B Prameela SVCT
42
// Read Divisor
System.out.println("Enter the size of the divisor:");
n = input.nextInt();
int[] divisor = new int[n];
System.out.println("Enter the divisor, bit by bit:");
for (int i = 0; i < n; i++) {
System.out.println("Enter bit number " + (n - i) + ":");
divisor[i] = input.nextInt();
}
// Perform Division
int[] remainder = divide(data, divisor);
System.out.print("Remainder: ");
for (int i = 0; i < remainder.length - 1; i++) {
System.out.print(remainder[i]);
}
System.out.println("\nThe CRC code generated is:");
A B Prameela SVCT
43
return remainder;
}
Output
yaml
Copy code
D:\Project>javac crc.java
D:\Project>java crc
Enter the size of the data:
7
Enter the data, bit by bit:
Enter bit number 7:
1
Enter bit number 6:
0
Enter bit number 5:
0
Enter bit number 4:
1
Enter bit number 3:
1
Enter bit number 2:
0
Enter bit number 1:
1
Enter the size of the divisor:
4
Enter the divisor, bit by bit:
Enter bit number 4:
1
Enter bit number 3:
0
Enter bit number 2:
1
Enter bit number 1:
1
First data bit is: 1
Remainder: 0101
First data bit is: 0
Remainder: 1010
First data bit is: 1
Remainder: 0011
First data bit is: 0
Remainder: 0110
First data bit is: 0
Remainder: 1100
First data bit is: 1
Remainder: 1110
A B Prameela SVCT
44
Result:
Thus, a program in Java was implemented to simulate Error Correction Code (CRC).
A B Prameela SVCT