You are on page 1of 46

1.

Write a program for error detecting code using CRC-CCITT (16bit)


#include<stdio.h>
char m[50],g[50],r[50],q[50],temp[50];
void caltrans(int);
void crc(int);
void calram();
void shiftl();
int main()
{
int n,i=0;
char ch,flag=0;
printf("Enter the frame bits:");
while((ch=getc(stdin))!='\n')
m[i++]=ch;
n=i;
for(i=0;i<16;i++)
m[n++]='0';
m[n]='\0';
printf("Message after appending 16 zeros:%s",m);
for(i=0;i<=16;i++)
g[i]='0';
g[0]=g[4]=g[11]=g[16]='1';g[17]='\0';
printf("\ngenerator:%s\n",g);
crc(n);
printf("\n\nquotient:%s",q);
caltrans(n);
printf("\ntransmitted frame:%s",m);
printf("\nEnter transmitted freme:");
scanf("\n%s",m);printf("CRC checking\n");
crc(n);
printf("\n\nlast remainder:%s",r);
for(i=0;i<16;i++)
if(r[i]!='0')
flag=1;
else
continue;
if(flag==1)
printf("Error during transmission");
else
printf("\n\nReceived freme is correct");
}
void crc(int n)
{
int i,j;
for(i=0;i<n;i++)
temp[i]=m[i];
for(i=0;i<16;i++)
r[i]=m[i];
printf("\nintermediate remainder\n");
for(i=0;i<n-16;i++)
{
if(r[0]=='1')

{
q[i]='1';
calram();
}
else
{
q[i]='0';shiftl();
}
r[16]=m[17+i];
r[17]='\0';
printf("\nremainder %d:%s",i+1,r);
for(j=0;j<=17;j++)
temp[j]=r[j];
}
q[n-16]='\0';
}
void calram()
{
int i,j;
for(i=1;i<=16;i++)
r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48;
}
void shiftl()
{
int i;
for(i=1;i<=16;i++)
r[i-1]=r[i];
}
void caltrans(int n)
{
int i,k=0;
for(i=n-16;i<n;i++)
m[i]=((int)m[i]-48)^((int)r[k++]-48)+48;
m[i]='\0';
}OUTPUT:[root@localhost nwcn]# vi 1.c
[root@localhost nwcn]# cc 1.c
[root@localhost nwcn]# ./a.out
Enter the binary data
1011
The msg before adding checksum:
10110000000000000000
The checksum calculated:
1011000101101011
The code word is:10111011000101101011
Enter the transmitted code word
10111011000101101011
Received msg:10111011000101101011
The checksum is:0000000000000000
No error in the msg
[root@localhost nwcn]# cc 1.c
[root@localhost nwcn]# ./a.out

Enter the binary data


1101
The msg before adding checksum:
11010000000000000000
The checksum calculated:
1101000110101101
The code word is:11011101000110101101
Enter the transmitted code word
10111101000110101101
Received msg:10111101000110101101
The checksum is:0110000011000110
Error in the msg

1.) Write a program in C# using command line arguments to display a welcome message. The
message has to be supplied as input in the command line
using System;
class prac1
{
public static void Main(string[] args)
{
foreach(string k in args)
{
Console.Write(k+" ");
}
Console.ReadLine();
}
}
2.) Write a program in C# to find the area of a circle, given its radius.
using System;
class prac2
{
public static void Main()
{
float radius=8.9f;
float pi=3.14f;
float area;
area=pi*radius*radius;
Console.WriteLine("The Area Of Circle whose Radius is {0} :- {1}",radius,area);
Console.ReadLine();
}
}
3.) Write a program in C# to find the area of polygon triangle and rectangle by using
multiple constructors.
using System;
class prac3
{
prac3(double half,double length,double height)
{

double areaoftriangle=half*length*height;
Console.WriteLine("The Area of Triangle of length {0} and height {1} is:
{2}",length,height,areaoftriangle);
Console.ReadLine();
}
prac3(double len,double breadth)
{
double areaofrectangle=len*breadth;
Console.WriteLine("The Area of Rectangle of length {0} and height {1} is:
{2}",len,breadth,areaofrectangle);
Console.ReadLine();
}
public static void Main()
{
prac3 obj1=new prac3(0.5,4.2,3.8);
prac3 obj2=new prac3(5.2,2.6);
Console.ReadLine();
}
}
4.)Write a program in C# to add and multiply two numbers and display the results.
Demonstrate the use of ref and out parameters in this program.
using System;
class prac4
{
public static void add(ref int num1,ref int num2)
{
int res_add=num1+num2;
Console.WriteLine("The Addition Of {0} and {1} usingis : {2} ",num1,num2,res_add);
Console.ReadLine();
}
public static void product(out int number1,out int number2)
{
number1=10;
number2=10;
int res_product=number1*number2;
Console.WriteLine ("The Product Of {0} and {1} usingis : {2}",number1,number2,res_product);
Console.ReadLine();
}
public static void Main(string[] args)
{
int num1;
int num2;
num1=10;
num2=10;
add(ref num1,ref num2);
int number1;
int number2;
product(out number1,out number2);
}
}

5.) Using foreach looping statement, write a program in C# to display all the elements of
string array.
using System;
class prac5
{
public static void Main(string[] args)
{
string[] name={"Shakty","Shashi","Gaurav","Rahul","Pawan","Gandharva","Rakesh"};
foreach(string element in name)
{
Console.WriteLine(element);
Console.WriteLine();
}
Console.ReadLine();
}
}
6.) Write a program in C# to create a string array to store 10 employees name. Display the
employees name to the console by passing array as a parameter to the display function.
using System;
class prac6
{
public static void display(out string[] name)
{
name=new string[10]
{"Shakty","Ronisha","Shashi","Gaurav","Rahul","Pawan","Gandharva","Rakesh","Manish","Mohd
. Arif"};
}
public static void Main(string[] args)
{
string[] name;
display(out name);
Console.WriteLine("Array Elements Are :");
for(int i=0;i
{
Console.WriteLine(name[i]);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press any key to exit..................");
Console.ReadLine();
}
}
7.) Write a program in C# to demonstrate the usage of checked and unchecked statements.
using System;
class prac7

{
static void Main()
{
int x=Int32.MaxValue;
Console.WriteLine(x+1);
checked
{
Console.WriteLine(x+1);
}
unchecked
{
Console.WriteLine(x+1);
}
}
}
8.) Write a class called rectangle which consists of members side_1, side_2 and displayArea().
Write another class square which inherits class rectangle. Take side of square as input and
display the area of square on console.
using System;
public class rectangle
{
public int side_1=30;
public int side_2=40;
public virtual void displayArea()
{
int area=side_1*side_2;
Console.WriteLine("Area of Rectangle is:-{0}",area);
}
}
class square:rectangle
{
public override void displayArea()
{
int area=side_1*side_1;
Console.WriteLine("Area of Square whose side {0} which is inherited fromclass is:{1}",side_1,area);
}
public static void Main()
{
square obj=new square();
obj.displayArea();
Console.ReadLine();
}
}
9.) Write a program to sort 20 decimal numbers in decreasing order and print the result.
using System;
namespace sorting
{
class Class1
{

static void sort(double[] a)


{
int i=0;
while(i
{
double temp;
if(a[i]
{
temp=a[i+1];
a[i+1]=a[i];
a[i]=temp;
i=0;
continue;
}
i++;
}
}
static void display(double[] a)
{
foreach(double x in a)
System.Console.WriteLine(x);
}
static void Main(string[] args)
{
double[] a = new double[]
{87.1,76.4,76.2,80.5,96.7,4.56,34.11,100.45,89.07,44.34,200.1,10.34,96.45,72.33,7.4,2.5,65.56,87.
87,99.83,101.66};
sort(a);
display(a);
Console.Read();
}
}
}
10.) Write a program for matrix multiplication in C#.
using System;
class Matrix3D
{
public const int DIMSIZE = 3;
private double[ , ] matrix = new double[DIMSIZE, DIMSIZE];
public double this[int x,int y]
{
get { return matrix[x, y]; }
set { matrix[x, y] = value;}
}
public static Matrix3D operator *(Matrix3D mat1, Matrix3D mat2)
{
Matrix3D newMatrix = new Matrix3D();
for (int x=0; x < DIMSIZE; x++)
for (int y=0; y < DIMSIZE; y++)
newMatrix[x, y] = mat1[x, y] * mat2[x, y];
return newMatrix;

}
}
class MatrixTest
{
public static Random rand = new Random();
static void Main()
{
Matrix3D mat1 = new Matrix3D();
Matrix3D mat2 = new Matrix3D();
InitMatrix(mat1);
InitMatrix(mat2);
Console.WriteLine("Matrix 1 : ");
PrintMatrix(mat1);
Console.WriteLine("Marix 2: ");
PrintMatrix(mat2);
Matrix3D mat3 = mat1 * mat2;
Console.WriteLine();
Console.WriteLine("Matrix 1 * Matrix 2 = ");
PrintMatrix(mat3);
}
public static void InitMatrix(Matrix3D mat)
{
for (int x=0; x < Matrix3D.DIMSIZE; x++)
for (int y=0; y < Matrix3D.DIMSIZE; y++)
mat[x, y] = rand.NextDouble()*10;
} //done
public static void PrintMatrix(Matrix3D mat)
{
Console.WriteLine();
for (int x=0; x< Matrix3D.DIMSIZE; x++)
{
Console.Write("[ ");
for (int y=0; y < Matrix3D.DIMSIZE; y++)
{
Console.Write("{0,8: # . 00}",mat[x, y]);
if((y+1 % 2)
{
Console.Write(" , ");
}
Console.WriteLine(" ] ");
}
Console.WriteLine();
}
}
}
11.) Write a program in C# to sort the students list based on their names. The students list is
stored in a string array.
using System;
namespace sorting
{
class Class1

{
[STAThread]
static void sort(string[] b)
{
int i=0;
while(i
{
string temp;
if(0
{
temp=b[i+1];
b[i+1]=b[i];
b[i]=temp;
i=0;
continue;
}
i++;
}
}
static void display(string[] b)
{
foreach(string x in b)
System.Console.WriteLine(x);
}
static void Main(string[] args)
{
string[] b = new String[] {"sumit","samit","scmit","sgmit","sglit"};
sort(b);
display(b);
Console.Read();
}
}
}
12.) Write a program in C# to demonstrate operator overloading
using System;
class Matrix3D
{
public const int DIMSIZE = 3;
private double[ , ] matrix = new double[DIMSIZE, DIMSIZE];
public double this[int x,int y]
{
get { return matrix[x, y]; }
set { matrix[x, y] = value;}
}
public static Matrix3D operator +(Matrix3D mat1, Matrix3D mat2)
{
Matrix3D newMatrix = new Matrix3D();
for (int x=0; x < DIMSIZE; x++)
for (int y=0; y < DIMSIZE; y++)
newMatrix[x, y] = mat1[x, y] + mat2[x, y];
return newMatrix;

}
}
class MatrixTest
{
public static Random rand = new Random();
static void Main()
{
Matrix3D mat1 = new Matrix3D();
Matrix3D mat2 = new Matrix3D();
InitMatrix(mat1);
InitMatrix(mat2);
Console.WriteLine("Matrix 1 : ");
PrintMatrix(mat1);
Console.WriteLine("Marix 2: ");
PrintMatrix(mat2);
Matrix3D mat3 = mat1 + mat2;
Console.WriteLine();
Console.WriteLine("Matrix 1 + Matrix 2 = ");
PrintMatrix(mat3);
}
public static void InitMatrix(Matrix3D mat)
{
for (int x=0; x < Matrix3D.DIMSIZE; x++)
for (int y=0; y < Matrix3D.DIMSIZE; y++)
mat[x, y] = rand.NextDouble()*10;
}
public static void PrintMatrix(Matrix3D mat)
{
Console.WriteLine();
for (int x=0; x< Matrix3D.DIMSIZE; x++)
{
Console.Write("[ ");
for (int y=0; y < Matrix3D.DIMSIZE; y++)
{
Console.Write("{0,8: # . 00}",mat[x, y]);
if((y+1 % 2)
{
Console.Write(" , ");
}
Console.WriteLine(" ] ");
}
Console.WriteLine();
}
}
}
13.) Write a program in C# to demonstrate boxing and unboxing opertation.
using System;
class prac13
{
public static void Main()
{

int unbox_var=10;
int box_var=100;
object obj=box_var; //boxing ie converting value type to reference type
Console.WriteLine("Value after boxing is : {0}",obj);
unbox_var=(int)obj; //unboxing ie converting reference type to value type
Console.WriteLine("Value after unboxing is : {0} ",unbox_var);
Console.ReadLine();
}
}
14.) Write a program in C# to throw and catch any arithmetic exception.
using System;
class prac14
{
public static void Main()
{
int num1=10;
int num2=0;
int div;
try
{
div=num1/num2;
Console.WriteLine(div);
Console.ReadLine();
}
catch(DivideByZeroException e)
{
Console.WriteLine("Exception Message : "+e.Message);
}
}
}
15.) Write a program to demonstrate the usage of threads in C#.
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
public class SyncEvents
{
public SyncEvents()
{
_newItemEvent = new AutoResetEvent(false);
_exitThreadEvent = new ManualResetEvent(false);
_eventArray = new WaitHandle[2];
_eventArray[0] = _newItemEvent;
_eventArray[1] = _exitThreadEvent;
}
public EventWaitHandle ExitThreadEvent
{
get { return _exitThreadEvent; }
}
public EventWaitHandle NewItemEvent

{
get { return _newItemEvent; }
}
public WaitHandle[] EventArray
{
get { return _eventArray; }
}
private EventWaitHandle _newItemEvent;
private EventWaitHandle _exitThreadEvent;
private WaitHandle[] _eventArray;
}
public class Producer
{
public Producer(Queueq, SyncEvents e)
{
_queue = q;
_syncEvents = e;
}
public void ThreadRun()
{
int count = 0;
Random r = new Random();
while (!_syncEvents.ExitThreadEvent.WaitOne(0, false))
{
lock (((ICollection)_queue).SyncRoot)
{
while (_queue.Count < 20)
{
_queue.Enqueue(r.Next(0, 100));
_syncEvents.NewItemEvent.Set();
count++;
}
}
}
Console.WriteLine("Producer thread: produced {0} items", count);
}
private Queue_queue;
private SyncEvents _syncEvents;
}
public class Consumer
{
public Consumer(Queueq, SyncEvents e)
{
_queue = q;
_syncEvents = e;
}
public void ThreadRun()
{
int count = 0;
while (WaitHandle.WaitAny(_syncEvents.EventArray) != 1)
{
lock (((ICollection)_queue).SyncRoot)

{
int item = _queue.Dequeue();
}
count++;
}
Console.WriteLine("Consumer Thread: consumed {0} items", count);
}
private Queue_queue;
private SyncEvents _syncEvents;
}
public class ThreadSyncSample
{
private static void ShowQueueContents(Queueq)
{
lock (((ICollection)q).SyncRoot)
{
foreach (int i in q)
{
Console.Write("{0} ", i);
}
}
Console.WriteLine();
}
static void Main()
{
SyncEvents syncEvents = new SyncEvents();
Queuequeue = new Queue();
Console.WriteLine("Configuring worker threads...");
Producer producer = new Producer(queue, syncEvents);
Consumer consumer = new Consumer(queue, syncEvents);
Thread producerThread = new Thread(producer.ThreadRun);
Thread consumerThread = new Thread(consumer.ThreadRun);
Console.WriteLine("Launching producer and consumer threads...");
producerThread.Start();
consumerThread.Start();
for (int i = 0; i < 4; i++)
{
Thread.Sleep(2500);
ShowQueueContents(queue);
}
Console.WriteLine("Signaling threads to terminate...");
syncEvents.ExitThreadEvent.Set();
Console.WriteLine("main thread waiting for threads to finish...");
producerThread.Join();
consumerThread.Join();
}
}

PROGRAM # 7:- Program for hamming code generation for error detection/correction
#include<stdlib.h>
#include<stdio.h>
char data[5];
int encoded[8], edata[7], syndrome[3];
int hmatrix[3][7]= { 1,0,0,0,1,1,1,
0,1,0,1,0,1,1,
0,0,1,1,1,0,1};
char gmatrix[4][8]={ "0111000", "1010100", "1100010", "1110001"};
int main()
{
int i,j;
system("clear");
printf("Hamming Code --- Encoding\n");
printf("Enter 4 bit data : ");
scanf("%s",data);
printf("Generator Matrix\n");
for(i=0;i<4;i++)
printf("\t %s \n",gmatrix[i]);
printf("Encoded Data : ");
for(i=0;i<7;i++)
{
for(j=0;j<4;j++)
encoded[i]+=((data[j]- '0')*(gmatrix[j][i]- '0'));
encoded[i]=encoded[i]%2;
printf("%d",encoded[i]);
}
printf("\nHamming code --- Decoding\n");
printf("Enter Encoded bits as received : ");
for(i=0;i<7;i++)
scanf("%d",&edata[i]);
for(i=0;i<3;i++)
{
for(j=0;j<7;j++)
syndrome[i]=syndrome[i]+(edata[j]*hmatrix[i][j]);
syndrome[i]=syndrome[i]%2;
}
for(j=0;j<7;j++)
if ((syndrome[0]==hmatrix[0][j])&&(syndrome[1]==hmatrix[1][j])&&
(syndrome[2]==hmatrix[2][j]))
break;
if(j==7)
printf("Data is error free!!\n");
else {
printf("Error received at bit number %d of the data\n",j+1);
edata[j]=!edata[j];printf("The Correct data Should be : ");
for(i=0;i<7;i++) printf(" %d ",edata[i]);
}
}
Output:
[root@localhost nwcn]# cc 7.c
[root@localhost nwcn]# ./a.out

Hamming Code --- Encoding


Enter 4 bit data : 1001
Generator Matrix
0111000
1010100
1100010
1110001
Encoded Data : 1001001
Hamming code --- Decoding
Enter Encoded bits as received : 1
001001
Data is error free!!
[root@localhost nwcn]# cc 7.c
[root@localhost nwcn]# ./a.out
Hamming Code --- Encoding
Enter 4 bit data : 1011
Generator Matrix
0111000
1010100
1100010
1110001
Encoded Data : 0101011
Hamming code --- Decoding
Enter Encoded bits as received : 0 1 0 1 0 1 0
Error received at bit number 7 of the data
The Correct data Should be : 0 1 0 1 0 1 1
Program # 8: Program for congestion control using Leaky Bucket algorithm.
#include<stdio.h>
int rand(int a)
{
int rn=(random()%10)%a;
return rn==0?1:rn;
}
int main()
{
int packet_sz[5],i,clk,b_size,o_rate,p_sz_rm=0,p_sz,p_time;
for(i=0;i<5;++i)
packet_sz[i]=rand(6)*10;
for(i=0;i<5;++i)
printf("packet[%d]:%d bytes\t",i,packet_sz[i]);
printf("\nEnter the Output rate:");
scanf("%d",&o_rate);
printf("Enter the Bucket Size:");
scanf("%d",&b_size);
for(i=0; i<5; ++i)
{
if((packet_sz[i]+p_sz_rm) > b_size)
if(packet_sz[i] > b_size)
printf("\n\nIncomming packet size (%d) is Greater than bucket
capacity-PACKET REJECTED",packet_sz[i]);

else
printf("\n\nBucket capacity exceeded-REJECTED!!");
else
{
p_sz_rm+=packet_sz[i];
printf("\n\nIncomming Packet size: %d",packet_sz[i]);
printf("\nBytes remaining to Transmit: %d",p_sz_rm);
p_time = rand(4)*10;
printf("\nTime left for transmission: %d units",p_time);
for(clk=10; clk<=p_time; clk+=10)
{
sleep(1);
if(p_sz_rm)
{
if(p_sz_rm <= o_rate)
printf("\n Packet of size %d
Transmitted",p_sz_rm),
p_sz_rm=0;
else
printf("\n Packet of size %d Transmitted",o_rate),
p_sz_rm -= o_rate;
printf("----Bytes Remaining after Transmission:
%d",p_sz_rm);}
else
printf("\n No packets to transmit!!");
printf(" Time Left:%d",p_time-clk);
}
}
}
}
OUTPUT:
[root@localhost nwcn]# vi 8.c
[root@localhost nwcn]# cc 8.c
[root@localhost nwcn]# ./a.out
packet[0]:30 bytes
packet[4]:30 bytes
packet[1]:10 bytes
packet[2]:10 bytes
packet[3]:50 bytes
Enter the Output rate:20
Enter the Bucket Size:50
Incomming Packet size: 30
Bytes remaining to Transmit: 30
Time left for transmission: 10 units
Packet of size 20 Transmitted----Bytes Remaining after Transmission: 10 Time Left:0
Incomming Packet size: 10
Bytes remaining to Transmit: 20
Time left for transmission: 20 units
Packet of size 20 Transmitted----Bytes Remaining after Transmission: 0 Time Left:10
No packets to transmit!! Time Left:0
Incomming Packet size: 10
Bytes remaining to Transmit: 10

Time left for transmission: 20 units


Packet of size 10 Transmitted----Bytes Remaining after Transmission: 0 Time Left:10
No packets to transmit!! Time Left:0
Incomming Packet size: 50
Bytes remaining to Transmit: 50
Time left for transmission: 10 units
Packet of size 20 Transmitted----Bytes Remaining after Transmission: 30 Time Left:0
Bucket capacity exceeded-REJECTED!!

Set-1
1. Write a program for frame sorting technique used in buffers.
Answer#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct frame
{
int fslno;
char finfo[20];
};
struct frame arr[10]; int n; void sort()
{
int i,j,ex;
struct frame temp;
for(i=0;i<n;i++)
{
ex=0;
for(j=0;j<n-i-1;j++)
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
ex++;
}
if(ex==0) break
}
}
void main()
{
int i;
clrscr();
printf(\n Enter the number of frames \n);
scanf(%d,&n);
for(i=0;i<n;i++)
{
arr[i].fslno=random(50);
printf(\n Enter the frame contents for sequence number %d\n,arr[i].fslno);
scanf(%s,arr[i].finfo);

}
sort();
printf(\n The frames in sequence \n);
for(i=0;i<n;i++)
printf(\n %d\t%s \n,arr[i].fslno,arr[i].finfo);
getch(); }
-----------------2. Write a program in C# using command line arguments to display a welcome message.
The message has to be supplied as input in the command line.
Answerusing System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
args = new string[1];
Console.Write(Enter Message Here: );
args[0] = Console.ReadLine();
Console.WriteLine(\n + args[0]);
Console.ReadLine();
}
}
}
-----------------Set -2
1. Write a program for distance vector algorithm to find suitable path for transmission.
Answer#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 adjecency 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(\nNode\tvia node\tdistance\tnumber of hops\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);
}
}
}
-----------------2. Write a program to demonstrate the usage of threads in C#.
Answerusing System;
using System.Threading;
namespace ConsoleApplication4
{
class Program
{

static void Main()


{
Thread thread1 = new Thread(new ThreadStart(A));
Thread thread2 = new Thread(new ThreadStart(B));
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
}
static void A()
{
Thread.Sleep(100);
Console.WriteLine(A);
}
static void B()
{
Thread.Sleep(1000);
Console.WriteLine(B);
}
}
}
-----------------Set -3
1. Write a program for simple RSA/DES algorithm to encrypt and decrypt the data.
Answer#include<stdio.h>
#include<math.h>
double min(double x, double y)
{
return(x<y?x:y);
}
double max(double x,double y)
{
return(x>y?x:y);
}
double gcd(double x,double y)
{
if(x==y)
return(x);
else
return(gcd(min(x,y),max(x,y)-min(x,y)));
}
long double modexp(long double a,long double x,long double n)
{
long double r=1;
while(x>0)
{
if ((int)(fmodl(x,2))==1)

{
r=fmodl((r*a),n);
}
a=fmodl((a*a),n);
x/=2;
}
return(r);
}
int main()
{
long double p,q,phi,n,e,d,cp,cq,dp,dq,mp,mq,sp,sq,rp,rq,qInv,h;
long double ms,es,ds;
do
{
printf(\n Enter prime numbers p and q:);
scanf( %Lf %Lf,&p,&q);
}
while(p==q);
n=p*q;
phi=(p-1)*(q-1);
do
{
printf(\n Enter prime value of e:);
scanf( %Lf,&e);
}
while((gcd(e,phi)!=1)&&e>phi);
for(d=1;d<phi;++d)
{
if(fmod((e*d),phi)==1)
break;
}
printf(\n D within main = %Lf,d);
printf(\n Enter the message:);
scanf( %Lf,&ms);
es=modexp(ms,e,n);
ds=modexp(es,d,n);
printf(\n Original Message : %Lf,ms);
printf(\n Encrypted Message : %Lf,es);
printf(\n Decrypted Message : %Lf\n,ds);
return(0);
}
-----------------2. Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling.
Answerusing System;
namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)


{
int i, j, divide = 0;
i = 10;
j = 0;
try
{
divide = i / j;
}
catch (DivideByZeroException)
{
divide = 0;
}
finally
{
Console.WriteLine(divide);
}
Console.ReadLine();
}
}
}
-----------------Set-4
1. Write a program for Spanning Tree Algorithm (PRIM) to find loop less path.
Answer#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
#define INFINITY 999
#include<conio.h>
enum boolean {FALSE,TRUE};
void prim(int c[][MAX],int t[MAX],int n);
int mincost=0;
int main()
{
int n,c[MAX][MAX],t[2*(MAX-1)];
int i,j;
clrscr();
printf(\nTo find min path spanning tree);
printf(\nEnter no of nodes:);
scanf(%d,&n);
printf(\nEnter the cost adjacency matrix);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf(%d,&c[i][j]);
prim(c,t,n);
for(i=0;i<2*(n-1);i+=2)

printf(\n(%d %d),t[i]+1,t[i+1]+1);
printf(\nmincost=%d,mincost);
getch();
return 0;
}
//using prims algorithm for finding shortest path
void prim(int c[][MAX],int t[MAX],int n)
{
int i,j;
enum boolean v[MAX];
int k,s,min,v1,v2;
for(i=0;i<n;i++)
v[i]=FALSE;
v[0]=TRUE;
k=0;
t[k]=1;
s=0;
k++;
while(k<n)
{
min=INFINITY;
for(i=0;i<n;i++)
for(j=1;j<n;j++)
if(v[i]==TRUE && v[j]==FALSE && c[i][j]<min)
{
min=c[i][j];
v1=i;
v2=j;
}
mincost=mincost+min;
if(min==INFINITY)
{
printf(graph disconnected);
exit(0);
}
v[v2]=TRUE;
k++;
t[s++]=v1;
t[s++]=v2;
}
}
-----------------2. Write a program in C# to demonstrate operator overloading.
Answerusing System;
namespace ConsoleApplication4
{
class Program

{
static void Main()
{
Widget w = new Widget();
w++;
Console.WriteLine(w._value);
w++;
Console.WriteLine(w._value);
Widget g = new Widget();
g++;
Console.WriteLine(g._value);
Widget t = w + g;
Console.WriteLine(t._value);
}
}
class Widget
{
public int _value;
public static Widget operator +(Widget a, Widget b)
{
Widget widget = new Widget();
widget._value = a._value + b._value;
return widget;
}
public static Widget operator ++(Widget w)
{
w._value++;
return w;
}
}
}
-----------------Set -5
1. Write a program in C# to sort the students list based on their names. The students list is stored in a
string array.
Answerusing System;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string[] students = { Vikash, Bhushan, Deepak, Tapas, Suresh, Avinash };
Array arr = students;
Array.Sort(arr);
foreach (string item in arr)
{

Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
2. Write a C# Program to demonstrate use of Virtual and override Key words in C#.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ChildClass cc = new ChildClass();
cc.Display();
Console.ReadLine();
}
}
class ParentClass
{
public virtual void Display()
{
Console.WriteLine(Base Class);
}
}
class ChildClass:ParentClass
{
public override void Display()
{
base.Display();
Console.WriteLine(Derived Class);
}
}
}
-----------------Set -6
1. Write a program in C# , create a class called rectangle which consists of members side_1, side_2 and
displayArea(). Write another class square which inherits class rectangle. Take side of square as input
and display the area of square on console.
Answerusing System;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)

{
square sq = new square();
sq.displayArea();
Console.ReadLine();
}
}
class rectangle
{
public int side_1 = 10;
public int side_2 = 20;
public virtual void displayArea()
{
Console.WriteLine(Area of Rectangle is: + side_1 * side_2);
}
}
class square:rectangle
{
public override void displayArea()
{
Console.WriteLine(Area of Square is: + side_1 * side_1);
}
}
}
-----------------2. Write a program to multiply two matrices.
Answer#include<stdio.h>
int main(){
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
printf(\nEnter the row and column of first matrix);
scanf(%d %d,&m,&n);
printf(\nEnter the row and column of second matrix);
scanf(%d %d,&o,&p);
if(n!=o){
printf(Matrix mutiplication is not possible);
printf(\nColumn of first matrix must be same as row of second matrix);
}
else{
printf(\nEnter the First matrix->);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(%d,&a[i][j]);
printf(\nEnter the Second matrix->);
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf(%d,&b[i][j]);
printf(\nThe First matrix is\n);
for(i=0;i<m;i++){
printf(\n);

for(j=0;j<n;j++){
printf(%d\t,a[i][j]);
}
}
printf(\nThe Second matrix is\n);
for(i=0;i<o;i++){
printf(\n);
for(j=0;j<p;j++){
printf(%d\t,b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++){ //row of first matrix
for(j=0;j<p;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf(\nThe multiplication of two matrix is\n);
for(i=0;i<m;i++){
printf(\n);
for(j=0;j<p;j++)
{
printf(%d\t,c[i][j]);
}
}
return 0;
}
-----------------Set -7
1. Write a C# program to demonstrate handling of server control programs.
AnswerIn order to create new ASP.NET Server Control, use File New Project from Visual Studio 2008
menu to open the New Project dialog box. From the Web project type, choose ASP.NET Server
Control.
Code Behind File For ServerControl1.cs :using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

using System.Web.UI.WebControls;
namespace FirstServerControl
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class ServerControl1 : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? [" + this.ID + "] : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
}
}
-----------------2. Write a program to display a message Welcome to ASP.NET 8 times in increasing order of their font
size using ASP.NET.
Answer<%@ Page language=c# Codebehind=WebForm1.aspx.cs
AutoEventWireup=false Inherits=WebApplication9.WebForm1? %>
<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN >
<html>
<head>
<title>WebForm1</title>
<meta name=GENERATOR Content=Microsoft Visual Studio .NET 7.1?>
<meta name=CODE_LANGUAGE Content=C#>
<meta name=vs_defaultClientScript content=JavaScript>
<meta name=vs_targetSchema
content=http://schemas.microsoft.com/intellisense/ie5?>
</head>
<body MS_POSITIONING=GridLayout>
<form id=Form2? method=post runat=server>
<%for(int i=0;i<8;i++){%>
<font size=<%=i%>>Welcome to ASP.NET CHANDU</font><br/></font>

<%}%>
</form>
</body>
</html>

1. Write a program for error detecting code using CRC-CCITT (16bit)


#include<stdio.h>
char m[50],g[50],r[50],q[50],temp[50];
void caltrans(int);
void crc(int);
void calram();
void shiftl();
int main()
{
int n,i=0;
char ch,flag=0;
printf("Enter the frame bits:");
while((ch=getc(stdin))!='\n')
m[i++]=ch;
n=i;
for(i=0;i<16;i++)
m[n++]='0';
m[n]='\0';
printf("Message after appending 16 zeros:%s",m);
for(i=0;i<=16;i++)
g[i]='0';
g[0]=g[4]=g[11]=g[16]='1';g[17]='\0';
printf("\ngenerator:%s\n",g);
crc(n);
printf("\n\nquotient:%s",q);
caltrans(n);
printf("\ntransmitted frame:%s",m);
printf("\nEnter transmitted freme:");
scanf("\n%s",m);printf("CRC checking\n");
crc(n);
printf("\n\nlast remainder:%s",r);
for(i=0;i<16;i++)
if(r[i]!='0')
flag=1;
else
continue;
if(flag==1)
printf("Error during transmission");
else
printf("\n\nReceived freme is correct");
}

void crc(int n)
{
int i,j;
for(i=0;i<n;i++)
temp[i]=m[i];
for(i=0;i<16;i++)
r[i]=m[i];
printf("\nintermediate remainder\n");
for(i=0;i<n-16;i++)
{
if(r[0]=='1')
{
q[i]='1';
calram();
}
else
{
q[i]='0';shiftl();
}
r[16]=m[17+i];
r[17]='\0';
printf("\nremainder %d:%s",i+1,r);
for(j=0;j<=17;j++)
temp[j]=r[j];
}
q[n-16]='\0';
}
void calram()
{
int i,j;
for(i=1;i<=16;i++)
r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48;
}
void shiftl()
{
int i;
for(i=1;i<=16;i++)
r[i-1]=r[i];
}
void caltrans(int n)
{
int i,k=0;
for(i=n-16;i<n;i++)
m[i]=((int)m[i]-48)^((int)r[k++]-48)+48;
m[i]='\0';
}OUTPUT:[root@localhost nwcn]# vi 1.c
[root@localhost nwcn]# cc 1.c
[root@localhost nwcn]# ./a.out
Enter the binary data

1011
The msg before adding checksum:
10110000000000000000
The checksum calculated:
1011000101101011
The code word is:10111011000101101011
Enter the transmitted code word
10111011000101101011
Received msg:10111011000101101011
The checksum is:0000000000000000
No error in the msg
[root@localhost nwcn]# cc 1.c
[root@localhost nwcn]# ./a.out
Enter the binary data
1101
The msg before adding checksum:
11010000000000000000
The checksum calculated:
1101000110101101
The code word is:11011101000110101101
Enter the transmitted code word
10111101000110101101
Received msg:10111101000110101101
The checksum is:0110000011000110
Error in the msgPROGRAM # 2: Frame sorting technique used in buffers.
#include<stdio.h>
#include<string.h>
#define FRAM_TXT_SIZ 3
#define MAX_NOF_FRAM 127
char str[FRAM_TXT_SIZ*MAX_NOF_FRAM];
struct frame
// structure maintained to hold frames
{
char text[FRAM_TXT_SIZ];
int seq_no;
}fr[MAX_NOF_FRAM], shuf_ary[MAX_NOF_FRAM];
int assign_seq_no()
{
int k=0,i,j;
//function which splits message
//into frames and assigns sequence no
for(i=0; i < strlen(str); k++)
{
fr[k].seq_no = k;
for(j=0; j < FRAM_TXT_SIZ && str[i]!='\0'; j++)
fr[k].text[j] = str[i++];
}
printf("\nAfter assigning sequence numbers:\n");
for(i=0; i < k; i++)
printf("%d:%s ",i,fr[i].text);

return k;
//k gives no of frames
}
void generate(int *random_ary, const int limit)
{
int r, i=0, j;
while(i < limit)
{
r = random() % limit;
for(j=0; j < i; j++)
if( random_ary[j] == r )
break;
if( i==j )
random_ary[i++] = r;
}
}
void shuffle( const int no_frames )
{
int i, k=0, random_ary[no_frames];
//generate array of random nos
// function shuffles the framesgenerate(random_ary, no_frames);
for(i=0; i < no_frames; i++)
shuf_ary[i] = fr[random_ary[i]];
printf("\n\nAFTER SHUFFLING:\n");
for(i=0; i < no_frames; i++)
printf("%d:%s ",shuf_ary[i].seq_no,shuf_ary[i].text);
}
void sort(const int no_frames)
// sorts the frames
{
int i,j,flag=1;
struct frame hold;
for(i=0; i < no_frames-1 && flag==1; i++) // search for frames in sequence
{
flag=0;
for(j=0; j < no_frames-1-i; j++) //(based on seq no.) and display
if(shuf_ary[j].seq_no > shuf_ary[j+1].seq_no)
{
hold = shuf_ary[j];
shuf_ary[j] = shuf_ary[j+1];
shuf_ary[j+1] = hold;
flag=1;
}
}
}
int main()
{
int no_frames,i;
printf("Enter the message: ");
gets(str);

no_frames = assign_seq_no();
shuffle(no_frames);
sort(no_frames);
printf("\n\nAFTER SORTING\n");
for(i=0;i<no_frames;i++)
printf("%s",shuf_ary[i].text);
printf("\n\n");
}
OUTPUT:
[root@localhost nwcn]# ./a.out
Enter the message: Welcome To Acharya Institute of Technology
After assigning sequence numbers:0:Wel 1:com 2:e T 3:o A 4:cha 5:rya 6: In 7:sti 8:tut 9:e o 10:f T
11:ech 12:nol 13:ogy
AFTER SHUFFLING:
1:com 4:cha 9:e o 5:rya 3:o A 10:f T 2:e T 6: In 11:ech 13:ogy 0:Wel 8:tut 12:nol 7:sti
AFTER SORTING
Welcome To Acharya Institute of Technology3. Write a program for distance vector algorithm to
find suitable path for transmission
#include<stdlib.h>
#define nul 1000
#define nodes 10
int no;
struct node
{
int a[nodes][4];
}router[nodes];
void init(int r)
{
int i;
for(i=1;i<=no;i++)
{
router[r].a[i][1]=i;
router[r].a[i][2]=999;
router[r].a[i][3]=nul;
}
router[r].a[r][2]=0;
router[r].a[r][3]=r;
}
void inp(int r)
{int i;
printf("\nEnter dist from the node %d to other nodes",r);
printf("\nPls enter 999 if there is no direct route\n",r);
for(i=1;i<=no;i++)
{
if(i!=r)
{
printf("\nEnter dist to the node %d:",i);
scanf("%d",&router[r].a[i][2]);
router[r].a[i][3]=i;
}

}
}
void display(int r)
{
int i,j;
printf("\n\nThe routing table for node %d is as follows:",r);
for(i=1;i<=no;i++)
{
if(router[r].a[i][2]>=999)
printf("\n\t\t\t %d \t no link \t no hop",router[r].a[i][1]);
else
printf("\n\t\t\t %d \t %d \t\t d",router[r].a[i][1],router[r].a[i][2],router[r].a[i][3]);
}
}
void dv_algo(int r)
{
int i,j,z;
for(i=1;i<=no;i++){
if(router[r].a[i][2]!=999 && router[r].a[i][2]!=0)
{
for(j=1;j<=no;j++)
{
z=router[r].a[i][2]+router[i].a[j][2];
if(router[r].a[j][2]>z)
{
router[r].a[j][2]=z;
router[r].a[j][3]=i;
}
}
}
}
}
int main()
{
int i,j,x,y;
char choice;
printf("Enter the no. of nodes required (less than 10 pls):");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
init(i);
inp(i);
}
printf("\nThe configuration of the nodes after initialization is as follows:");
for(i=1;i<=no;i++)
display(i);
for(i=1;i<=no;i++)dv_algo(i);
printf("\nThe configuration of the nodes after computation of paths is as follows:");
for(i=1;i<=no;i++)
display(i);

while(1)
{
printf("\n\nWanna continue (y/n):");
scanf("%c",&choice);
if(choice=='n')
break;
printf("\nEnter the nodes btn which shortest path is to be found:\n");
scanf("%d %d",&x,&y);
printf("\nThe length of the shortest path is %d",router[x].a[y][2]);
}
}
[root@localhost ~]# ./a.out
Enter the no. of nodes required (less than 10 pls):4
Enter dist from the node 1 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 2:5
Enter dist to the node 3:3Enter dist to the node 4:7
Enter dist from the node 2 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:5
Enter dist to the node 3:999
Enter dist to the node 4:6
Enter dist from the node 3 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:3\
Enter dist to the node 2:
Enter dist to the node 4:
Enter dist from the node 4 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:Enter dist to the node 2:
Enter dist to the node 3:
The configuration of the nodes after initialization is as follows:
The routing table for node 1 is as follows:
101
252
333
474
The routing table for node 2 is as follows:
151
202
3 no link no hop
464
The routing table for node 3 is as follows:
131
2 no link no hop
303
4 no link no hop
The routing table for node 4 is as follows:1 no link no hop
2 no link no hop
3 no link no hop

404
The configuration of the nodes after computation of paths is as follows:
The routing table for node 1 is as follows:
101
252
333
474
The routing table for node 2 is as follows:
151
202
381
464
The routing table for node 3 is as follows:
131
281
303
4 10 1The routing table for node 4 is as follows:
1 no link no hop
2 no link no hop
3 no link no hop
404
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found:
^C
[root@localhost ~]# clear
[root@localhost ~]# ./a.out
Enter the no. of nodes required (less than 10 pls):4
Enter dist from the node 1 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 2:5
Enter dist to the node 3:3
Enter dist to the node 4:7Enter dist from the node 2 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:5
Enter dist to the node 3:999
Enter dist to the node 4:6
Enter dist from the node 3 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:3
Enter dist to the node 2:999
Enter dist to the node 4:2
Enter dist from the node 4 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:7Enter dist to the node 2:6
Enter dist to the node 3:2
The configuration of the nodes after initialization is as follows:
The routing table for node 1 is as follows:
101
252
333

474
The routing table for node 2 is as follows:
151
202
3 no link no hop
464
The routing table for node 3 is as follows:
131
2 no link no hop
3 0 34
2
4
The routing table for node 4 is as follows:
171
262
323
404
The configuration of the nodes after computation of paths is as follows:
The routing table for node 1 is as follows:
101
252
333
453
The routing table for node 2 is as follows:
151
202
381
464
The routing table for node 3 is as follows:
1
3
12 8 1
303
424
The routing table for node 4 is as follows:
153
262
323
404
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found:
14
The length of the shortest path is 7
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found:4. Using TCP or IP sockets write a
client/server program to make client send the name of a
file and server to send back the contents of the requested file if present.
Client Side:
#include<stdio.h>
#include<sys/types.h>

#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{
printf("Err:no port no.\nusage:\n./client portno\n ex:./client 7777\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero((char *)&serv,sizeof(serv));
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_port=htons(portno);
if(connect(sockfd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("server not responding..\n\n\n\ti am to terminate\n");
exit(1);
}
printf("Enter the file with complete path\n");scanf("%s",&buffer);
if(write(sockfd,buffer,strlen(buffer))<0)
printf("Err writing to socket..\n");
bzero(c,2000);
printf("Reading..\n..\n");
if(read(sockfd,c,1999)<0)
printf("error: read error\n");
printf("client: display content of %s\n..\n",buffer);
fputs(c,stdout);
printf("\n..\n");
return 0;
}
Server Side:
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[2000],cc[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{

printf("erroe:no port no\n usage:\n/server port no\n");


exit(1);}
sockfd=socket(AF_INET,SOCK_STREAM,0);
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=INADDR_ANY;
serv.sin_port=htons(portno);
bind(sockfd,(struct sockaddr *)&serv,sizeof(serv));
listen(sockfd,10);
len=sizeof(cli);
printf("serve:\nwaiting for connection\n");
newsockfd=accept(sockfd,(struct sockaddr *)&cli,&len);
bzero(buffer,255);
n=read(newsockfd,buffer,255);
printf("\nserver recv:%s\n",buffer);
if((fd=fopen(buffer,"r"))!=NULL)
{
printf("server:%s found\n opening and reading..\n",buffer);
printf("reading..\n..reading complete");
fgets(cc,2000,fd);
while(!feof(fd))
{
fgets(c,2000,fd);
strcat(cc,c);
}
n=write(newsockfd,cc,strlen(cc));
if(n<0)
printf("error writing to socket");
printf("\ntransfer complete\n");
}
else
{printf("server:file not found\n");
n=write(newsockfd,"file not foung",15);
if(n<0)
printf("error: writing to socket..\n");
}
return 0;
}
Output:
Server part output
[root@localhost ~]# ./a.out 4455
server online
waiting for request...
server:(null) found!
transfering the contentserver:transfer completed
Segmentation fault
[root@localhost ~]#
client part output
[root@localhost ~]# ./a.out 4455
waiting for serverserver is online

client enter the path2.c


File Recieved:Display Contents#include<stdio.h>
struct frame{
int fslno;
char data[5];
}arr[10];
sort(int n)
{
struct frame temp;
int i,j,ex;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
ar5. Implement the above program using as Message queue or FIFOs as IPC channels.
Server Side:
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#define fifo1 "fifo1"
#define fifo2 "fifo2"
int main()
{
char p[100],c[100];
int fd1,fd2,fd,i;
for(i=0;i<100;i++)
c[i]=p[i]='\0';
mknod(fifo1,S_IFIFO|0777,0);
mknod(fifo2,S_IFIFO|0777,0);
printf("server online\n waiting for request...\n");
fd1=open(fifo1,O_RDONLY);
read(fd1,p,100);
if((fd=open(p,O_RDONLY))<0)
{
printf("\n server:file %s not found\n",p);
exit(1);
}
printf("\n server:%s found!\n transfering the content");
read(fd,c,1000);
fd2=open(fifo2,O_WRONLY);
write(fd2,c,strlen(c));
printf("server:transfer completed\n");
}Client Side:
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#define fifo1 "fifo1"
#define fifo2 "fifo2"
int main()

{
char p[100],c[100],ch;
int fd1,fd2,fd,i;
for(i=0;i<100;i++)
c[i]=p[i]='\0';
mknod(fifo1,S_IFIFO|0777,0);
mknod(fifo2,S_IFIFO|0777,0);
printf("waiting for server");
fd1=open(fifo1,O_WRONLY);
printf("server is online\n client enter the path");
i=0;
while(1)
{
ch=getchar();
if(ch=='\n')
break;
p[i++]=ch;
}
p[i]='\0';
write(fd1,p,strlen(p));
if((fd=open(p,O_RDONLY))<0)
{printf("error file not found!enter valid path name\n\n",p);
exit(1);
}
fd2=open(fifo2,O_RDONLY);
read(fd2,c,1000);
printf("File Recieved:Display Contents");
fputs(c,stdout);
printf("\n\n");
}
Output:
Server part output
[root@localhost ~]# ./a.out
server online
waiting for request...
server:(null) found!
transfering the contentserver:transfer completed
Segmentation fault
[root@localhost ~]#
client part output
[root@localhost ~]# ./a.out
waiting for serverserver is online
client enter the path2.c
File Recieved:Display Contents#include<stdio.h>
struct frame{
int fslno;
char data[5];
}arr[10];
sort(int n)
{

struct frame temp;


int i,j,ex;
for(i=0;i<n-1;i++)
{for(j=0;j<n-i-1;j++)
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
ar6. Write a program for simple RSA algorithm to encrypt and decrypt the data
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
unsigned long modexp(unsigned long msg,unsigned long exp,unsigned long n)
{
unsigned long i,k=1;
for(i=0;i<exp;i++)
k=(k*msg)%n;
return k;
}
int main()
{
unsigned long p,q,e,d,n,z,i,C,M;
int len;
char data[100];
system("clear");
printf("Enter the value of P and Q (such that p*q>255 and p not equal to q)\n");
scanf("%lu%lu",&p,&q);
n=p*q;
z=(p-1)*(q-1);
for(i=1;i<z;i++)
{
if((z%i)==0)
continue;
else
break;}
e=i;
printf("\nEncryption key is :%lu",e);
for(i=1;i<z;i++)
if(((e*i-1)%z)==0)
break;
d=i;
printf("\ndecryption key is :%lu",d);
printf("\npls enter the message:");
scanf("%s",data);
len=strlen(data);
for(i=0;i<len;i++)
{
M=(unsigned long)data[i];
C=modexp(M,e,n);
printf("\nencrypted value and its char representation:%lu\t%c\n",C,C);

M=modexp(C,d,n);
printf("\ndecrypted value and its char representation:%lu\t%c\n",M,M);
}
return 0;
}
Output:
[root@localhost ~]# ./a.outEnter the value of P and Q (such that p*q>255 and p not equal to q)
31
37
Encryption key is :7
decryption key is :463
pls enter the message:computer
encrypted value and its char representation:657
decrypted value and its char representation:99 c
encrypted value and its char representation:629 udecrypted value and its char representation:111
o
encrypted value and its char representation:1093
decrypted value and its char representation:109 m
encrypted value and its char representation:38 &
decrypted value and its char representation:112 p
encrypted value and its char representation:623 o
decrypted value and its char representation:117 u
encrypted value and its char representation:277 #
decrypted value and its char representation:116 t
encrypted value and its char representation:64 @
decrypted value and its char representation:101 e
encrypted value and its char representation:1003
Edecrypted value and its char representation:114 r
put:PROGRAM # 7:- Program for hamming code generation for error detection/correction
#include<stdlib.h>
#include<stdio.h>
char data[5];
int encoded[8], edata[7], syndrome[3];
int hmatrix[3][7]= { 1,0,0,0,1,1,1,
0,1,0,1,0,1,1,
0,0,1,1,1,0,1};
char gmatrix[4][8]={ "0111000", "1010100", "1100010", "1110001"};
int main()
{
int i,j;
system("clear");
printf("Hamming Code --- Encoding\n");
printf("Enter 4 bit data : ");
scanf("%s",data);
printf("Generator Matrix\n");
for(i=0;i<4;i++)
printf("\t %s \n",gmatrix[i]);
printf("Encoded Data : ");
for(i=0;i<7;i++)
{

for(j=0;j<4;j++)
encoded[i]+=((data[j]- '0')*(gmatrix[j][i]- '0'));
encoded[i]=encoded[i]%2;
printf("%d",encoded[i]);
}
printf("\nHamming code --- Decoding\n");
printf("Enter Encoded bits as received : ");
for(i=0;i<7;i++)
scanf("%d",&edata[i]);
for(i=0;i<3;i++)
{
for(j=0;j<7;j++)
syndrome[i]=syndrome[i]+(edata[j]*hmatrix[i][j]);
syndrome[i]=syndrome[i]%2;
}
for(j=0;j<7;j++)
if ((syndrome[0]==hmatrix[0][j])&&(syndrome[1]==hmatrix[1][j])&&
(syndrome[2]==hmatrix[2][j]))
break;
if(j==7)
printf("Data is error free!!\n");
else {
printf("Error received at bit number %d of the data\n",j+1);
edata[j]=!edata[j];printf("The Correct data Should be : ");
for(i=0;i<7;i++) printf(" %d ",edata[i]);
}
}
Output:
[root@localhost nwcn]# cc 7.c
[root@localhost nwcn]# ./a.out
Hamming Code --- Encoding
Enter 4 bit data : 1001
Generator Matrix
0111000
1010100
1100010
1110001
Encoded Data : 1001001
Hamming code --- Decoding
Enter Encoded bits as received : 1
001001
Data is error free!!
[root@localhost nwcn]# cc 7.c
[root@localhost nwcn]# ./a.out
Hamming Code --- Encoding
Enter 4 bit data : 1011
Generator Matrix
0111000
1010100
1100010

1110001
Encoded Data : 0101011
Hamming code --- Decoding
Enter Encoded bits as received : 0 1 0 1 0 1 0
Error received at bit number 7 of the data
The Correct data Should be : 0 1 0 1 0 1 1Program # 8: Program for congestion control using Leaky
Bucket algorithm.
#include<stdio.h>
int rand(int a)
{
int rn=(random()%10)%a;
return rn==0?1:rn;
}
int main()
{
int packet_sz[5],i,clk,b_size,o_rate,p_sz_rm=0,p_sz,p_time;
for(i=0;i<5;++i)
packet_sz[i]=rand(6)*10;
for(i=0;i<5;++i)
printf("packet[%d]:%d bytes\t",i,packet_sz[i]);
printf("\nEnter the Output rate:");
scanf("%d",&o_rate);
printf("Enter the Bucket Size:");
scanf("%d",&b_size);
for(i=0; i<5; ++i)
{
if((packet_sz[i]+p_sz_rm) > b_size)
if(packet_sz[i] > b_size)
printf("\n\nIncomming packet size (%d) is Greater than bucket
capacity-PACKET REJECTED",packet_sz[i]);
else
printf("\n\nBucket capacity exceeded-REJECTED!!");
else
{
p_sz_rm+=packet_sz[i];
printf("\n\nIncomming Packet size: %d",packet_sz[i]);
printf("\nBytes remaining to Transmit: %d",p_sz_rm);
p_time = rand(4)*10;
printf("\nTime left for transmission: %d units",p_time);
for(clk=10; clk<=p_time; clk+=10)
{
sleep(1);
if(p_sz_rm)
{
if(p_sz_rm <= o_rate)
printf("\n Packet of size %d
Transmitted",p_sz_rm),
p_sz_rm=0;
else
printf("\n Packet of size %d Transmitted",o_rate),

p_sz_rm -= o_rate;
printf("----Bytes Remaining after Transmission:
%d",p_sz_rm);}
else
printf("\n No packets to transmit!!");
printf(" Time Left:%d",p_time-clk);
}
}
}
}
OUTPUT:
[root@localhost nwcn]# vi 8.c
[root@localhost nwcn]# cc 8.c
[root@localhost nwcn]# ./a.out
packet[0]:30 bytes
packet[4]:30 bytes
packet[1]:10 bytes
packet[2]:10 bytes
packet[3]:50 bytes
Enter the Output rate:20
Enter the Bucket Size:50
Incomming Packet size: 30
Bytes remaining to Transmit: 30
Time left for transmission: 10 units
Packet of size 20 Transmitted----Bytes Remaining after Transmission: 10 Time Left:0
Incomming Packet size: 10
Bytes remaining to Transmit: 20
Time left for transmission: 20 units
Packet of size 20 Transmitted----Bytes Remaining after Transmission: 0 Time Left:10
No packets to transmit!! Time Left:0
Incomming Packet size: 10
Bytes remaining to Transmit: 10
Time left for transmission: 20 units
Packet of size 10 Transmitted----Bytes Remaining after Transmission: 0 Time Left:10
No packets to transmit!! Time Left:0
Incomming Packet size: 50
Bytes remaining to Transmit: 50
Time left for transmission: 10 units
Packet of size 20 Transmitted----Bytes Remaining after Transmission: 30 Time Left:0
Bucket capacity exceeded-REJECTED!!