Cnmanual
Cnmanual
Cnmanual
R N S INSTITUTE OF TECHNOLOGY
UTTARAHALLI ROAD, CHANNASANDRA, BANGALORE -560 098
Faculty-in-charge
Dr. Prakasha S and Mrs. Sudha V
R N S INSTITUTE OF TECHNOLOGY
UTTARAHALLI ROAD, CHANNASANDRA, BANGALORE -560 098
• PSO1 – Problem Solving Abilities: Ability to demonstrate the fundamental and theoretical
concepts, analyze the real-time problems and develop customized software solutions by applying
the knowledge of mathematics and algorithmic techniques.
• PSO2 – Applied Engineering Skills: Enable creative thinking, Ability to apply standard
practices and strategies, technical skills in software design, development, integration of systems
and management for improving the security, reliability and survivability of the infrastructure.
• PSO3 – General Expertise and Higher Learning – Ability to exchange knowledge effectively,
demonstrate the ability of team work, documentation skills, professional ethics, entrepreneurial
skills and continuing higher education in the field of Information technology.
R N S INSTITUTE OF TECHNOLOGY
UTTARAHALLI ROAD, CHANNASANDRA, BANGALORE -560 098
Course objectives
This course will enable students to
CO1 Develop programs to demonstrate the communication for wired networks using NS-3
Simulator
CO2 Develop programs to demonstrate the communication for wireless networks using NS-3
Simulator.
CO3 Develop programs to related to Data integrity and Security.
CO4 Create programs to transfer data using TCP and UDP transport layer protocols.
CO5 Develop programs to find the shortest path using routing algorithms.
CO6 Develop programs to avoid congestion in the network.
CO mapping to PO/PSOs
CO /
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
PO & PSO
15CSL48.1 2 3 3 2 2 1
15CSL48.2 3 3 2 2 2 1
15CSL48.3 3 3 2 2 2
15CSL48.4 3 3 2 2 2
15CSL48.5 3 3 2 2 2
15CSL48.6 3 3 2 2 2
Sl.
Name of Experiment C
No
O
PART B
Write a program for error detecting code using CRC-CCITT(16 bits)
7 CO3
Write a program to find the shortest path between vertices using bellman- ford algorithm.
8 CO5
Using TCP/IP sockets, write a client-server program to make the client send the
9 File name and to make the server send back the contents of the requested file if present. CO4
Write a program on datagram socket for client/server to display the messages on Client
10 side, typed at the server side.
CO4
11 Write a program for simple RSA algorithm to encrypt and decrypt the data. CO3
1 Week1
2 Week2
3 Week3
10. Design and develop an assembly program to drive a Stepper Motor interface
and rotate the motor in specified direction (clockwise or counter-clockwise) by
8 N steps (Direction and N are specified by the examiner). Introduce suitable delay Week8
between successive steps. (Any arbitrary value for the delay may be assumed by
the student).
11a. Design and develop an assembly language program to generate the Sine Wave
using DAC interface (The output of the DAC is to be displayed on the CRO). Week9
9
11b. Generate a Half Rectified Sine waveform using the DAC interface. (The
output of the DAC is to be displayed on the CRO).
6. To write and simulate ARM assembly language programs for data transfer,
arithmetic and logical operations (Demonstrate with the help of a suitable Week10
10
program).
7.To write and simulate C Programs for ARM microprocessor using KEIL
13.To interface Stepper motor with ARM processor-- RM7TDMI/LPC2148. Write Week12
12
a program to rotate stepper motor
Note:
Here onwards lab experiments and solutions continues.
Marks distribution in Rubrix table change as per ur lab subject.
15 scheme as given above.
10 scheme is 10 for daily assess+5 for record+10 for test
17 scheme 8 for observation +8 for record +8 for viva+16 for test (3+3+10)
CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1
PART -A
1. Implement three nodes point – to – point network with duplex links between them. Set the queue
size, vary the bandwidth and find the number of packets dropped.
Network topology
10.1.1.0 10.1.2.0
n0 -------------- n1.......... n2
point-to-point
In this program we have created 3 point to point nodes n0, n1, n2. Node n0 has IP
address 10.1.1.1 and n3 has 10.1.2.2. Node n1 has 2 interfaces(10.1.1.2 and 10.1.2.1).
OnOffHelper application is used to generate the traffic at source node n0. Packets move
from n0 to n2 via n1. Acknowledgment is sent from n2 to n0 via n1. Details of the
flow(Number of packets sent, received and dropped) can be verified by using
tracemetrics(lab1.tr file).
Program
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/traffic-control-module.h"
NS_LOG_COMPONENT_DEFINE ("Lab-Program-1");
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (3); //3 point-to-point nodes are created
InternetStackHelper stack;
stack.Install (nodes); //TCP-IP layer functionality configured on all nodes
//Bandwidth and delay set for the point-to-point channel. Vary these parameters to //see the variation in
number of packets sent/received/dropped.
PointToPointHelper p2p1;
p2p1.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
p2p1.SetChannelAttribute ("Delay", StringValue ("1ms"));
//Set the base address for the first network(nodes n0 and n1)
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
NetDeviceContainer devices;
//Set the base address for the second network(nodes n1 and n2)
devices = p2p1.Install (nodes.Get (1), nodes.Get (2));
address.SetBase ("10.1.2.0", "255.255.255.0"); interfaces =
address.Assign (devices);
//RateErrorModel allows us to introduce errors into a Channel at a given rate. //Vary the error rate
value to see the variation in number of packets dropped
Ptr<RateErrorModel>em = CreateObject<RateErrorModel> (); em-
>SetAttribute ("ErrorRate", DoubleValue (0.00002));
devices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (em));
uint16_t port = 7;
//Install receiver (for packetsink) on node 2
Address localAddress1 (InetSocketAddress (Ipv4Address::GetAny (), port));
PacketSinkHelper packetSinkHelper1 (socketType, localAddress1);
ApplicationContainer sinkApp1 = packetSinkHelper1.Install (nodes.Get (2));
sinkApp1.Start (Seconds (0.0));
sinkApp1.Stop (Seconds (10));
AsciiTraceHelper ascii;
p2p1.EnableAsciiAll (ascii.CreateFileStream ("lab1.tr"));
Network topology
n0 n1 n2 n3 n4 n5
| | | | | |
===========================
CSMA channel with base IP 10.1.1.0
In this program we have created 6 CSMA nodes n0, n1, n2, n3, n4 and n5 with IP
addresses 10.1.1.1, 10.1.1.2, 10.1.1.3, 10.1.1.4, 10.1.1.5 and 10.1.1.6 respectively.
Nodes n0 and n1 ping node n2, we can visualize the ping messages transferred between
the nodes. Data transfer is also simulated between the nodes n0 and n2 using
UdpSocketFactory to generate traffic.
Program
#include <iostream>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/applications-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/internet-module.h"
NS_LOG_COMPONENT_DEFINE ("Lab-Program-2");
ipStack.Install (c);
// assign ip addresses
NS_LOG_INFO ("Assign ip addresses.");
Ipv4AddressHelper ip;
ip.SetBase ("192.168.1.0", "255.255.255.0");Ipv4InterfaceContainer
addresses = ip.Assign (devs);
/ Create an OnOff application to send UDP datagrams from node zero to node 1.
NS_LOG_INFO ("Create Applications.");
uint16_t port = 9; // Discard port (RFC 863)
ApplicationContainer apps;
apps = ping.Install (pingers);
apps.Start (Seconds (1.0));
apps.Stop (Seconds (5.0));
AsciiTraceHelper ascii;
csma.EnableAsciiAll (ascii.CreateFileStream ("ping1.tr"));
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");
}
Node n1 sends ping message to n2(Broadcast message is generated) and only n2 responds to n1
Node n0 sends ping message to n2(Broadcast message is generated) and only n2 responds to n0
3. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.
Network topology
n0 n1 n2 n3
| | | |
===================== CSMA channel with base IP 10.1.1.0
Source node – n0 sink node - n1
In this program we have created 4 CSMA nodes n0, n1, n2 and n3 with IP addresses 10.1.1.1,
10.1.1.2, 10.1.1.3 and 10.1.1.4 respectively. Data transmission is simulated between nodes n0
and n1. Once the cwnd values are generated, they are exported to .dat file and congestion
graph is plot using gnuplot.
Program
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include <iostream>
#include "ns3/csma-module.h"
MyApp ();
virtual ~MyApp();
void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate
dataRate);
private:
virtual void StartApplication (void);
virtual void StopApplication (void);
Ptr<Socket> m_socket;
Address m_peer;
uint32_t m_packetSize;
uint32_t m_nPackets;
DataRate m_dataRate;
EventId m_sendEvent;
bool m_running;
uint32_t m_packetsSent;
};
MyApp::MyApp () // constructor
: m_socket (0),
m_peer (),
m_packetSize
(0),
m_nPackets
(0),
m_dataRate
(0),
m_sendEvent
(), m_running
(false),
m_packetsSen
t (0)
{
}
MyApp::~MyApp() // destructor
{
m_socket = 0;
}
//The next bit of code explains to the Application how to stop creating simulation //events.
//StartApplication calls SendPacket to start the chain of events that describes the //Application behavior.
void MyApp::SendPacket (void)
{
Ptr<Packet> packet = Create<Packet> (m_packetSize);
m_socket->Send (packet);
//Below function logs the current simulation time and the new value of the congestion
window every time it is changed.
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (0.0001)));
NetDeviceContainer devices;
devices = csma.Install (nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");Ipv4InterfaceContainer
interfaces = address.Assign (devices);
//PacketSink Application is used on the destination node to receive TCP connections //and data.
//next two lines of code will create the socket and connect the trace source.
//tell the Application what Socket to use, what address to connect to, how much //data to send at each
send event, how many send events to generate and the rate at //which to produce data from those events.
return 0;
}
i
n
b
i
ts
Time in seconds
4.Implement simple ESS and with transmitting nodes in wire-less LAN by simulation and determine the
performance with respect to transmission ofpackets.
Default Network Topology
|
Rank 0 | Rank 1
------------------
------ |----------------------------
Wifi 10.1.3.0
AP
* * * *
| | | | 10.1.1.0
n2 n3 n4 n0 n1
point-to-point |
Program
#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");
CommandLine cmd;
cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); cmd.Parse (argc,argv);
if (verbose)
{
NodeContainer p2pNodes;
p2pNodes.Create (2);// 2 nodes are n0,n1 are created
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
NodeContainer wifiStaNodes;
wifiStaNodes.Create (nWifi);
NodeContainer wifiApNode = p2pNodes.Get (0);// 1st node of p2p is also access point
WifiHelper wifi;
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");//AARF= rate control algorithm
WifiMacHelper mac;
Ssid ssid = Ssid ("ns-3-ssid");// ssid=service set identifier in 802.11 mac.SetType
("ns3::StaWifiMac",
"Ssid", SsidValue (ssid),
"ActiveProbing", BooleanValue (false));
NetDeviceContainer staDevices;
staDevices = wifi.Install (phy, mac, wifiStaNodes);
NetDeviceContainer apDevices;
apDevices = wifi.Install (phy, mac, wifiApNode);
MobilityHelper mobility;
mobility.SetMobilityModel
("ns3::RandomWalk2dMobilityModel","Bounds",RectangleValue
(Rectangle (-50, 50, -50, 50))); mobility.Install (wifiStaNodes);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (wifiApNode);
InternetStackHelper stack;
stack.Install (p2pNodes.Get(1));// stack installed on n1 of p2p stack.Install
(wifiApNode);//stack installed on access point
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0",
"255.255.255.0");Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
ApplicationContainer clientApps =
echoClient.Install (wifiStaNodes.Get (nWifi - 1)); clientApps.Start
(Seconds (2.0)); clientApps.Stop (Seconds (10.0));
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Output
5. Implement and study the performance of GSM on NS2/NS3 (Using MAC layer) or equivalent
environment.
6. Implement and study the performance of CDMA on NS2/NS3 (Using stack called Call net) or
equivalent environment.
#include "ns3/lte-helper.h"
#include "ns3/epc-helper.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/internet-module.h"
#include "ns3/mobility-module.h"
#include "ns3/lte-module.h"
#include "ns3/applications-module.h"
#include "ns3/point-to-point-helper.h"
#include "ns3/config-store.h"
//#include "ns3/gtk-config-store.h"
//.............................................................................................................
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("EpcFirstExample");
int
main (int argc, char *argv[])
{
cmd.Parse(argc, argv);
inputConfig.ConfigureDefaults();
//Specify configuration parameters of the objects that are being used for the simulation
// parse again so you can override default values from the command line
cmd.Parse(argc, argv);
Ipv4StaticRoutingHelper ipv4RoutingHelper;
Ptr<Ipv4StaticRouting> remoteHostStaticRouting = ipv4RoutingHelper.GetStaticRouting
(remoteHost->GetObject<Ipv4> ());
remoteHostStaticRouting->AddNetworkRouteTo (Ipv4Address ("7.0.0.0"), Ipv4Mask ("255.0.0.0"),
1);
NodeContainer ueNodes;
NodeContainer enbNodes;
enbNodes.Create(numberOfNodes);
ueNodes.Create(numberOfNodes);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.SetPositionAllocator(positionAlloc);
mobility.Install(enbNodes);
mobility.Install(ueNodes);
Simulator::Stop(Seconds(simTime));
Simulator::Run();
/*GtkConfigStore config;
config.ConfigureAttributes();*/
Simulator::Destroy();
return 0;
Output
Part –B
7. Write a program for error detecting code using CRC-CCITT (16- bits).
import java.io.*;
import java.util.Scanner;
class crcscanner
{
public static void main(String a[]) throws IOException
{
Scanner sc=new Scanner(System.in);
int[] message;
int[] gen;
int[] app_message;
int[] rem;
int[] trans_message;
int message_bits,gen_bits, total_bits;
System.out.println("\n Enter number of bits in message : ");
message_bits=sc.nextInt();
message=new int[message_bits];
System.out.println("\n Enter message bits : ");
for(int i=0; i<message_bits; i++)
message[i]=sc.nextInt();
System.out.println("\n Enter number of bits in gen : ");
gen_bits=sc.nextInt();
gen=new int[gen_bits];
System.out.println("\n Enter gen bits : ");
for(int i=0; i<gen_bits; i++)
{
gen[i]=sc.nextInt();
}
total_bits=message_bits+gen_bits-1;
app_message=new int[total_bits];
rem=new int[total_bits];
trans_message=new int[total_bits];
for(int i=0;i<message.length;i++)
{
app_message[i]=message[i];
}
System.out.print("\n Message bits are : ");
for(int i=0; i< message_bits; i++)
{
System.out.print(message[i]);
}
System.out.print("\n Generators bits are : ");
for(int i=0; i< gen_bits; i++)
{
System.out.print(gen[i]);
}
System.out.print("\n Appended message is : ");
for(int i=0; i< app_message.length; i++)
{
System.out.print(app_message[i]);
}
for(int j=0; j<app_message.length; j++)
{
rem[j] = app_message[j];
}
rem=computecrc(app_message, gen, rem);
for(int i=0;i<app_message.length;i++)
{
trans_message[i]=(app_message[i]^rem[i]);
}
System.out.println("\n Transmitted message from the transmitter is : ");
for(int i=0;i<trans_message.length;i++)
{
System.out.print(trans_message[i]);
}
System.out.println("\n Enter received message of "+total_bits+" bits at receiver end : ");
for(int i=0; i<trans_message.length; i++)
{
trans_message[i]=sc.nextInt();;
}
System.out.println("\n Received message is :");
for(int i=0; i< trans_message.length; i++)
{
System.out.print(trans_message[i]);
}
for(int j=0; j<trans_message.length; j++)
{
rem[j] = trans_message[j];
}
rem=computecrc(trans_message, gen, rem);
for(int i=0; i< rem.length; i++)
{
if(rem[i]!=0)
{
System.out.println("\n There is Error in the received me ");
break;
}
if(i==rem.length-1)
System.out.println("\n There is No Error in the received m ");
}
}
8. Write a program to find the shortest path between vertices using bellman-ford
algorithm.
import java.util.Scanner;
public class BellmanFord
{
private int D[]; private int num_ver;
public static final int MAX_VALUE = 999;
public BellmanFord(int num_ver)
{
this.num_ver = num_ver; D = new int[num_ver + 1];
}
public void BellmanFordEvaluation(int source, int A[][])
{
}
D[source] = 0;
for (int node = 1; node <= num_ver - 1; node++)
{
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
if (A[sn][dn] != MAX_VALUE)
{
if (D[dn] > D[sn]+ A[sn][dn])
D[dn] = D[sn] + A[sn][dn];
}
}
}
}
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
if (A[sn][dn] != MAX_VALUE)
{
if (D[dn] > D[sn]+ A[sn][dn])
System.out.println("The Graph contains negative egde cycle");
}
}
}
for (int vertex = 1; vertex <= num_ver; vertex++)
{
System.out.println("distance of source " + source + " to "+ vertex + " is " + D[vertex]);
}
}
public static void main(String[ ] args)
{
int num_ver = 0; int source;
Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of
vertices"); num_ver = scanner.nextInt();
int A[][] = new int[num_ver + 1][num_ver + 1]; System.out.println("Enter the adjacency
matrix"); for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
Input graph:
Output:
9.Using TCP/IP sockets, write a client – server program to make the client send the file name and
to make the server send back the contents of the requested file if present.
Server Program
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public final static int SOCKET_PORT = 13267; // you may change this
public final static String FILE_TO_SEND = "e:/source1.txt"; // you may change this
Client Program
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public final static int SOCKET_PORT = 13267; // you may change this
public final static int FILE_SIZE = 6022386; // file size temporary hard coded
int bytesRead;
int current = 0;
try {
System.out.println("Connecting...");
// receive file
InputStream is = sock.getInputStream();
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
bos.write(mybytearray, 0 , current);
bos.flush();
finally {
10. Write a program on datagram socket for client/server to display the messages on client side,
typed at the server side.
UDP Client
import java.io.*;
import java.net.*;
public class UDPC
{
public static void main(String[] args)
{
DatagramSocket skt;
try {
skt=new DatagramSocket(); String msg= "text message "; byte[] b = msg.getBytes();
InetAddress host=InetAddress.getByName("127.0.0.1"); int serverSocket=6788;
DatagramPacket request =new DatagramPacket (b,b.length,host,serverSocket); skt.send(request);
byte[] buffer =new byte[1000];
DatagramPacket reply= new DatagramPacket(buffer,buffer.length); skt.receive(reply);
System.out.println("client received:" +new String(reply.getData())); skt.close();
}
catch(Exception ex)
{
}
}
}
UDP Server
import java.io.*; import java.net.*;
public class UDPS
{
public static void main(String[] args)
{
DatagramSocket skt=null;
try
{
skt=new DatagramSocket(6788); byte[] buffer = new byte[1000];
while(true)
{
DatagramPacket request = new DatagramPacket(buffer,buffer.length);
skt.receive(request);
String[] message = (new String(request.getData())).split("");
byte[] sendMsg= (message[1]+ " server processed").getBytes();
DatagramPacket reply = new
DatagramPacket(sendMsg,sendMsg.length,request.getAddress(),request.getPort());
skt.send(reply);
}
}
catch(Exception ex)
{
}
}
}
11. Write a program for simple RSA algorithm to encrypt and decrypt the data.
import java.math.BigInteger;
import java.util.Random;
import java.io.*;
private BigInteger p;
private BigInteger q;
private BigInteger N;
private BigInteger e;
private BigInteger d;
//blocksize in byte
private Random r;
public RSA() {
r = new Random();
p = BigInteger.probablePrime(bitlength, r);
q = BigInteger.probablePrime(bitlength, r);
N = p.multiply(q);
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(bitlength/2, r);
e.add(BigInteger.ONE);
d = e.modInverse(phi);
this.e = e;
this.d = d;
this.N = N;
String teststring ;
teststring=in.readLine();
// encrypt
// decrypt
test += Byte.toString(b);
return test;
//Encrypt message
// Decrypt message
12. Write a program for congestion control using leaky bucket algorithm.
filename:Licky.java
import java.io.*;
import java.util.*;
class Queue
{
int q[],f=0,r=0,size;
void insert(int n)
{
Scanner in = new Scanner(System.in);
q=new int[10];
for(int i=0;i<n;i++)
{
System.out.print("\nEnter " + i + " element: ");
int ele=in.nextInt();
if(r+1>10)
{
System.out.println("\nQueue is full \nLost Packet: "+ele);
break;
}
else
{
r++;
q[i]=ele;
}
}
}
void delete()
{
Scanner in = new Scanner(System.in);
Thread t=new Thread();
if(r==0)
else
{
for(int i=f;i<r;i++)
{
try
{
t.sleep(1000);
}
catch(Exception e){}
System.out.print("\nLeaked Packet: "+q[i]);
f++;
}
}
System.out.println();
}
}
}
/*
OUTPUT
Enter 0 element: 1
Enter 1 element: 0
Enter 2 element: 2
Enter 3 element: 3
Enter 4 element: 4
Enter 5 element: 5
Enter 6 element: 6
Enter 7 element: 7
Enter 8 element: 8
Enter 9 element: 9
Enter 10 element: 10
Queue is full
Lost Packet: 10
Leaked Packet: 1
Leaked Packet: 0
Leaked Packet: 2
Leaked Packet: 3
Leaked Packet: 4
Leaked Packet: 5
Leaked Packet: 6
Leaked Packet: 7
Leaked Packet: 8
Leaked Packet: 9
import java.io.*;
import java.util.Scanner;
class crcscanner
{
public static void main(String a[]) throws IOException
{
Scanner sc=new Scanner(System.in);
int[] message;
int[] gen;
int[] app_message;
int[] rem;
int[] trans_message;
int message_bits,gen_bits, total_bits;
System.out.println("\n Enter number of bits in message : ");
message_bits=sc.nextInt();
message=new int[message_bits];
System.out.println("\n Enter message bits : ");
for(int i=0; i<message_bits; i++)
message[i]=sc.nextInt();
System.out.println("\n Enter number of bits in gen : ");
gen_bits=sc.nextInt();
gen=new int[gen_bits];
System.out.println("\n Enter gen bits : ");
for(int i=0; i<gen_bits; i++)
{
gen[i]=sc.nextInt();
}
total_bits=message_bits+gen_bits-1;
app_message=new int[total_bits];
rem=new int[total_bits];
trans_message=new int[total_bits];
for(int i=0;i<message.length;i++)
{
app_message[i]=message[i];
}
System.out.print("\n Message bits are : ");
for(int i=0; i< message_bits; i++)
{
System.out.print(message[i]);
}
System.out.print("\n Generators bits are : ");
for(int i=0; i< gen_bits; i++)
{
System.out.print(gen[i]);
}
System.out.print("\n Appended message is : ");
for(int i=0; i< app_message.length; i++)
{
System.out.print(app_message[i]);
}
for(int j=0; j<app_message.length; j++)
{
rem[j] = app_message[j];
}
rem=computecrc(app_message, gen, rem);
for(int i=0;i<app_message.length;i++)
{
trans_message[i]=(app_message[i]^rem[i]);
}
System.out.println("\n Transmitted message from the transmitter is : ");
for(int i=0;i<trans_message.length;i++)
{
System.out.print(trans_message[i]);
}
System.out.println("\n Enter received message of "+total_bits+" bits at receiver end : ");
for(int i=0; i<trans_message.length; i++)
{
trans_message[i]=sc.nextInt();;
}
System.out.println("\n Received message is :");
for(int i=0; i< trans_message.length; i++)
{
System.out.print(trans_message[i]);
}
for(int j=0; j<trans_message.length; j++)
{
rem[j] = trans_message[j];
}
rem=computecrc(trans_message, gen, rem);
for(int i=0; i< rem.length; i++)
{
if(rem[i]!=0)
{
System.out.println("\n There is Error in the received me ");
break;
}
if(i==rem.length-1)
System.out.println("\n There is No Error in the received m ");
}
}
8. Write a program to find the shortest path between vertices using bellman-ford
algorithm.
import java.util.Scanner;
public class BellmanFord
{
private int D[]; private int num_ver;
public static final int MAX_VALUE = 999;
public BellmanFord(int num_ver)
{
this.num_ver = num_ver; D = new int[num_ver + 1];
}
public void BellmanFordEvaluation(int source, int A[][])
{
for (int node = 1; node <= num_ver; node++)
{
D[node] = MAX_VALUE;
}
D[source] = 0;
for (int node = 1; node <= num_ver - 1; node++)
{
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
if (A[sn][dn] != MAX_VALUE)
{
if (D[dn] > D[sn]+ A[sn][dn])
D[dn] = D[sn] + A[sn][dn];
}
}
}
}
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
if (A[sn][dn] != MAX_VALUE)
{
if (D[dn] > D[sn]+ A[sn][dn])
System.out.println("The Graph contains negative egde cycle");
}
}
}
for (int vertex = 1; vertex <= num_ver; vertex++)
{
System.out.println("distance of source " + source + " to "+ vertex + " is " + D[vertex]);
}
}
public static void main(String[ ] args)
{
int num_ver = 0; int source;
Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of
vertices"); num_ver = scanner.nextInt();
int A[][] = new int[num_ver + 1][num_ver + 1]; System.out.println("Enter the adjacency
matrix"); for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
Output:
9.Using TCP/IP sockets, write a client – server program to make the client send the file
name and to make the server send back the contents of the requested file if present.
Server Program
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public final static int SOCKET_PORT = 13267; // you may change this
public final static String FILE_TO_SEND = "e:/source1.txt"; // you may change this
Client Program
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public final static int FILE_SIZE = 6022386; // file size temporary hard coded
int bytesRead;
int current = 0;
try {
System.out.println("Connecting...");
// receive file
InputStream is = sock.getInputStream();
fos = new FileOutputStream(FILE_TO_RECEIVED);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
bos.write(mybytearray, 0 , current);
bos.flush();
finally {
}
}
10. Write a program on datagram socket for client/server to display the messages on
client side, typed at the server side.
UDP Client
import java.io.*;
import java.net.*;
public class UDPC
{
public static void main(String[] args)
{
DatagramSocket skt;
try {
skt=new DatagramSocket(); String msg= "text message "; byte[] b = msg.getBytes();
InetAddress host=InetAddress.getByName("127.0.0.1"); int serverSocket=6788;
DatagramPacket request =new DatagramPacket (b,b.length,host,serverSocket);
skt.send(request);
byte[] buffer =new byte[1000];
DatagramPacket reply= new DatagramPacket(buffer,buffer.length); skt.receive(reply);
System.out.println("client received:" +new String(reply.getData())); skt.close();
}
catch(Exception ex)
{
}
}
}
UDP Server
import java.io.*; import java.net.*;
public class UDPS
{
public static void main(String[] args)
{
DatagramSocket skt=null;
try
{
skt=new DatagramSocket(6788); byte[] buffer = new byte[1000];
while(true)
{
DatagramPacket request = new DatagramPacket(buffer,buffer.length);
skt.receive(request);
String[] message = (new String(request.getData())).split("");
byte[] sendMsg= (message[1]+ " server processed").getBytes();
DatagramPacket reply = new
DatagramPacket(sendMsg,sendMsg.length,request.getAddress(),request.getPort());
skt.send(reply);
}
}
catch(Exception ex)
{
}
}
}
13. Write a program for simple RSA algorithm to encrypt and decrypt the data.
import java.math.BigInteger;
import java.util.Random;
import java.io.*;
private BigInteger p;
private BigInteger q;
private BigInteger N;
private BigInteger e;
private BigInteger d;
//blocksize in byte
private Random r;
public RSA() {
r = new Random();
p = BigInteger.probablePrime(bitlength, r);
q = BigInteger.probablePrime(bitlength, r);
N = p.multiply(q);
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(bitlength/2, r);
e.add(BigInteger.ONE);
d = e.modInverse(phi);
this.e = e;
this.d = d;
this.N = N;
teststring=in.readLine();
// encrypt
// decrypt
test += Byte.toString(b);
return test;
//Encrypt message
}
// Decrypt message
14. Write a program for congestion control using leaky bucket algorithm.
filename:Licky.java
import java.io.*;
import java.util.*;
class Queue
{
int q[],f=0,r=0,size;
void insert(int n)
{
Scanner in = new Scanner(System.in);
q=new int[10];
for(int i=0;i<n;i++)
{
System.out.print("\nEnter " + i + " element: ");
int ele=in.nextInt();
if(r+1>10)
{
System.out.println("\nQueue is full \nLost Packet: "+ele);
break;
}
else
{
r++;
q[i]=ele;
}
}
}
void delete()
{
Scanner in = new Scanner(System.in);
Thread t=new Thread();
if(r==0)
System.out.print("\nQueue empty ");
else
{
for(int i=f;i<r;i++)
{
try
{
t.sleep(1000);
}
catch(Exception e){}
System.out.print("\nLeaked Packet: "+q[i]);
f++;
}
}
System.out.println();
}
}
}
/*
OUTPUT
Enter 0 element: 1
Enter 1 element: 0
Enter 2 element: 2
Enter 3 element: 3
Enter 4 element: 4
Enter 5 element: 5
Enter 6 element: 6
Enter 7 element: 7
Enter 8 element: 8
Enter 9 element: 9
Enter 10 element: 10
Queue is full
Lost Packet: 10
Leaked Packet: 1
Leaked Packet: 0
Leaked Packet: 2
Leaked Packet: 3
Leaked Packet: 4
Leaked Packet: 5
Leaked Packet: 6
Leaked Packet: 7
Leaked Packet: 8
Leaked Packet: 9