Cs3591 Computer Networks Lab Record
Cs3591 Computer Networks Lab Record
Student Name:_____________________________
Year / Semester: III / V
THE KAVERY ENGINEERING COLLEGE Page :
Staff Sign
Awarded
Page No.
Marks
Ex. Date Name of the Experiment
No.
EX. NO.:1(a)
STUDY OF NETWORKING COMMANDS
AIM
To study the basic networking commands.
COMMANDS:
C:\>arp –a:
ARP is short form of address resolution protocol, It will show the IP address of
your computer along with the IP address and MAC address of your router.
C:\>hostname:
This is the simplest of all TCP/IP commands. It simply displays the
name of your computer.
C:\>ipconfig:
The ipconfig command displays information about the host (the computer your
sitting at)computer TCP/IP configuration.
C:\>ipconfig /all:
This command displays detailed configuration information about your
TCP/IP connection including Router, Gateway, DNS, DHCP, and type of Ethernet
adapter in your system.
C:\>Ipconfig /renew:
Using this command will renew all your IP addresses that you are currently
(leasing) borrowing from the DHCP server. This command is a quick problem solver
if you are having connection issues, but does not work if you have been configured
with a static IP address.
C:\>Ipconifg /release:
This command allows you to drop the IP lease from the DHCP server.
C:\>ipconfig /flushdns:
This command is only needed if you‟re having trouble with your networks
DNS configuration. The best time to use this command is after network configuration
frustration sets in, and you really need the computer to reply with flushed.
C:\>nbtstat –a:
This command helps solve problems with NetBIOS name resolution. (Nbt
stands for NetBIOS over TCP/IP)
THE KAVERY ENGINEERING COLLEGE Page :
C:\>netdiag:
Netdiag is a network testing utility that performs a variety of network
diagnostic tests, allowing you to pinpoint problems in your network. Netdiag isn‟t
installed by default, but can be installed from the Windows XP CD after saying no to
the install. Navigate to the CD ROM drive letter and open the support\tools folder on
the XP CD and click the setup.exe icon in the support\tools folder.
C:\>netstat:
Netstat displays a variety of statistics about a computers active TCP/IP
connections. This tool is most useful when you‟re having trouble with TCP/IP
applications such as HTTP, and FTP.
C:\>nslookup:
Nslookup is used for diagnosing DNS problems. If you can access a resource by
specifying an IP address but not it‟s DNS you have a DNS problem.
C:\>pathping:
Pathping is unique to Window‟s, and is basically a combination of the Ping
and Tracert commands. Pathping traces the route to the destination address then
launches a 25 second test of each router along the way, gathering statistics on the rate
of data loss along each hop.
C:\>ping:
Ping is the most basic TCP/IP command, and it‟s the same as placing a phone
call to your best friend. You pick up your telephone and dial a number, expecting
your best friend to reply with “Hello” on the other end. Computers make phone calls
to each other over a network by using a Ping command. The Ping commands main
purpose is to place a phone call to another computer on the network, and request an
answer. Ping has 2 options it can use to place a phone call to another computer on the
network. It can use the computers name or IP address.
C:\>route:
The route command displays the computers routing table. A typical computer,
with a single network interface, connected to a LAN, with a router is fairly simple and
generally doesn‟t pose any network problems. But if you‟re having trouble accessing
other computers on your network, you can use the route command to make sure the
entries in the routing table are correct.
C:\>tracert: The tracert command displays a list of all the routers that a packet has to
go through to get from the computer where tracert is run to any other computer on
the internet.
To get the network packets from all network interfaces, run the following command,
THE KAVERY ENGINEERING COLLEGE Page :
To read an already created, old tcpdump file, use the following command,
To get the packets for whole network, execute the following command from terminal
To check all the packets used based on the protocol, run the following command
RESULT:
Thus the above list of primitive has been studied.
THE KAVERY ENGINEERING COLLEGE Page :
EX. NO.:1(b)
SIMULATION OF PING COMMAND
AIM:
To test the communication between hosts at IP level using Ping command.
Algorithm:
Program:
// PingServer.java : Simple Ping Program
import java.io.*;
import java.net.*;
class PingServer
{
public static void main(String args[])
{
try
{
String str;
System.out.print("Enter IP address/domain name: ");
BufferedReader buf1=new BufferedReader(new InputStreamReader(System.in));
String ip = buf1.readLine();
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("ping " + ip);
InputStream in = p.getInputStream();
BufferedReader buf2 = new BufferedReader(new InputStreamReader(in));
while((str=buf2.readLine()) != null)
{
System.out.println(" " + str);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
THE KAVERY ENGINEERING COLLEGE Page :
Output:
$ javac PingServer.java
$ java PingServer
Enter IP address/domain name: gmail.com
Result:
Thus using Ping command, connective and communicative status is determined.
THE KAVERY ENGINEERING COLLEGE Page :
EX.NO.:1(C)
SIMULATION OF TRACEROUTE COMMAND
Aim:
To trace the path traversed by a packet from host to destination using
Traceroute command.
Algorithm:
Program:
// 4b - TraceServer.java : Traceroute Program
import java.io.*;
import java.net.*;
class TraceServer
{
public static void main(String args[])
{
try
{
String str;
System.out.print("Enter domain name : ");
BufferedReader buf1=new BufferedReader(new InputStreamReader(System.in));
String ip = buf1.readLine();
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("tracert " + ip);
InputStream in = p.getInputStream();
BufferedReader buf2 = new BufferedReader(new InputStreamReader(in));
while((str=buf2.readLine()) != null)
{
System.out.println(" " + str);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
THE KAVERY ENGINEERING COLLEGE Page :
Output:
$ javac TraceServer.java
$ java TraceServer
Enter domain name: yahoo.com
Trace complete.
Result:
Thus using traceroute command, path traversed by the packet is determined.
THE KAVERY ENGINEERING COLLEGE Page :
EX. NO.: 2
CREATE SOCKET FOR HTTP FOR WEB PAGE UPLOAD AND DOWNLOAD
Aim:
To download a web page using java URL method.
Algorithm:
Program:
// DownloadPage.java
import java.io.*;
import java.net.*;
class MyDownload
{
public void Download() throws Exception
{
try
{
String WebPage, MyPage;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// File Instance
System.out.print("Enter filename to store : ");
MyPage = br.readLine();
File Out = new File(MyPage);
FileOutputStream FOS = new FileOutputStream(Out);
catch (Exception E)
{
System.out.println(E);
}
}
}
class DownloadPage
{
public static void main(String args[]) throws Exception
{
String Choice;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
MyDownload MDP = new MyDownload();
MDP.Download();
System.out.println("Download complete. View the file");
}
}
THE KAVERY ENGINEERING COLLEGE Page :
Output
$ javac DownloadPage.java
$ java DownloadPage
Enter the URL :
http://www.google.co.in
Enter filename to store :
mypage.html Download complete. View
the file
Result:
Thus using java URL methods, a webpage is downloaded.
THE KAVERY ENGINEERING COLLEGE Page :
EX. NO.:3(a)
Socket Program For ECHO
AIM
To write a socket program for implementation of echo.
ALGORITHM:
CLIENT SIDE
1. Start the program.
2. Create a socket which binds the Ip address of server and the port address to acquire
service.
3. After establishing connection send a data to server.
4. Receive and print the same data from server.
5. Close the socket.
6. End the program.
SERVER SIDE
1. Start the program.
2. Create a server socket to activate the port address.
3. Create a socket for the server socket which accepts the connection.
4. After establishing connection receive the data from client.
5. Print and send the same data to client.
6. Close the socket.
7. End the program.
THE KAVERY ENGINEERING COLLEGE Page :
// eclient.java
import java.io.*;
import java.net.*;
public class eclient
{
String line;
DataInputStream is,is1;
PrintStream os;
try
{
c=new Socket("localhost",8080);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());
do
{
System.out.println("client");
line=is.readLine(); os.println(line);
if(!line.equals("exit"))
System.out.println("server:"+is1.readLine());
}while(!line.equals("exit"));
}
catch(IOException e)
{
System.out.println("socket closed");
}
}
}
THE KAVERY ENGINEERING COLLEGE Page 15 of 65
import java.io.*;
import java.net.*;
import java.lang.*;
public class eserver
{
public static void main(String args[])throws IOException
{
ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try
{
s=new ServerSocket(8080);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
c=s.accept();
is=new DataInputStream(c.getInputStream());
ps=new PrintStream(c.getOutputStream());
while(true)
{
line=is.readLine();
System.out.println("msg received and sent back to client");
ps.println(line);
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
THE KAVERY ENGINEERING COLLEGE Page :
OUTPUT
CLIENT:
CONNECTION ESTABLISHED
Enter the data SRM
Client received SRM
SERVER:
CONNECTION ACCEPTED
Server received SRM
RESULT:
Thus the program for simulation of echo server was written & executed
THE KAVERY ENGINEERING COLLEGE Page :
EX. NO.:3(b)
TCP Chat Server/Client
Aim
To implement a chat server and client in java using TCP sockets.
Algorithm:
Server
Client
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(5555);
System.out.print("\nServer started\n");
Socket Clt = Srv.accept(); System.out.println("Client connected");
toClient = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())), true);
fromClient = new BufferedReader(new InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
String CltMsg, SrvMsg;
while(true)
{
CltMsg= fromClient.readLine();
if(CltMsg.equals("end"))
break;
else
{
System.out.println("\nServer <<< " + 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());
}
}}
THE KAVERY ENGINEERING COLLEGE Page :
import java.io.*;
import java.net.*;
class tcpchatclient
{
public static void main(String args[])throws Exception
{
Socket Clt;
PrintWriter toServer;
BufferedReader fromUser, fromServer;
try
{
if (args.length > 1)
{
System.out.println("Usage: java hostipaddr");
System.exit(-1);
}
if (args.length == 0)
Clt = new Socket(InetAddress.getLocalHost(),5555);
else
Clt = new Socket(InetAddress.getByName(args[0]),5555);
toServer = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())), true);
fromServer = new BufferedReader(new InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
String CltMsg, SrvMsg;
System.out.println("Type \"end\" to Quit");
while (true)
{
System.out.print("\nMessage 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());
}
}
}
THE KAVERY ENGINEERING COLLEGE Page :
Output
Server Console
$ javac tcpchatserver.java
$ java tcpchatserver
Server started
Client connected
Server <<< hi
Message to Client : hello
Client Disconnected
Client Console
$ javac tcpchatclient.java
$ java tcpchatclient
Type "end" to Quit
Message to Server : hi
Client <<< hello
Result
Thus both the client and server exchange data using TCP socket programming.
THE KAVERY ENGINEERING COLLEGE Page :
EX. NO.:3(c)
Aim
To implement a file server and client in java using TCP sockets.
Algorithm
Server
Client
Program:
FileServer .java
import java.io.*;
import java.net.*;
if(args.length == 0)
dir = new File("./");
else
dir = new File(args[0]);
if(!dir.exists() || !dir.isDirectory())
{
System.out.println("Directory does not exist");
System.exit(-1);
}
try
{
ss = new ServerSocket(3210);
conn = ss.accept();
br = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
pw = new PrintWriter(conn.getOutputStream());
break;
pw.println(line);
}
System.out.println("File Transferred\n");
}
pw.flush();
pw.close();
}
catch(Exception e)
{
System.out.println("Error: " + e);
}
}
}
Program:
FileClient.java
import java.io.*;
import java.net.*;
class FileClient
{
public static void main(String args[])throws Exception
{
try
{
Socket Clt;
if (args.length == 0)
Clt = new Socket(InetAddress.getLocalHost(),3210);
else
Clt = new Socket(InetAddress.getByName(args[0]),3210);
System.out.print("\nFilename : ");
CltMsg = br.readLine();
THE KAVERY ENGINEERING COLLEGE Page :
OS.println(CltMsg);
Output
Server Console
$ javac FileServer.java
$ java FileServer
File Transferred
Client Console
$ javac FileClient.java
$ java FileClient
Filename : hello.java
import java.io.*;
class hello
{
public static void main(String args[])
{
System.out.println("hello");
}
}
Result:
Thus file contents was sent from server to client using TCP socket programming.
THE KAVERY ENGINEERING COLLEGE Page :
EX.NO.: 4
UDP DNS Server/Client
Aim
To implement a DNS server and client in java using UDP sockets.
Algorithm
Server
Client
Program:
udpdnsserver .java
import java.io.*;
import java.net.*;
while (true)
{
DatagramSocket serversocket=new DatagramSocket(1362);
byte[] senddata = new byte[1021];
byte[] receivedata = new byte[1021];
serversocket.close();
}
}
}
Program:
udpdnsclient.java
import java.io.*;
import java.net.*;
Output
Server Console
$ javac udpdnsserver.java
$ java
udpdnsserver
Press Ctrl + C to
Quit Request for
host yahoo.com
Client Console
$ javac udpdnsclient.java
$ java udpdnsclient
Enter the hostname : yahoo.com
IP Address: 68.180.206.184
Result:
Thus domain name requests by the client are resolved into their respective logical
address using lookup method.
THE KAVERY ENGINEERING COLLEGE Page :
EX. NO.:5(a)
ARP Client/Server
Aim:
To know the physical address of a host when its logical address is known using
ARP protocol.
Algorithm:
Target/Server
1. Create a server socket.
2. Accept client connection.
3. Read IP address from the client request
4. Check its configuration file and compare with its logical address.
5. If there is a match, send the host physical address.
6. Stop
Client
1.Create a socket.
2.Send IP address to the target machine
3.Receive target's response
4.If it is a MAC address then display it and go to step 6
5.Display "Host not found"
6.Stop
THE KAVERY ENGINEERING COLLEGE Page :
Program:
Client: Clientarp.java
import java.io.*;
import java.net.*;
import java.util.*;
class Clientarp
{
public static void main(String args[])
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverarp
{
public static void main(String args[])
{
try
{
ServerSocket obj=new ServerSocket(139);
Socket obj1=obj.accept();
THE KAVERY ENGINEERING COLLEGE Page :
while(true)
{
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<ip.length;i++)
{
if(str.equals(ip[i]))
{
dout.writeBytes(mac[i]+'\n');
break;
}
}
obj.close();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
E:\networks>java Serverarp
E:\networks>java Clientarp
Enter the Logical address(IP):
165.165.80.80
The Physical Address is: 6A:08:AA:C2
Result:
Thus using ARP protocol, server’s MAC address is obtained.
THE KAVERY ENGINEERING COLLEGE Page :
EX. NO.:5(b)
RARP Client/Server
Aim:
To know the logical address of a host when its physical address is known using RARP
protocol.
Algorithm:
Target/Server
1. Create a server socket.
2. Accept client connection.
3. Read MAC address from the client request
4. Check its configuration file and compare with its physical address.
5. If there is a match, send the host logical address.
6. Stop
Client
1. Create a socket.
2. Send physical address to the target machine
3. Receive target's response
4. If it is a IP address then display it and go to step 6
5. Display "Host not found"
6. Stop
THE KAVERY ENGINEERING COLLEGE Page :
Program:
Client: Clientrarp.java
import java.io.*;
import java.net.*;
import java.util.*;
class Clientrarp
{
public static void main(String args[])
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Socket clsct=new Socket("127.0.0.1",139);
Server: Serverrarp.java
import java.io.*;
import java.net.*;
import java.util.*;
class Serverrarp
{
public static void main(String args[])
{
try
{
ServerSocket obj=new ServerSocket(139);
Socket obj1=obj.accept();
THE KAVERY ENGINEERING COLLEGE Page :
while(true)
{
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<mac.length;i++)
{
if(str.equals(mac[i]))
{
dout.writeBytes(ip[i]+'\n');
break;
}
}
obj.close();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
E:\networks>java Serverrarp
E:\networks>java Clientrarp
Enter the Physical Address (MAC):
6A:08:AA:C2
The is Logical address(IP): 165.165.80.80
Result:
Thus using RARP protocol, IP address of the server is obtained.
THE KAVERY ENGINEERING COLLEGE Page :
EX. NO.:6(a)
Study of Network Simulator (NS)
Introduction
Basic Architecture
Figure 6.1 shows the basic architecture of NS2. NS2 provides users with
anexecutable command ns
THE KAVERY ENGINEERING COLLEGE Page :
which take on input argument, the name of a Tcl simulation scripting file. Users
are feeding the name of a Tcl simulation script (which sets up a simulation) as an input
argument of an NS2 executable command ns.
In most cases, a simulation trace file is created, and is used to plot graph
and/or to create animation. NS2 consists of two key languages: C++ and Object-
oriented Tool Command Language (OTcl). While the C++ defines the internal
mechanism (i.e., a backend) of the simulation objects, the OTcl sets up simulation by
assembling and configuring the objects as well as scheduling discrete events (i.e., a
frontend).
The C++ and the OTcl are linked together using TclCL. Mapped to a C++ object,
variables in the OTcl domains are sometimes referred to as handles. Conceptually, a
handle (e.g., n as a Node handle) is just 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 mapped C++ object (e.g., of class Connector). In the OTcl domain, a
handle acts as a frontend which interacts with users and other OTcl objects. It may
defines its own procedures and variables to facilitate the interaction. Note that the
member procedures and variables in the OTcl domain are called instance procedures
(instprocs) and instance variables (instvars), respectively. Before proceeding further, the
readers are encouraged to learn C++ and OTcl languages. We refer the readers to [14] for
the detail of C++, while a brief tutorial of Tcl and OTcl tutorial are given in Appendices
A.1 and A.2, respectively.
NS2 provides a large number of built-in C++ objects. It is advisable to use these
C++ objects to set up a simulation using a Tcl simulation script. However, advance users
may find these objects insufficient. They need to develop their own C++ objects, and use
a OTcl configuration interface to put together these objects. After simulation, NS2
outputs either text-based or animation-based simulation results. To interpret these results
graphically and interactively, tools such as NAM (Network AniMator) and XGraph are
used. To analyze a particular behavior of the network, users can extract a relevant subset
of text-based data and transform it to a more conceivable presentation.
THE KAVERY ENGINEERING COLLEGE Page :
CONCEPT OVERVIEW
NS uses two languages because simulator has two different kinds of things it needs to do.
On one hand, a detailed simulation of protocols requires a systems programming language
which can efficiently
manipulate bytes, packet headers, and implement algorithms that run over large
data sets. For these tasks run-time speed is important and turn-around time (run
simulation, find bug, fix bug, recompile, re- run) is less important. On the other hand, a
large part of network research involves slightly varying parameters or configurations, or
quickly exploring a number of scenarios.
In these cases, iteration time (change the model and re-run) is more important.
Since configuration runs once (at the beginning of the simulation), run-time of this part
of the task is less important. NS meets both of these needs with two languages, C++ and
OTcl.
SIMULATOR INITIALIZATION
The scheduler runs by selecting the next earliest event, executing it to completion
and returning to execute the next event. Unit of time used by scheduler is seconds.
Presently, the simulator is single- threaded and only one event in execution at any
given time. If more than one event are scheduled to execute at the same time, their
THE KAVERY ENGINEERING COLLEGE Page :
NODE BASICS
The basic primitive for creating a node is set ns [new Simulator] $ns node
The instance procedure node constructs a node out of simpler classifier objects
(Section 5.4). The Node itself is a standalone class in OTcl. However, most of the
components of the node are themselves
TclObjects. The typical structure of a (unicast) node is as shown in Figure 5.1. This
simple structure consists of two TclObjects: an address classifer (classifer_) and a port
classifier (dmux_). The function of these classifiers is to distribute incoming packets to
the correct agent or outgoing link.
All nodes contain at least the following components:
agent)
Create connections between agents (ex. make connection between a "tcp" and "sink")
THE KAVERY ENGINEERING COLLEGE Page :
Most of member functions are for simulation setup (referred to as plumbing functions in
the Overview section) and scheduling, however some of them are for the NAM display.
The "Simulator" object member function implementations are located in the
"ns-2/tcl/lib/ns-lib.tcl"file.
$ns color fid color: is to set color of the packets for a flow specified by the flow id (fid).
This member function of "Simulator" object is for the NAM display, and has no effect on
the actual simulation.
$ns namtrace-all file-descriptor: This member function tells the simulator to record
simulation traces in NAM input format. It also gives the file name that the trace will be
written to later by the command $ns flush-trace. Similarly, the member function trace-all is
for recording the simulation trace in a general
format.
Procfinish {}: is called after this simulation is over by the command $ns at 5.0
"finish". In this function, post-simulation processes are specified.
setn0 [$ns node]: The member function node creates a node. A node in NS is
compound object made of address and port classifiers (described in a later section).
Users can create a node by separately creating an address and a port classifier objects
and connecting them together. However, this member function of Simulator object
makes the job easier. To see how a node is created, look at the files: "ns-
2/tcl/libs/ns-lib.tcl"andns-2/tcl/libs/ns-node.tcl".
$ns duplex-link node1 node2 bandwidth delay queue-type: creates two simplex
links of specified bandwidth and delay, and connects the two specified nodes. In NS, the
output queue of a node isimplemented as a part of a link; therefore users should specify
the queue-type when creating links. In the above simulation script, DropTail queue is
used. If the reader wants to use a RED queue, simply replace the word DropTail with
RED. The NS implementation of a link is shown in a later section. Like a node, a link is
THE KAVERY ENGINEERING COLLEGE Page :
a compound object, and users can create its sub-objects and connect them and the
nodes. Link source codes can be found in "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-
link.tcl" files. One thing to note is that you can insert error modules in a link component
to simulate a lossy link (actually users can make and insert any network objects). Refer
to the NS documentation to find out how to do this.
$ns queue-limit node1 node2 number: This line sets the queue limit of the two
simplex links that connect node1 and node2 to the number specified. At this point, the
authors do not know how many of these kinds of member functions of Simulator objects
are available and what they are. Please take a look at "ns-2/tcl/libs/ns-lib.tcl" and "ns-
2/tcl/libs/ns-link.tcl", or NS.
$ns duplex-link-op node1 node2 ...: The next couple of lines are used for the
NAM display. To see the effects of these lines, users can comment these lines out and try
the simulation.
Now that the basic network setup is done, the next thing to do is to setup traffic agents
such as TCP and UDP, traffic sources such as FTP and CBR, and attach them to nodes
and agents respectively.
settcp [new Agent/TCP]: This line shows how to create a TCP agent. But in
general, users can create any agent or traffic sources in this way. Agents and traffic
sources are in fact basic objects (not compound objects), mostly implemented in C++
and linked to OTcl. Therefore, there are no specific Simulator object member
functions that create these object instances.
To create agents or traffic sources, a user should know the class names these objects
(Agent/TCP,
$ns attach-agent node agent: The attach-agent member function attaches an agent
THE KAVERY ENGINEERING COLLEGE Page :
object created to a node object. Actually, what this function does is call the attach
member function of specified node, which attaches the given agent to itself.
Therefore, a user can do the same thing by, for example, $n0 attach $tcp. Similarly,
each agent object has a member function.
$ns connect agent1 agent2: After two agents that will communicate with each
other are created, the next thing is to establish a logical network connection between
them. This line establishes a network connection by setting the destination address to
each others' network and port address pair.
Assuming that all the network configuration is done, the next thing to do is write a
simulation scenario (i.e. simulation scheduling). The Simulator object has many
scheduling member functions. However, the one that is mostly used is the following:
$ns at time "string": This member function of a Simulator object makes the
scheduler (scheduler_ is the variable that points the scheduler object created by [new
Scheduler] command at the beginning of the script) to schedule the execution of the
specified string at given simulation time.
http://nsnam.isi.edu/nsnam/index.php/Downloading_and_installing_ns-2
After downloading, copy of ns-allinone-2.34.tar.gz from above link, the following steps
executed:
% ./install
Once following message comes:
Nam has been installed successfully.
Ns-allinone package has been installed successfully.
THE KAVERY ENGINEERING COLLEGE Page :
(Note: In case there is a failure, as it happens with me, you first need to check what Linux
platform you
are using. In my case it is Ubuntu-9.10 version. And in such case you need to provide the
UNIX command
and make some modification in „configuration‟ file as suggested in following link:
http://lotti.netsons.org/2009/12/install-ns-allinone-2-34-on-ubuntu-9-10/
Then again type following command: (under directory: /ns-allinone-2.34)
% ./install
This hopefully will complete successful installation and he below message will come.
Nam has been installed successfully.
Ns-allinone package has been installed successfully.
EX. NO.:6(b)
PROGRAM
Agent/TCP
set_nam_tracevar_true
set tcp [new
Agent/TCP]
$tcp set window 1
$tcp set maxcwnd 1
$tcp set fid 1
OUTPUT
CONCLUSION
Thus tcl programs were executed to simulate the performance of stop wait protocol
using ns2.
THE KAVERY ENGINEERING COLLEGE Page :
Aim
To study the performance of a TCP network with droptail queue mechanism
on the gateway
Algorithm
1. Create a simulator object
2. Define different flows for data flows
3. Trace all events in a nam file and text file
4. Create source nodes (s1, s2, s3), gateway (G) and receiver (r)
5. Describe their layout topology
6. Specify the link between nodes
7. Define the queue size between nodes G and r as 5
8. Monitor queue on all links vertically 90°
9. Create TCP agents tcp1, tcp2, tcp3 and attach it to nodes s1, s2 and s3
respectively
10. Create three TCP sinks and attach it to node r
11. Connect traffic sources to the sink
12. Create FTP agents ftp1, ftp2, ftp3 and attach it to tcp1, tcp2 and tcp3
respectively
13. Label the nodes at start time
14. Schedule ftp1, ftp2, ftp3 to start at 0.1 and stop at 5.0 seconds
15. Call finish procedure at 5.25 seconds
16. Run the simulation
17. Execute NAM on the trace file
18. Observe the simulated events on the NAM editor and packet flow on link G to
r
19. View the trace file and analyse the events
THE KAVERY ENGINEERING COLLEGE Page :
Program
#Create a simulator
object set ns [new
Simulator]
#G acts as a
gateway set G [$ns
node]
#r acts as a receiver
set r [$ns node]
#Define the queue size for the link between node G and r
$ns queue-limit $G $r 5
#Define a 'finish'
procedure proc finish {} {
global ns
$ns flush-trace
puts "running
nam..."
exec nam -a droptail-queue-out.nam &
exit 0
}
Output
$ ns TCP.tcl
Result
Thus the behaviour of TCP was observed and the basic terminologies of
TCP transmission were understood.
THE KAVERY ENGINEERING COLLEGE Page :
Aim
To study the performance of UDP by simulating a simple network
Algorithm
1. Create a simulator object
2. Define different color for data flows
3. Trace all events in a nam file.
4. Create four nodes n0, n1, n2 and n3
5. Describe their layout topology
6. Specify the link capacity between nodes
7. Monitor queue on the link n2 to n3 vertically 90°
8. Create a UDP agents udp0, udp1 and attach it to nodes n0 and n1 respectively
9. Create a CBR traffic cbr0, cbr1 and attach it to udp0 and udp1 respectively
10. Create a traffic sink and attach it to node n3
11. Connect sources to the sink
12. Label the nodes
13. Schedule cbr0 to start at 0.5 and stop at 4.5 seconds
14. Schedule cbr1 to start at 1.0 and stop at 4.0 seconds
15. Call finish procedure at 5.0 seconds
16. Run the simulation
17. Execute NAM on the trace file
18. Observe simulated events on the NAM and packet flow on link n2 to n3
19. Stop
THE KAVERY ENGINEERING COLLEGE Page :
Program
#Create a simulator
object set ns [new
Simulator]
#Create four
nodes set n0 [$ns
node] set n1 [$ns
node] set n2 [$ns
node] set n3 [$ns
node]
#Define finish
procedure proc finish
{} {
global ns nf
$ns flush-trace
Output
$ ns UDP.tcl
Result
Thus the behaviour of UDP was observed and the basic terminologies of UDP
transmission were understood.
THE KAVERY ENGINEERING COLLEGE Page :
EX.NO.:8(a)
OBJECTIVE
Acquire tcl scripting skills to simulate link state routing protocol using NS2
simulator.
PROGRAM
$ns namtrace-all
$nf proc finish { } { global ns nr nf
$ns flush-trace close $nf
close $nr
exec nam thro.nam &
exit 0
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }
OUTPUT
CONCLUSION
Thus tcl programs were executed to simulate the performance of link state routing protocol
using ns2
THE KAVERY ENGINEERING COLLEGE Page :
Exp# 8b
SIMULATION OF DISTANCE VECTOR ROUTING PROTOCOL USING
NS2
OBJECTIVE
Acquire tcl scripting skills to simulate distance vector routing protocol using NS2
simulator.
PROGRAM
set ns [new Simulator]
set nf [open out.nam w]
$ns namtrace-all $nf set tr [open out.tr w]
$ns trace-all $tr
proc finish {}
{
global nf ns tr
$ns flush-trace close $tr
exec nam out.nam &
exit 0
}
set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]
$ns rtproto DV
$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"
$ns at 5.0 "finish"
$ns run
OUTPUT:
CONCLUSION
Thus tcl programs were executed to simulate the performance of distance vector routing
protocol using ns2.
THE KAVERY ENGINEERING COLLEGE Page :
EX. NO.: 9
OBJECTIVE
To implement error correction Using CRC( Cyclic redundancy check)
ALGORITHM
PROGRAM
import java.io.*;
import java.util.*;
class crc_gen
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int[] data;
int[] div;
int[] divisor;
int[] rem;
int[] crc;
int data_bits, divisor_bits, tot_length;
System.out.println("Enter number of data bits : ");
data_bits=Integer.parseInt(br.readLine());
data=new int[data_bits];
divisor=new int[divisor_bits];
*/ tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
/*------------------ CRC GENERATION-----------------------*/
for(int i=0;i<data.length;i++)
div[i]=data[i];
for(int i=0;i<div.length;i++)
//append dividend and ramainder
{
crc[i]=(div[i]^rem[i]);
}
THE KAVERY ENGINEERING COLLEGE Page :
System.out.println();
System.out.println("CRC code : ");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);
/*-------------------ERROR DETECTION---------------------*/
System.out.println();
System.out.println("Enter CRC code of "+tot_length+" bits : ");
for(int i=0; i<crc.length; i++)
crc[i]=Integer.parseInt(br.readLine());
/*
System.out.print("crc bits are : ");
for(int i=0; i< crc.length; i++) System.out.print(crc[i]);
System.out.println();
*/
{
int cur=0;
while(true)
{
for(int i=0;i<divisor.length;i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);
if((rem.length-cur)<divisor.length)
break;
}
return rem;
}
}
OUTPUT:
THE KAVERY ENGINEERING COLLEGE Page :
CONCLUSION
Thus the java program to simulate error correction using CRC was executed successfully