CS8581 - Networks Lab Manual
CS8581 - Networks Lab Manual
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
REGISTER NO. :
NAME :
V SEMESTER
BONAFIDE CERTIFICATE
REGISTER NUMBER
Date:
PAGE STAFF
S.NO DATE NAME OF THE PROGRAM
NO. SIGN
EX.NO.1(A) CODE SIMULATING PING COMMAND
DATE:
PROGRAM:
import java.io.*;
import java.net.*;
class pingserver
{
public static void main(String args[])
{
try
{
String str;
System.out.print(" Enter the IP Address to be Ping : ");
BufferedReader buf1=new BufferedReader(new
InputStreamReader(System.in));
String ip=buf1.readLine();
Runtime H=Runtime.getRuntime();
Process p=H.exec("ping " + ip);
I`nputStream 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());
}
}
}
Output:
Enter the IP address to the ping:192.168.0.1
Pinging 192.168.0.1: with bytes of data =32
Reply from 192.168.0.11:bytes=32 time<1ms TTL =128
Reply from 192.168.0.11:bytes=32 time<1ms TTL =128
Reply from 192.168.0.11:bytes=32 time<1ms TTL =128
Reply from 192.168.0.11:bytes=32 time<1ms TTL =128
Ping statistics for 192.168.0.1
Packets: sent=4,received=4,lost=0(0% loss),approximate round trip time in milli seconds:
Minimum=1ms,maximum=4ms,average=2ms.
EX.NO.1(B) TRACE ROUTE
DATE:
PROGRAM:
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);
//in.close();
}
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
}
Output :
Server
import java.io.*;
import java.net.*;
import java.util.*;
class Serverfile
{
public static void main(String args[])
{
Try
{
ServerSocket obj=new ServerSocket(139);
while(true)
{
Socket obj1=obj.accept();
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
FileReader f=new FileReader(str);
BufferedReader b=new BufferedReader(f);
String s;
while((s=b.readLine())!=null)
{
System.out.println(s);
dout.writeBytes(s+'\n');
}f.close();
dout.writeBytes("-1\n");
}}
catch(Exception e)
{
System.out.println(e);
}}}
Output
File content
Computer networks
jhfcgsauf
jbsdava
jbvuesagv
client
Enter the file name:
sample.txt
server
Computer networks
jhfcgsauf
jbsdava
jbvuesagv
client
Enter the new file name:
net.txt
Computer networks
jhfcgsauf
jbsdava
jbvuesagv
Destination file
Computer networks
Jhfcgsauf
Jbsdava
EX.NO: 4 DNS USING UDP SOCKETS
DATE:
PROGRAM:
UDP DNS Server
import java.io.*;
import java.net.*;
public class udpdnsserver
{
private static int indexOf(String[] array, String str)
{
str = str.trim();
for (int i=0; i < array.length; i++)
{
if (array[i].equals(str)) return i;
}
return -1;
}
public static void main(String arg[])throws IOException
{
String[] hosts = {"yahoo.com", "gmail.com","cricinfo.com", "facebook.com"};
String[] ip = {"68.180.206.184", "209.85.148.19","80.168.92.140", "69.63.189.16"};
System.out.println("Press Ctrl + C to Quit");
while (true)
{
DatagramSocket serversocket=new DatagramSocket(1362);
byte[] senddata = new byte[1021];
byte[] receivedata = new byte[1021];
DatagramPacket recvpack = new DatagramPacket(receivedata, receivedata.length);
serversocket.receive(recvpack);
String sen = new String(recvpack.getData());
InetAddress ipaddress = recvpack.getAddress();
int port = recvpack.getPort();
String capsent;
System.out.println("Request for host " + sen);
if(indexOf (hosts, sen) != -1)
capsent = ip[indexOf (hosts, sen)];
else capsent = "Host Not Found";
senddata = capsent.getBytes();
DatagramPacket pack = new DatagramPacket (senddata, senddata.length,ipaddress,port);
serversocket.send(pack);
serversocket.close();
}
}
}
/ /UDP DNS Client
import java.io.*;
import java.net.*;
public class udpdnsclient
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientsocket = new DatagramSocket();
InetAddress ipaddress;
if (args.length == 0)
ipaddress =
InetAddress.getLocalHost(); else
ipaddress = InetAddress.getByName(args[0]);
byte[] senddata = new byte[1024];
byte[] receivedata = new byte[1024];
int portaddr = 1362;
System.out.print("Enter the hostname : ");
String sentence = br.readLine();
Senddata = sentence.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,senddata.length, ipaddress,portaddr);
clientsocket.send(pack);
DatagramPacket recvpack =new DatagramPacket(receivedata,receivedata.length);
clientsocket.receive(recvpack);
String modified = new String(recvpack.getData());
System.out.println("IP Address: " + modified);
clientsocket.close();
}
}
OUTPUT
Server
javac udpdnsserver.java
java udpdnsserver
Press Ctrl + C to Quit Request for host yahoo.com
Request for host cricinfo.com
Request for host youtube.com
Client
javac udpdnsclient.java
java udpdnsclient
Enter the hostname : yahoo.com
IP Address: 68.180.206.184
java udpdnsclient
Enter the hostname : cricinfo.com
IP Address: 80.168.92.140
java udpdnsclient
Enter the hostname : youtube.com
IP Address: Host Not Found
EX.NO:5(A) CODE SIMULATING ARP PROTOCOLS.
DATE:
PROGRAM:
Client:
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));
Socket clsct=new Socket("127.0.0.1",139);
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
System.out.println("Enter the Logical address(IP):");
String str1=in.readLine();
dout.writeBytes(str1+'\n');
String str=din.readLine();
System.out.println("The Physical Address is: "+str);
clsct.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
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();
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
EX.NO:5(B) REVERSE ADDRESS RESOLUTION PROTOCOL(RARP)
DATE:
PROGRAM:
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientrarp12
{
public static void main(String args[])
{
try
{
DatagramSocket client=new DatagramSocket();
InetAddress addr=InetAddress.getByName("127.0.0.1");
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Physical address (MAC):")
String str=in.readLine(); sendbyte=str.getBytes();
DatagramPacket sender=newDatagramPacket(sendbyte,sendbyte.length,addr,1309);
client.send(sender);
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
client.receive(receiver);
String s=new String(receiver.getData());
System.out.println("The Logical Address is(IP): "+s.trim());
client.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverrarp12
{
public static void main(String args[])
{
try
{
DatagramSocket server=new DatagramSocket(1309);
while(true)
{
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
server.receive(receiver);
String str=new String(receiver.getData());
String s=str.trim();
InetAddress addr=receiver.getAddress();
int port=receiver.getPort();
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(s.equals(mac[i]))
{
sendbyte=ip[i].getBytes();
DatagramPacket sender=newDatagramPacket(sendbyte,sendbyte.length,addr,port);
server.send(sender);
break;
}
}
break;
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
I:\ex>java Serverrarp12
I:\ex>java Clientrarp12
Enter the Physical address
(MAC): 6A:08:AA:C2
The Logical Address is(IP): 165.165.80.80
EX.NO: 6 STUDY OF NETWORK SIMULATOR (NS).AND SIMULATION OF CONGESTION
CONTROL ALGORITHMS USING NS
DATE:
AIM:
To Study of Network simulator (NS).and Simulation of Congestion Control Algorithms using NS.
Wireless
Ad hoc routing, mobile IP, sensor-MACTracing, visualization and various utilitieNS(Network Simulators)
Most of the commercial simulators are GUI driven, while some network simulators are CLI driven. The network model /
configuration describes the state of the network (nodes,routers,switches, links) and the events (data transmissions, packet
error etc.). An important output of simulations are the trace files. Trace files log every packet, every event that occurred in
the simulation and are used for analysis. Network simulators can also provide other tools to facilitate visual analysis of
trends and potential trouble spots.
Most network simulators use discrete event simulation, in which a list of pending "events"is stored, and those
events are processed in order, with some events triggering future events—such as the event of the arrival of a packet at one
node triggering the event of the arrival of thatpacket at a downstream node.Simulation of networks is a very complex task.
For example, if congestion is high, then estimation of the average occupancy is challenging because of high variance.
To estimate the likelihood of a buffer overflow in a network, the time required for an accurate answer can be
extremely large. Specialized techniques such as "control variates" and "importance sampling"
have been developed to speed simulation.
There are many both free/open-source and proprietary network simulators. Examples ofnotable network simulation
software are, ordered after how often they are mentioned in research papers:
1. ns (open source)
2. OPNET (proprietary software)
3. NetSim (proprietary software)
Network simulators serve a variety of needs. Compared to the cost and time involved in setting up an entire test bed
containing multiple networked computers, routers and data links, network simulators are relatively fast and inexpensive.
They allow engineers, researchers to test scenarios that might be particularly difficult or expensive to emulate using real
hardware - for instance, simulating a scenario with several nodes or experimenting with a new protocol in the network.
Network simulators are particularly useful in allowing researchers to test new networking protocols or changes to
existing protocols in a controlled and reproducible environment. A typical network simulator encompasses a wide range of
networking technologies and can help the users to build complex networks from basic building blocks such as a variety of
nodes and links.
With the help of simulators, one can design hierarchical networks using various types of nodeslike computers, hubs,
bridges, routers, switches, links, mobile units etc.Various types of Wide Area Network (WAN) technologies like TCP, ATM,
IP etc. Local Area Network (LAN) technologies like Ethernet, token rings etc., can all be simulated with a typical simulator
and the user can test, analyze various standard results apart from devising some novel protocol or strategy for routing etc.
Network simulators are also widely used to simulate
battlefield networks in Network-centric warfare.
There are a wide variety of network simulators, ranging from the very simple to the very complex. Minimally, a
network simulator must enable a user to represent a network topology, specifying the nodes on the network, the links
between those nodes and the traffic between thenodes.More complicated systems may allow the user to specify everything
about the protocols used to handle traffic in a network. Graphical applications allow users to easily visualize the workings of
their simulated environment. Text-based applications may provide a less intuitive interface, but may permit more advanced
forms of customization.
Packet loss
when one or morepacketsof data travelling across a computer networkfail to reachtheir destination. Packet loss is
distinguished as one of the three main error types encountered in digital communications; the other two being bit errorand
spurious packets caused due to noise.
Packets can be lost in a network because they may be dropped when a queue in the network node overflows. The
amount of packet loss during the steady state is another important property of a congestion control scheme. The larger the
value of packet loss, the more difficult it is for transportlayer protocols to maintain high bandwidths, the sensitivity to loss of
individual packets, as well as to frequency and patterns of loss among longer packet sequences is strongly dependent on the
application itself.
Throughput
This is the main performance measure characteristic, and most widely used. In communicationnetworks, such
asEthernetorpacket radio, throughputor network throughputs. the average rate of successfulmessage delivery over a
communication channel. The throughput is usually measured inbitsper second (bit/s orbps), andsometimes indata packetsper
second or data packets pertime slotThis measure how soon the receiver is able to get a certain amount of data send by the
sender. It is determined as the ratio of the total data received to the end to end delay.Throughput is an important factor which
directly impacts the network performance
Delay
Delay is the time elapsed while a packet travels from one point e.g., source premise or network ingress to
destination premise or network degrees. The larger the valueof delay, the more difficult it is for transport layer protocols to
maintain highbandwidths. We will calculate end to end delay.
Queue Length
A queuing system in networks can be described as packets arriving for service, waiting for service if it is not
immediate, and if having waited for service, leaving thesystem after being served. Thus queue length is very important
characteristic to determine that how well the active queue management of the congestion control
algorithm has been working.
EX. NO:7 STUDY OF TCP/UDP PERFORMANCE USING SIMULATION TOOL
DATE:
AIM:
To study TCP/IP model using simulation tools.
PROCEDURE:
TCP/IP means Transmission Control Protocol and Internet Protocol. It is the network model used in
the current Internet architecture as well. Protocols are set of rules which govern every possible communication
over a network. These protocols describe the movement of data between the source and destination or the
internet. These protocols offer simple naming and addressing schemes.
TCP/IP that is Transmission Control Protocol and Internet Protocol was developed by Department of
Defense’s Project Research Agency (ARPA, later DARPA) as a part of a research project of network
interconnection to connect remote machines. There are four layers in TCP/IP model Hostto-
Network layer
Internet layer
Transporlayer
Application layer
Network interface layer
Internet layer:
The Internet Layer exists for routing and providing a single network interface to the upper layers. IP
provides the single network interface for the upper layers.
The Internet Protocol (IP) is a protocol that contains addressing information and some control
information that enables packets to be routed. IP has two primary responsibilities: providing connectionless,
best-effort delivery of datagrams through an internetwork; and providing fragmentation and reassembly of
datagrams to support data links.All machines on a TCP/IP network have a unique logical address, an IP
address. The Internet Layer (IP) has a complete picture of the entire network and is responsible for path
determination and packet switching.
ICMPs generate several kinds of useful messages, including Destination Unreachable, Echo Request
and Reply, Redirect, Time Exceeded, and Router Advertisement and Router Solicitation. If an ICMP message
cannot be delivered, no second one is generated. This is to avoid an endless flood of ICMP messages.
When an ICMP destination-unreachable message is sent by a router, it means that the router is unable
to send the package to its final destination. The router then discards the original packet. Destination-
unreachable messages include four basic types: network unreachable, host unreachable, protocol unreachable,
and port unreachable.
Transport layer:
It is designed to allow peer entities on the source and destination hosts to carry on a conversation. Two
protocols are defined here TCP and UDP.
Application Layer
Application layer is the top most layer of four layer TCP/IP model. Application layer defines TCP/IP application
protocols and how host programs interface with Transport layer services to use the network.
Application layer includes all the higher-level protocols like DNS (Domain Naming System), HTTP (Hypertext
Transfer Protocol), Telnet, SSH, FTP (File Transfer Protocol), TFTP (Trivial File Transfer Protocol), SNMP
(Simple Network Management Protocol), SMTP (Simple Mail Transfer Protocol) , DHCP (Dynamic Host
Configuration Protocol).
Layer Description Protocols
Application Defines TCP/IP application protocols and how host HTTP, Telnet, FTP,
programs interface with transport layer services to use the TFTP, SNMP, DNS,
network. SMTP,
X Windows,
other application
protocols
Transport Provides communication session management between host TCP, UDP, RTP
computers. Defines the level of service and status of the
connection used when transporting data.
Internet Packages data into IP datagrams, which contain source and IP, ICMP, ARP, RARP
destination address information that is used to forward the
datagrams between hosts and across networks. Performs
routing of IP datagrams.
Network Specifies details of how data is physically sent through the Ethernet, Token Ring,
interface network, including how bits are electrically signaled by FDDI, X.25, Frame Relay,
hardware
devices that interface directly with a network medium, RS-232, v.35
such as coaxial cable, optical fiber, or twisted-pair
copper wire.
EX.NO:8 DISTANCE VECTOR ROUTING
DATE:
PROGRAM:
#include<stdio.h>
#define MAX 10
struct dist_vect
{
int dist[MAX];
int from[MAX];
};
int main()
{
int adj[MAX][MAX],n,i,j,hop[10][10]={{0}},k,count;
struct dist_vect arr[10];
printf("enter the number of nodes\n");
scanf("%d",&n);
printf("enter adjacency matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++);
scanf("%d",&adj[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
arr[i].dist[j]=adj[i][j];
arr[i].from[j]=j;
}
}
count=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
if(arr[i].dist[j]>adj[i][k]+arr[k].dist[j])
{
arr[i].dist[j]=adj[i][k]+arr[k].dist[j];
arr[i].from[j]=k;
count++;
if(count==0)
hop[i][j]=1;
else
hop[i][j]=count+hop[k][j];
}
}
count=0;
}
}
for(i=0;i<n;i++)
{
printf("state value of router under %d",i+1);
printf("\n node\tvia node\tdiatance\tnumber of hopes\n");
for(j=0;j<n;j++)
{
if(i==j)
printf("\n%d\t%d\t%d\n",j+1,arr[i].from[j]+1,arr[i].dist[j]);
else
printf("\n%d\t%d\t\t%d\t\t%d\n",j+1,arr[i].from[j]+1,arr[i].dist[j],hop[i][j]+1);
}
}
}
EX.NO.9 ROUTING PROTOCOL SIMULATION TOOLS
DATE:
PROGRAM:
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the Trace file
set file1 [open out.tr w]
$ns trace-all $file1
#Open the NAM trace file
set file2 [open out.nam w]
$ns namtrace-all $file2
#Define a 'finish' procedure
proc finish {} {
global ns file1 file2
$ns flush-trace
close $file1
close $file2
exec nam out.nam &
exit 3
}
# Next line should be commented out to have the static routing
$ns rtproto DV
#Create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n4 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n1 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n2 0.3Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n4 0.3Mb 10ms DropTail
$ns duplex-link $n3 $n5 0.5Mb 10ms DropTail
$ns duplex-link $n4 $n5 0.5Mb 10ms DropTail
#Give node position (for NAM)
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right
$ns duplex-link-op $n2 $n3 orient up
$ns duplex-link-op $n1 $n4 orient up-left
$ns duplex-link-op $n3 $n5 orient left-up
$ns duplex-link-op $n4 $n5 orient right-up
#Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n5 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
$ns rtmodel-at 1.0 down $n1 $n4
$ns rtmodel-at 4.5 up $n1 $n4
$ns at 0.1 "$ftp start"
$ns at 6.0 "finish"
$ns run
EX.NO: 10 ERROR CORRECTION CODE USING CRC
DATE:
PROGRAM
import java.io.*;
class CRC
{
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];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in divisor : ");
divisor_bits=Integer.parseInt(br.readLine());
divisor=new int[divisor_bits];
System.out.println("Enter Divisor bits : ");
for(int i=0; i<divisor_bits; i++)
divisor[i]=Integer.parseInt(br.readLine());
System.out.print("Data bits are : ");
for(int i=0; i< data_bits; i++)
System.out.print(data[i]);
System.out.println();
System.out.print("divisor bits are : ");
for(int i=0; i< divisor_bits; i++)
System.out.print(divisor[i]);
System.out.println();
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];
System.out.print("Dividend (after appending 0's) are : ");
for(int i=0; i< div.length; i++)
System.out.print(div[i]);
System.out.println();
for(int j=0; j<div.length; j++){
rem[j] = div[j];
}
rem=divide(div, divisor, rem);
CRC code :
101100111
Enter CRC code of 9 bits :
1
0
1
1
0
0
1
0
1
crc bits are : 101100101
Error
THANK YOU. .... )
Press any key to continue...