0% found this document useful (0 votes)
27 views50 pages

Printout For Record - CS3591 CN Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views50 pages

Printout For Record - CS3591 CN Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

EX.

NO:1 Learn to use commands like tcpdump, netstat, ifconfig, nslookup and trace
route.

Capture ping and trace route PDUs using a network protocol analyzer and examine.

PRE LAB DISCUSSION:

Tcpdump:

The tcpdump utility allows you to capture packets that flow within your network to assist in
network troubleshooting. The following are several examples of using tcpdump with different
options.
Traffic is captured based on a specified filter.

Netstat:

Netstat is a common command line TCP/IP networking available in most versions of Windows,
Linux, UNIX and other operating systems.
Netstat provides information and statistics about protocols in use and current TCP/IP
network connections.

Ipconfig:

ipconfig is a console application designed to run from the Windows command prompt.
This utility allows you to get the IP address information of a Windows computer.
From the command prompt, type ipconfig to run the utility with default options. The output
of the default command contains the IP address, network mask, and gateway for all
physical and virtual network adapter.

Nslookup:

The nslookup (which stands for name server lookup) command is a network utility program used
to obtain information about internet servers. It finds name server information for domains by
querying the Domain Name System.

Trace route:

Trace route is a network diagnostic tool used to track the pathway taken by a packet on an IP
network from source to destination. Trace route also records the time taken for each hop the
packet makes during its route to the destination
Commands:

1. Tcpdump:
# tcpdump src host # tcpdump
dst host
# tcpdump src port ftp # tcpdump
dst port http

2. Netstat
#netstat

3. ipconfig
#ipconfig

4. nslookup
#nslookup

5. Trace route:
#tracert google.com

6. Ping:
# ping172.16.6.2
OUTPUT:
Ex.No: 2 Write a HTTP web client program to download a web page using TCP sockets
PROGRAM
Client
import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage; import
java.io.ByteArrayOutputStream; import
java.io.File;
import java.io.IOException; import
javax.imageio.ImageIO;
public class Client{
public static void main(String args[]) throws Exception
{
Socket soc;
BufferedImage img = null;
soc=new
Socket("localhost",4000);
System.out.println("Client is running.
");
try {
System.out.println("Reading image from disk. ");
img = ImageIO.read(new File("digital_image_processing.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
baos.flush();
byte[] bytes = baos.toByteArray(); baos.close();
System.out.println("Sending image to server.");
OutputStream out = soc.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(bytes.length);
dos.write(bytes, 0, bytes.length);
System.out.println("Image sent to server. ");
dos.close();
out.close();}
catch (Exception e){
System.out.println("Exception: " + e.getMessage());
soc.close();}
soc.close();}}
Server
import java.net.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
class Server
{
public static void main(String args[]) throws Exception{
ServerSocket server=null;
Socket socket;
server=new ServerSocket(4000);
System.out.println("Server Waiting for image");
socket=server.accept(); System.out.println("Client connected.");
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = dis.readInt();
System.out.println("Image Size: " + len/1024 + "KB"); byte[] data = new byte[len];
dis.readFully(data);
dis.close();
in.close();
InputStream ian = new ByteArrayInputStream(data);
BufferedImage bImage = ImageIO.read(ian);
JFrame f = new JFrame("Server");
ImageIcon icon = new ImageIcon(bImage);
JLabel l = new JLabel();
l.setIcon(icon);
f.add(l);
f.pack();
f.setVisible(true);}}
OUTPUT:
Ex.No: 3 Applications using TCP sockets like: Echo client and echo server,
Chat and File Transfer

(I) ECHO CLIENT ANS ECHO SERVER

PROGRAM:

Client:

import java.net.*;

import java.io.*;

public class eclient

public static void main (String args[])

Socket c=null;

String line;

DataInputStream is,is1;

PrintStream os;

try{

c=new Socket("172.27.101.243",9000);

catch(Exception e){

System.out.println(e);

try{

System.out.println("input:");

is=new DataInputStream(System.in);

os=new PrintStream(c.getOutputStream());

is1=new DataInputStream(c.getInputStream());

while(true)

System.out.println("client:");

line=is.readLine();
os.println(line);

System.out.println("server:"+is1.readLine());

}}

catch(Exception e){

System.out.println("Socket Closed p");}}}

SERVER:

import java.net.*;

import java.io.*;

public class eserver

public static void main (String args[])

ServerSocket s=null;

String line;

DataInputStream is;

PrintStream ps;

Socket c=null;

try

s=new ServerSocket(9000);

catch(Exception e)

System.out.println(e);

try

c=s.accept();

is=new DataInputStream(c.getInputStream());
ps=new PrintStream(c.getOutputStream());

while(true)

line=is.readLine();

ps.println(line);

System.out.println(line);

catch(Exception e)

System.out.println(e);

}
OUTPUT:

CLIENT:

SERVER:
(II) CHAT

PROGRAM

CLIENT:

import java.net.*;

import java.io.*;

class client

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

Socket s=new Socket("localhost",3333);

DataInputStream din=new DataInputStream(s.getInputStream());

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str="",str2="";

while(!str.equals("stop"))

str=br.readLine();

dout.writeUTF(str);

dout.flush();

str2=din.readUTF();

System.out.println("server says:"+str2);

dout.close();

s.close();

}}
SERVER:

import java.net.*;

import java.io.*;

class server

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

ServerSocket ss=new ServerSocket(3333);

Socket s=ss.accept();

DataInputStream din=new DataInputStream(s.getInputStream());

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str="",str2="";

while(!str.equals("stop"))

str=din.readUTF();

System.out.println("client says:"+str);

str2=br.readLine();

dout.writeUTF(str2);

dout.flush();

din.close();

s.close();

ss.close();

}
OUTPUT:

CLIENT:

SERVER:
(III) FILE TRANSFER

PROGRAM

CLIENT:

import java.io.BufferedOutputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.net.InetAddress;

import java.net.Socket;

public class FileClient {

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

Socket socket = new Socket(InetAddress.getByName("localhost"), 5000);

byte[] contents = new byte[10000];

FileOutputStream fos = new FileOutputStream("e:\\Bookmarks1.html");

BufferedOutputStream bos = new BufferedOutputStream(fos);

InputStream is = socket.getInputStream();

int bytesRead = 0;

while((bytesRead=is.read(contents))!=-1)

bos.write(contents, 0, bytesRead);

bos.flush();

socket.close();

System.out.println("File saved successfully!"); }}

SERVER:

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.OutputStream;

import java.net.InetAddress;

import java.net.ServerSocket;

import java.net.Socket;
public class FileServer{

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

ServerSocket ssock = new ServerSocket(5000);

Socket socket = ssock.accept();

InetAddress IA = InetAddress.getByName("localhost");

File file = new File("e:\\ragu.txt");

FileInputStream fis = new FileInputStream(file);

BufferedInputStream bis = new BufferedInputStream(fis);

OutputStream os = socket.getOutputStream();

byte[] contents;

long fileLength = file.length();

long current = 0;

long start = System.nanoTime();

while(current!=fileLength){

int size = 10000;

if(fileLength - current >= size)

current += size;

else{

size = (int)(fileLength - current);

current = fileLength; }

contents = new byte[size];

bis.read(contents, 0, size);

os.write(contents);

System.out.print("Sending file ... "+(current*100)/fileLength+"% complete!"); }

os.flush();

socket.close();

ssock.close();

System.out.println("File sent succesfully!");

}}
OUTPUT:

CLIENT:

SERVER:
Ex.No: 4 Simulation of DNS using UDP Sockets

PROGRAM

import java.net.*;

import java.io.*;

import java.util.*;

public class dns

public static void main (String args[])

int n;

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

do

System.out.println("\n menu:\n1.DNS 2.ReverseDns 3.EXIT\n");

System.out.println("\n enter your choice");

n=Integer.parseInt(System.console().readLine());

if(n==1)

try

System.out.println("\n enter the host name");

String hname=in.readLine();

InetAddress address;

address=InetAddress.getByName(hname);

System.out.println("host name:"+address.getHostName());

System.out.println("IP:"+address.getHostAddress());

catch(IOException ioe){
ioe.printStackTrace();

}}

if(n==2)

try

System.out.println("\nenter IP address");

String ipstr=in.readLine();

InetAddress ia=InetAddress.getByName(ipstr);

System.out.println("Hostname:"+ia.getHostName());

catch(IOException ioe)

ioe.printStackTrace();

}}

}while(!(n==3));}}
OUTPUT:
Ex.No: 5 Wireshark to capture packets and examine the packets

Wireshark v3 organizes the output into three vertically stacked window panes. Wireshark v4 uses the same
three panes, but the Packet Details pane is in the lower-left corner -- it was the middle pane in v3 -- and the
Packet Bytes pane is in the lower-right corner.
Select a frame in the Packet List pane
In the new Wireshark interface, the top pane summarizes the capture. It displays one or more frames,
along with the packet number, time, source, destination, protocol, length and info fields.

Use the protocols, source and destination addresses, and ports columns to help you decide which
frame to examine. Click on a frame to select it, and then look at the two lower panes for details.

The sample capture for these screenshots is a simple Whois query to www.iana.org. This example
generates DNS and other traffic that is handy for the explanations below

Frame content

This frame section provides Ethernet information, such as frame size, time of capture and the physical
interface on which the frame was captured.
Use this information:
Administrators can use this information to examine frame size, for example. Certain
devices might have issues accepting frames that exceed the standard size.
Troubleshooters can also verify which interface the data was captured on, ensuring
information flows through the proper connection.
Ethernet content
Next is Ethernet II content, including source and destination MAC addresses.
Depending on the frame's direction of travel, the local MAC address is either the source
or destination address, and the next network device's MAC address is the other.

Use this information:


Confirm the MAC addresses are correct to combat security issues, such as Address Resolution
Protocol poisoning or spoofing. Troubleshooters might also confirm which local interface is in use
with the MAC address.
IP content

Next is the IP section, with source and destination IP addresses and port numbers. For
most networks, the address structure is IPv4. Time-to-live information exists here, as
does fragmentation instructions. Finally, a field defines whether the packet uses TCP
or UDP at the transport layer.
Use this information:
Network technicians can verify the IP addresses are valid and expected. Remember, an address
beginning with 169.254.x.x is not valid on the network and indicates a possible Dynamic Host
Configuration Protocol problem. Techs can also confirm the source and destination IP addresses are as
expected to reduce issues of DNS poisoning or incorrect name resolution settings.

Transport content
Next is a section containing transport layer information. You should see either TCP or UDP here,
depending on the type of datagram captured. Remember, TCP uses the three-way handshake to
enumerate the data exchange, ensuring that the source device resends any lost data.
This section displays the source and destination ports, too. If the packet originates from the client
computer, the destination port is the service port number. For example, if the destination system is a
web server, the destination port number is 80 or 443 (HTTP or HTTPS). The client's port number is a
randomly generated number between 1,024 and 65,535 (this range varies by OS). Both port numbers
appear in this header.

Use this information:


Confirm the client and server use the correct service port number. If you captured packets on the
server, this is the destination port number on inbound packets. This is especially important with any
nonstandard port numbers for custom applications or odd firewall rules in place
Application content
The application layer information is at the bottom of the Packet Details pane but at the top of the
TCP/IP model. This information varies by service and protocol. For example, when using HTTP, the
pane includes instructions such as GET or the contents of the requested webpage. For capture
targeting, you see information with SMTP, Post Office Protocol 3 or Internet Message Access
Protocol. The same goes for services such as SSH, network file sharing, DNS, etc.
Use this information:
Applications are preconfigured for specific ports, so there probably isn't much room for
misconfiguration here. You could use this information to ensure the destination services are running
on the server.
The header information in the capture helps confirm that addresses, ports and other settings meet
expectations. Such captures tell what is happening on the network, not what should be happening.

Interpret payload information

The Packet Bytes pane in the lower-right corner of Wireshark displays the payload. This content can
be the end-user data security professionals worry about. Unencrypted protocols, such as HTTP, Telnet,
SMTP and others, don't protect the confidentiality of their payload, so the data is shown in this
window.

The payload information demonstrates the importance of HTTPS, SSH and


other protocols that encrypt data -- or the use of IPsec, which can encrypt
protocols that don't offer built-in encryption themselves.
Ex.No:6 Write a code simulating ARP /RARP protocols

ARP PROTOCOL:
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
RARP PROTOCOL:
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 = new
DatagramPacket(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.8
Ex.No: 7 Study of Network simulator (NS) and Simulation of Congestion Control
Algorithms using NS

Program:
include <wifi_lte/wifi_lte_rtable.h>
struct r_hist_entry *elm, *elm2;
int num_later = 1;
elm = STAILQ_FIRST(&r_hist_);
while (elm != NULL && num_later <= num_dup_acks_){
num_later;
elm = STAILQ_NEXT(elm, linfo_);
}
if (elm != NULL){
elm = findDataPacketInRecvHistory(STAILQ_NEXT(elm,linfo_));
if (elm != NULL){
elm2 = STAILQ_NEXT(elm, linfo_);
while(elm2 != NULL){
if (elm2->seq_num_ < seq_num && elm2->t_recv_ <

time){
STAILQ_REMOVE(&r_hist_,elm2,r_hist_entry,linfo_);
delete elm2;
} else
elm = elm2;
elm2 = STAILQ_NEXT(elm, linfo_);}
}}}
void DCCPTFRCAgent::removeAcksRecvHistory(){
struct r_hist_entry *elm1 = STAILQ_FIRST(&r_hist_);
struct r_hist_entry *elm2;
int num_later = 1;
while (elm1 != NULL && num_later <= num_dup_acks_){
num_later;
elm1 = STAILQ_NEXT(elm1, linfo_);
}
if(elm1 == NULL)
return;
elm2 = STAILQ_NEXT(elm1, linfo_);
while(elm2 != NULL){
if (elm2->type_ == DCCP_ACK){
STAILQ_REMOVE(&r_hist_,elm2,r_hist_entry,linfo_);
delete elm2;
} else {

elm1 = elm2;
}
elm2 = STAILQ_NEXT(elm1, linfo_);
}
}
inline r_hist_entry
*DCCPTFRCAgent::findDataPacketInRecvHistory(r_hist_entry *start){
while(start != NULL && start->type_ == DCCP_ACK)
start = STAILQ_NEXT(start,linfo_);
return start;
}
Ex.No: 8 Study of TCP/UDP performance using Simulation tool.

TCP performance:

PROGRAM:
set ns [new Simulator]
$ns color 0 Blue
$ns color 1 Red
$ns color 2 Yellow
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set f [open tcpout.tr w]
$ns trace-all $f
set nf [open tcpout.nam w]
$ns namtrace-all $nf
$ns duplex-link $n0 $n2 5Mb 2ms DropTail
$ns duplex-link $n1 $n2 5Mb 2ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-up
$ns duplex-link-op $n1 $n2 orient right-down
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n2 $n3 queuePos 0.5
set tcp [new Agent/TCP]
$tcp set class_ 1
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $tcp
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.2 "$ftp start"
$ns at 1.35 "$ns detach-agent $n1 $tcp ; $ns detach-agent $n3 $sink"
$ns at 3.0 "finish"
proc finish {}
{
global ns f nf
$ns flush-trace
close $f
close $nf
puts "Running nam.."
exec xgraph tcpout.tr -geometry 600x800 &
exec nam tcpout.nam &
exit 0
}
$ns run
Output:
UDP Performance:

PROGRAM:

set ns [new Simulator]

$ns color 0 Blue

$ns color 1 Red

$ns color 2 Yellow

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set f [open udpout.tr w]

$ns trace-all $f

set nf [open udpout.nam w]

$ns namtrace-all $nf

$ns duplex-link $n0 $n2 5Mb 2ms DropTail

$ns duplex-link $n1 $n2 5Mb 2ms DropTail

$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail

$ns duplex-link-op $n0 $n2 orient right-up

$ns duplex-link-op $n1 $n2 orient right-down

$ns duplex-link-op $n2 $n3 orient right

$ns duplex-link-op $n2 $n3 queuePos 0.5

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

set udp1 [new Agent/UDP]

$ns attach-agent $n3 $udp1


$udp1 set class_ 0

set cbr1 [new Application/Traffic/CBR]

$cbr1 attach-agent $udp1

set null0 [new Agent/Null]

$ns attach-agent $n1 $null0

set null1 [new Agent/Null]

$ns attach-agent $n1 $null1

$ns connect $udp0 $null0

$ns connect $udp1 $null1

$ns at 1.0 "$cbr0 start"

$ns at 1.1 "$cbr1 start"

puts [$cbr0 set packetSize_]

puts [$cbr0 set interval_]

$ns at 3.0 "finish"

proc finish {} {

global ns f nf

$ns flush-trace

close $f

close $nf

puts "Running nam.."

exec nam udpout.nam &

exit 0

$ns run
Output:
Ex.No: 9 Simulation of Distance Vector/ Link State Routing algorithm.

LINK STATE ROUTING PROTOCOL

PROGRAM

set ns [new Simulator]

$ns rtproto LS

set nf [open linkstate.nam w]

$ns namtrace-all $nf

set f0 [open linkstate.tr w]

$ns trace-all $f0

proc finish {} {

global ns f0 nf

$ns flush-trace

close $f0

close $nf

exec nam linkstate.nam &

exit 0}

for {set i 0} {$i <7} {incr i} {

set n($i) [$ns node]}

for {set i 0} {$i <7} {incr i} {

$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail}

set udp0 [new Agent/UDP]

$ns attach-agent $n(0) $udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.005

$cbr0 attach-agent $udp0

set null0 [new Agent/Null]

$ns attach-agent $n(3) $null0


$ns connect $udp0 $null0

$ns at 0.5 "$cbr0 start"

$ns rtmodel-at 1.0 down $n(1) $n(2)

$ns rtmodel-at 2.0 up $n(1) $n(2)

$ns at 4.5 "$cbr0 stop"

$ns at 5.0 "finish"

$ns run
Output:
DISTANCE VECTOR ROUTING

PROGRAM

#Distance vector routing protocol – distvect.tcl

#Create a simulator object

set ns [new Simulator]

#Use distance vector routing

$ns rtproto DV

#Open the nam trace file

set nf [open out.nam w]

$ns namtrace-all $nf

# Open tracefile

set nt [open trace.tr w]

$ns trace-all $nt

#Define 'finish' procedure

proc finish {}{

global ns nf

$ns flush-trace

#Close the trace file

close $nf

#Execute nam on the trace file

exec nam -a out.nam &

exit 0}

# Create 8 nodes

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]


set n6 [$ns node]

set n7 [$ns node]

set n8 [$ns node]

# Specify link characterestics

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n2 $n3 1Mb 10ms DropTail

$ns duplex-link $n3 $n4 1Mb 10ms DropTail

$ns duplex-link $n4 $n5 1Mb 10ms DropTail

$ns duplex-link $n5 $n6 1Mb 10ms DropTail

$ns duplex-link $n6 $n7 1Mb 10ms DropTail

$ns duplex-link $n7 $n8 1Mb 10ms DropTail

$ns duplex-link $n8 $n1 1Mb 10ms DropTail

# specify layout as a octagon

$ns duplex-link-op $n1 $n2 orient left-up

$ns duplex-link-op $n2 $n3 orient up

$ns duplex-link-op $n3 $n4 orient right-up

$ns duplex-link-op $n4 $n5 orient right

$ns duplex-link-op $n5 $n6 orient right-down

$ns duplex-link-op $n6 $n7 orient down

$ns duplex-link-op $n7 $n8 orient left-down

$ns duplex-link-op $n8 $n1 orient left

#Create a UDP agent and attach it to node n1

set udp0 [new Agent/UDP]

$ns attach-agent $n1 $udp0

#Create a CBR traffic source and attach it to udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.005


$cbr0 attach-agent $udp0

#Create a Null agent (a traffic sink) and attach it to node n4

set null0 [new Agent/Null]

$ns attach-agent $n4 $null0

#Connect the traffic source with the traffic sink

$ns connect $udp0 $null0

#Schedule events for the CBR agent and the network dynamics

$ns at 0.0 "$n1 label Source"

$ns at 0.0 "$n4 label Destination"

$ns at 0.5 "$cbr0 start"

$ns rtmodel-at 1.0 down $n3 $n4

$ns rtmodel-at 2.0 up $n3 $n4

$ns at 4.5 "$cbr0 stop"

#Call the finish procedure after 5 seconds of simulation time

$ns at 5.0 "finish"

#Run the simulation

$ns run
OUTPUT
Ex.No:10 Simulation of Error Detection Code (like CRC)

PROGRAM:

import java.io.*;

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];

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

for(int i=0;i<div.length;i++){

//append dividend and remainder

crc[i]=(div[i]^rem[i]);}

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

for(int j=0; j<crc.length; j++){

rem[j] = crc[j];}

rem=divide(crc, divisor, rem);

for(int i=0; i< rem.length; i++){

if(rem[i]!=0){

System.out.println("Error");

break;}

if(i==rem.length-1)

System.out.println("No Error");}

System.out.println("THANK YOU )");}

static int[] divide(int div[],int divisor[], int rem[]){

int cur=0;

while(true){

for(int i=0;i<divisor.length;i++)

rem[cur+i]=(rem[cur+i]^divisor[i]);

while(rem[cur]==0 && cur!=rem.length-1)

cur++;

if((rem.length-cur)<divisor.length)

break;}

return rem;}}
OUTPUT :
Enter number of data bits :
7
Enter data bits :
1
0
1
1
0
0
1
Enter number of bits in divisor :
3
Enter Divisor bits :
1
0
1
Dividend (after appending 0's) are : 101100100
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. )
BUILD SUCCESSFUL (total time: 1 minute 34 seconds)

You might also like