Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
40 views369 pages

NS Lab Final

Uploaded by

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

NS Lab Final

Uploaded by

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

Figure 1

S.NO DATE TITLE PG. MARK SIGN


NO

8 Implementation of Data
Encryption and Decryption using
RSA Algorithm(Rivest, Shamir and
Adleman)
9 Implementation Client Server
model using FTP Protocol

10A Network Topology- Star


10B Network Topology- Bus
10C Network Topology- Ring
11 Implementation and perform the
operation of CSMA/CD and
CSMA/CA
EX.NO:1A IMPLEMENTATION OF DATALINK LAYER FRAMING METHODS
DATE: BIT STUFFING

AIM:
Write a program for Datalink layer framing methods by Bit Stuffing using C-
program.
APPARATUS REQUIERD:
 C -editor
 Standalone desktop.
PROCEDURE:
1. Start the program.
2. Open C-editor.
3. Type the C program.
4. Save the program with file name ext .c
5. Run the program.
6. If any error occurs in the program correct the error and run it again.
7. Enter the data of 4 bit size message bit.
8. Check the entered data.
9. Stop the program.
PROGRAM FOR BIT STUFFING METHOD:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void charc(void);
void main()
{
int choice;
while(1)
{
printf("\n\n\n1.character stuffing"); printf("\n\n2.exit");
printf("\n\n\n enter choice"); scanf("%d",& choice);
printf("%d", choice); if(choice>2)
printf("\n\n invalid option. .. please renter");
switch(choice)
{
case 1:
charc ();
break;
case 2:
exit(0);
}
}
}
void charc(void)
{
char c[50],d[50],t[50];
int i,m,j;
clrscr();
printf("enter the number of characters\n");
scanf("%d",&m);
printf("\n enter the characters\n");
for(i=0;i<m+1;i++)
scanf("%c",&c[i]);
printf("\n original data\n");
for(i=0;i<m+1;i++)
printf("%c",c[i]);
d[0]='d';
d[1]='l';
d[2]='e';
d[3]='s';
d[4]='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
{
d[j]='d';
j++;
d[j]='l';
j++;
d[j]='e';
j++;
m=m+3;
}
d[j]=c[i];
}
m=m+6;
m++;
d[m]='d';
m++;
d[m]='l';
m++;
d[m]='e';
m++;
d[m]='e';
m++;
d[m]='t';
m++;
d[m]='x';
m++;
printf("\n\n transmitted data: \n");
for(i=0;i<m; i ++)
{
printf ("%c",d[i]);
}
for(i=6,j=0;i<m-6;i++,j++)
{
if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e')
i=i+3;
t[j]=d[i];
}
printf("\n\n received data:");
for(i=0;i<j; i++)
{
printf("%c", t[i]);
}
}
SAMPLE OUTPUT:
RESULT:
Thus the program for implementation of a data link control for bit stuffing
method is executed and output is verified successfully.
EX.NO:1B IMPLEMENTATION OF DATALINK LAYER FRAMING METHODS
DATE: CHARACTER STUFFING

AIM:
Write a program for Datalink layer framing methods by Character Stuffing using C-
program.
APPARATUS REQUIERD:
 C -editor
 Standalone desktop.
PROCEDURE:
1. Start the program.
2. Open C-editor.
3. Type the C program.
4. Save the program with file name ext .c
5. Run the program.
6. If any error occurs in the program correct the error and run it again.
7. Enter the data of 4 bit size message bit.
8. Check the entered data.
9. Stop the program.
PROGRAM FOR CHARACTER STUFFING METHOD:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>

void main() {
int i = 0, j = 0, n, pos;
char a[20], b[50], ch;
printf("Enter string\n");
scanf("%s", & a);
n = strlen(a);
printf("Enter position\n");
scanf("%d", & pos);
if (pos > n) {
printf("invalid position, Enter again :");
scanf("%d", & pos);
}
printf("Enter the character\n");
ch = getche();
b[0] = 'd';
b[1] = 'l';
b[2] = 'e';
b[3] = 's';
b[4] = 't';
b[5] = 'x';
j = 6;
while (i < n) {
if (i == pos - 1) {
b[j] = 'd';
b[j + 1] = 'l';
b[j + 2] = 'e';
b[j + 3] = ch;
b[j + 4] = 'd';
b[j + 5] = 'l';
b[j + 6] = 'e';
j = j + 7;
}
if (a[i] == 'd' && a[i + 1] == 'l' && a[i + 2] == 'e') {
b[j] = 'd';
b[j + 1] = 'l';
b[j + 2] = 'e';
j = j + 3;
}
b[j] = a[i];
i++;
j++;
}
b[j] = 'd';
b[j + 1] = 'l';
b[j + 2] = 'e';
b[j + 3] = 'e';
b[j + 4] = 't';
b[j + 5] = 'x';
b[j + 6] = '\0';
printf("\nframe after stuffing:\n");
printf("%s", b);
}
SAMPLE OUTPUT:
RESULT:
Thus the program for implementation of a data link control for character
stuffing method is executed and output is verified successfully.
EX.NO:2A IMPLEMENTATION OF ERROR DETECTION / ERROR
DATE: CORRECTION TECHNIQUES - LRC

AIM:
Write a program for Error Detection and Error Correction by LRC method using C-
program.
APPARATUS REQUIERD:
 C -editor
 Standalone desktop.
PROCEDURE:
1. Start the program.
2. Open C-editor.
3. Type the C program.
4. Save the program with file name ext .c
5. Run the program.
6. If any error occurs in the program correct the error and run it again.
7. Enter the data of 4 bit size message bit.
8. Check the entered data.
9. Stop the program.
PROGRAM FOR LRC METHOD:
#include<stdio.h>
#include<conio.h>
int binary(int);
void parity(int [],int []);

int arr[8],arr1[8],parityarr[8];
char chr;
int go,temp,temp1,i;

void main()
{ char chr1;
clrscr();
go=0;
Sender:
if(go==0)
printf("Enter Data : \n");
printf("\nEnter a character : ");
scanf("%c %c",&chr,&chr1);

temp=chr;
binary(temp);
printf("\n\nAscii value is : %d\n",temp);
printf("\nBinary Form : ");
for(i=7;i>=0;i--)
{
arr1[i]=arr[i];
printf("%d ",arr[i]);
}
printf("\n");

temp1=chr1;
binary(temp1);
printf("\n\nAscii value is : %d\n",temp1);
printf("\nBinary Form : ");
for(i=7;i>=0;i--)
{
printf("%d ",arr[i]);
}
parity(arr,arr1);
for(i=7;i>=0;i--)
{
arr[i]=0;
arr1[i]=0;
}
getch();
}

void parity(int arr[],int arr1[])


{
printf("Receiver Side :\n");
printf("\n\nLRC :\n");
for(i=7;i>=0;i--)
{
printf("%d ",arr[i]);
}
printf("\n");
for(i=7;i>=0;i--)
{
printf("%d ",arr1[i]);
}
printf("\n---------------------\n");
for(i=0;i<8;i++)
{
if(arr[i]==0 && arr1[i]==0 || arr[i]==1 && arr1[i]==1)
{
parityarr[i]=0;
}
else if(arr[i]==1 && arr1[i]==0 || arr[i]==0 && arr1[i]==1)
{
parityarr[i]=1;
}
}
for(i=7;i>=0;i--)
{
printf("%d ",parityarr[i]);
}
}

int binary(int x)
{
int rem;
int ctr=0,i=1;
do
{
rem=x%2;
arr[i]=rem;
if(rem==1)
{
ctr++;
}
x=x/2;
i++;
}
while(x!=0);
if(ctr%2==0)
{
arr[0]=0;
}
else
{
arr[0]=1;
}
return(0);
}
SAMPLING OUTPUT:
RESULT:
EX.NO:2B IMPLEMENTATION OF ERROR DETECTION / ERROR
DATE: CORRECTION TECHNIQUES-CRC
CCCCRCCRCCRC

Thus the Error detection and correction methods were executed and verified
successfully by LRC using c – editor.
AIM:
Write a program for Error Detection and Error Correction by CRC method using C-
program.
APPARATUS REQUIERD:
 C -editor
 Standalone desktop.
PROCEDURE:
1. Start the program.
2. Open C-editor.
3. Type the C program.
4. Save the program with file name ext .c
5. Run the program.
6. If any error occurs in the program correct the error and run it again.
7. Enter the data of 4 bit size message bit.
8. Check the entered data.
9. Stop the program.
PROGRAM FOR CRC:
#include <string.h>
#include <iostream.h>
int crc(char *ip, char *op, char *poly, int mode)
{
strcpy(op, ip);
if (mode) {
for (int i = 1; i < strlen(poly); i++)
strcat(op, "0");
}
/* Perform XOR on the msg with the selected polynomial */
for (int i = 0; i < strlen(ip); i++) {
if (op[i] == '1') {
for (int j = 0; j < strlen(poly); j++) {
if (op[i + j] == poly[j])
op[i + j] = '0';
else
op[i + j] = '1';
}
}
}
/* check for errors. return 0 if error detected */
for (i = 0; i < strlen(op); i++)
if (op[i] == '1')
return 0;
return 1;
}

int main()
{
char ip[50], op[50], recv[50], poly[50];

cout << "Enter the polynomial in binary" << endl;


cin >> poly;
cout << "Enter the input message in binary"<< endl;
cin >> ip;
crc(ip, op, poly, 1);
cout << "The transmitted message is: " << ip << op + strlen(ip) << endl;
cout << "Enter the recevied message in binary" << endl;
cin >> recv;
if (crc(recv, op, poly, 0))
cout << "No error in data" << endl;
else
cout << "Error in data transmission has occurred" << endl;
return 0;
}
SAMPLING OUTPUT:
RESULT:
EX.NO:2C IMPLEMENTATION OF ERROR DETECTION / ERROR CORRECTION
DATE: TECHNIQUES HAMMING METHOD

Thus the Error detection and correction methods were executed and verified
successfully by LRC using c – editor.

AIM:
Write a program for Error Detection and Error Correction by HAMMING method
using C- program.
APPARATUS REQUIERD:
 C -editor
 Standalone desktop.
PROCEDURE:
1. Start the program.
2. Open C-editor.
3. Type the C program.
4. Save the program with file name ext .c
5. Run the program.
6. If any error occurs in the program correct the error and run it again.
7. Enter the data of 4 bit size message bit.
8. Check the entered data.
9. Stop the program.
PROGRAM FOR HAMMING METHOD:
#include<stdio.h>
#include<conio.h>
void main() {
int data[7],rec[7],i,c1,c2,c3,c;
printf ("this works for message of 4bits in size \n enter message bit one by one: ");
scanf ("%d %d %d %d",& data[0],&data[1],&data[2],&data[4]);
data[6]=data[0]^data[2]^data[4];
data[5]=data[0]^data[1]^data[4];
data[3]=data[0]^data[1]^data[2];
printf("\n the encoded bits are given below: \n");
for (i=0;i<7;i++) {
printf("%d ",data[i]);
}
printf("\n enter the received data bits one by one: ");
for (i=0;i<7;i++) {
scanf("%d",& rec[i]);
}
c1=rec[6]^rec[4]^rec[2]^rec[0];
c2=rec[5]^rec[4]^rec[1]^rec[0];
c3=rec[3]^rec[2]^rec[1]^rec[0];
c=c3*4+c2*2+c1 ;
if(c==0) {
printf ("\n congratulations there is no error: ");
} else {
printf("\n error on the position: %d\n the correct message is \n",c);
if(rec[7-c]==0)
rec[7-c]=1;
else
rec[7-c]=0;
for (i=0;i<7;i++) {
}
}
getch();
printf("%d ",rec[i]);
}
SAMPLE OUTPUT:
RESULT:
Thus the Error detection and correction methods were executed and verified
successfully by HAMMING using c – editor.
EX.NO:3 IMPLEMENTATION OF STOP AND WAIT & SLIDING WINDOEW
DATE: WINDOW

AIM:
To write a program for talk commands using socket programming to send and receive
message from client and server using connection oriented service.
APPARATUS REQUIERD:
JDK TOOLKIT 5.0
Standalone desktop
PROCEDURE:
Server:
1. Start the program.
2. Create server and client sockets.
3. Use input streams to get the message from user.
4. Use output stream to send message to the client.
5. Wait for client to display this message and write a new one to be displayed by the
server.
6. Display message given at client using input streams read from socket.
7. If in the message given by server or client, the word “end” is encountered, exit both
the programs.
8. Stop the program.
Client:
1. Start the program.
2. Create a client socket that connects to the required host and port.
3. Using input streams read message given by server and print it.
4. Using input streams, get message from user to be given to the server.
5. Use output streams to write message to the server.
6. Stop the program

PROGRAM FOR STOP AND WAIT PROTOCOL:


SENDER:
import java.io.*;
import java.net.*;

public class Sender {


Socket sender;
ObjectOutputStream out;
ObjectInputStream in;
String packet, ack, str, msg;
int n, i = 0, sequence = 0;

Sender() {
}
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("Waiting for Connection. .. ");
sender = new Socket("localhost", 2004);
sequence = 0;
out = new ObjectOutputStream(sender.getOutputStream());
out.flush();
in = new ObjectInputStream(sender.getInputStream());
str = (String) in.readObject();
System.out.println("reciver > " + str);
System.out.println("Enter the data to send. .. ");
packet = br.readLine();
n = packet.length();
do {
try {
if (i < n) {
msg = String.valueOf(sequence);
msg = msg.concat(packet.substring(i, i + 1));
} else if (i == n) {
msg = "end";
out.writeObject(msg);
break;
}
out.writeObject(msg);
sequence = (sequence == 0) ? 1 : 0;
out.flush();
System.out.println("data sent>" + msg);
ack = (String) in.readObject();
System.out.println("waiting for ack. ... \n\n");
if (ack.equals(String.valueOf(sequence))) {
i++;
System.out.println("receiver > "
+ " packet recieved\n\n");
} else {
System.out.println("Time out resending data ... \n\
n");
sequence = (sequence == 0) ? 1 : 0;
}
} catch (Exception e) {
}
} while (i < n + 1);
System.out.println("All data sent. exiting.");
} catch (Exception e) {
} finally {
try {
in.close();
out.close();
sender.close();
} catch (Exception e) {
}
}
}
public static void main(String args[]) {
Sender s = new Sender();
s.run();
}
}

RECEIVER:
import java.io.*;
import java.net.*;

public class Reciever {


ServerSocket reciever;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String packet, ack, data = "";
int i = 0, sequence = 0;

Reciever() {
}

public void run() {


try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
reciever = new ServerSocket(2004, 10);
System.out.println("waiting for connection...");
connection = reciever.accept();
sequence = 0;
System.out.println("Connection established :");
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
out.writeObject("connected .");
do {
try {
packet = (String) in.readObject();
if (Integer.valueOf(packet.substring(0, 1)).intValue() ==
sequence) {
data += packet.substring(1);
sequence = (sequence == 0) ? 1 : 0;
System.out.println("\n\nreceiver >" +
packet);
} else {
System.out.println("\n\nreceiver >" + packet
+ " duplicate data");
}
if (i < 3) {
out.writeObject(String.valueOf(sequence));
i++;
} else {
out.writeObject(String.valueOf((sequence + 1) %
2));
i = 0;
}
} catch (Exception e) {
}
} while (!packet.equals("end"));
System.out.println("Data recived=" + data);
out.writeObject("connection ended .");
} catch (Exception e) {
} finally {
try {
in.close();
out.close();
reciever.close();
} catch (Exception e) {
}
}
}

public static void main(String args[]) {


Reciever s = new Reciever();
while (true) {
s.run();
}
}
}
SENDER OUTPUT:
Enter the no. of frames: 4
Enter 4 Messages to be send hiii
how r u
i am fine
how is everyone
Acknowledgment received for 4 frames
Do you wants to send some more frames: no
RECEIVER OUTPUT:
The received Frame 0 is : hiii
The received Frame 1 is : how r u
The received Frame 2 is : i am fine
The received Frame 3 is : how is everyone Acknowledgment sent
RESULT:
Thus the Java Socket Program for stop and Wait Protocol executed and output is
verified successfully.
EX.NO:4 IMPLEMENTATION OF GO BACK-N & SELECTIVE REPEAT
DATE: PROTOCOLS

AIM:
Write a program in Java to implement Go Back N algorithm. The Program sends the
frames from the Client to the Server with checking for missing frames via sending an
acknowledgement.
APPARATUS REQUIERD:
JDK TOOLKIT 5.0
Standalone desktop
PROCEDURE:
Server:
1. Start the program.
2. Create server and client sockets.
3. Use input streams to get the message from user.
4. Use output stream to send message to the client.
5. Wait for client to display this message and write a new one to be displayed by the
server.
6. Display message given at client using input streams read from socket.
7. If in the message given by server or client, the word “end” is encountered, exit
both the programs.
8. Stop the program.
Client:
1. Start the program.
2. Create a client socket that connects to the required host and port.
3. Using input streams read message given by server and print it.
4. Using input streams, get message from user to be given to the server.
5. Use output streams to write message to the server.
6. Stop the program
PROGRAM FOR GO BACK-N AND SELECTIVE REPEAT PROTOCOL:
SENDER:
import java.net.*;
import java.io.*;

public class SlideSender {


public static void main(String a[]) throws Exception {
ServerSocket ser = new ServerSocket(10);
Socket s = ser.accept();
DataInputStream in = new DataInputStream(System.in);
DataInputStream in1 = new DataInputStream(s.getInputStream());
String sbuff[] = new String[8];
PrintStream p;
int sptr = 0, sws = 8, nf, ano, i;
String ch;
do {
p = new PrintStream(s.getOutputStream());
System.out.print("Enter the no. of frames : ");
nf = Integer.parseInt(in.readLine());
p.println(nf);
if (nf <= sws - 1) {
System.out.println("Enter " + nf + " Messages to be send\n");
for (i = 1; i <= nf; i++) {
sbuff[sptr] = in.readLine();
p.println(sbuff[sptr]);
sptr = ++sptr % 8;
}
sws -= nf;
System.out.print("Acknowledgment received");
ano = Integer.parseInt(in1.readLine());
System.out.println(" for " + ano + " frames");
sws += nf;
} else {
System.out.println("The no. of frames exceeds window size");
break;
}
System.out.print("\nDo you wants to send some more frames : ");
ch = in.readLine();
p.println(ch);
} while (ch.equals("yes"));
s.close();
}
}

RECEIVER:
import java.net.*;
import java.io.*;

class SlideReceiver {
public static void main(String a[]) throws Exception {
Socket s = new Socket(InetAddress.getLocalHost(), 10);
DataInputStream in = new DataInputStream(s.getInputStream());
PrintStream p = new PrintStream(s.getOutputStream());
int i = 0, rptr = -1, nf, rws = 8;
String rbuf[] = new String[8];
String ch;
System.out.println();
do {
nf = Integer.parseInt(in.readLine());
if (nf <= rws - 1) {
for (i = 1; i <= nf; i++) {
rptr = ++rptr % 8;
rbuf[rptr] = in.readLine();
System.out.println("The received Frame " + rptr + " is : "
+ rbuf[rptr]);
}
rws -= nf;
System.out.println("\nAcknowledgment sent\n");
p.println(rptr + 1);
rws += nf;
} else
break;
ch = in.readLine();
} while (ch.equals("yes"));
}
}

SENDER OUTPUT:
CLIENT OUTPUT:
Enter the value of m : 7
Enter no. of frames to be sent:5
Enter data for frame no 0 => 1
Enter data for frame no 1 => 2
Enter data for frame no 2 => 3
Enter data for frame no 3 => 4
Enter data for frame no 4 => 5
Connected with server.
|0||1||2||3||4|
Sending frame:0
Acknowledgement of frame no 0 recieved.
Sending frame:1
Sending frame:2
Sending frame:3
Sending frame:4
Sending frame:5

SERVER OUTPUT:
Server established. Client is now connected. |0||1||2||3||4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||
19||20||21||22||23||24||25||26||27||28||29||30||31|| 32||33||34||35||36||37||38||39||40||41||42||43||44||45||46||47||
48||49||50||51||52||53||54||55||56||57||58||59||60|| 61||62||63||64||65||66||67||68||69||70||71||72||73||74||75||76||
77||78||79||80||81||82||83||84||85||86||87||88||89|| 90||91||92||93||94||95||96||97||98||99||100||101||102||103||
104||105||106||107||108||109||110||111||112||113|| 114||115||116||117||118||119||120||121||122||123||124||
125||126||0|
Frame 0 recieved Data:0 Acknowledgement sent
|1||2||3||4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||21||22||23||24||25||26||27||28||29||30||31||32||
33||34||35||36||37||38||39||40||41||42||43||44||45||46||47||48||49||50||51||52||53||54||55||56||57||58||59||60||61||
62||63||64||65||66||67||68||69||70||71||72||73||74||75||76||77||78||79||80||81||82||83||84||85||86||87||88||89||90||
91||92||93||94||95||96||97||98||99||100||101||102||103||104||105||106||107||108||109||110||111||112||113||114||
115||116||117||118||119||120||121||122||123||124||125||126||0||1|
Frame 1 recieved Data:1 Error found.
Acknowledgement not sent.
|2||3||4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||21||22||23||24||25||26||27||28||29||30||31||32||33||
34||35||36||37||38||39||40||41||42||43||44||45||46||47||48||49||50||51||52||53||54||55||56||57||58||59||60||61||62||
63||64||65||66||67||68||69||70||71||72||73||74||75||76||77||78||79||80||81||82||83||84||85||86||87||88||89||90||91||
92||93||94||95||96||97||98||99||100||101||102||103||104||105||106||107||108||109||110||111||112||113||114||
115||116||117||118||119||120||121||122||123||124||125||126||0||1||2|
Frames received not in correct order
Expected farme:1 Received frame no :2
|3||4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||21||22||23||24||25||26||27||28||29||30||31||32||33||
34||35||36||37||38||39||40||41||42||43||44||45||46||47||48||49||50||51||52||53||54||55||56||57||58||59||60||61||62||
63||64||65||66||67||68||69||70||71||72||73||74||75||76||77||78||79||80||81||82||83||84||85||86||87||88||89||90||91||
92||93||94||95||96||97||98||99||100||101||102||103||104||105||106||107||108||109||110||111||112||113||114||
115||116||117||118||119||120||121||122||123||124||125||126||0||1||2||3|
Frames received not in correct order
Expected farme:1 Received frame no :3
|4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||21||22||23||24||25||26||27||28||29||30||31||32||33||34||
35||36||37||38||39||40||41||42||43||44||45||46||47||48||49||50||51||52||53||54||55||56||57||58||59||60||61||62||63||
64||65||66||67||68||69||70||71||72||73||74||75||76||77||78||79||80||81||82||83||84||85||86||87||88||89||90||91||92||
93||94||95||96||97||98||99||100||101||102||103||104||105||106||107||108||109||110||111||112||113||114||115||
116||117||118||119||120||121||122||123||124||125||126||0||1||2||3||4|
Frames received not in correct order
Expected farme:1 Received frame no :4
RESULT:
Thus the Sliding Window Go Back N & SELECTIVE REPEAT PROTOCOL was
implemented and the parameters measured by using LAN-T simulation software.
EX.NO:5 IMPLEMENTATION OF DISTANCE VECTOR ROUTING ALGORITH,
DATE: ALGORITHM

AIM:
To simulate and study the Distance Vector routing algorithm using simulation.
SOFTWARE REQUIRED:
NS-2
THEORY:
Distance Vector Routing is one of the routing algorithm in a Wide Area Network for
computing shortest path between source and destination. The Router is one main devices used
in a wide area network. The main task of the router is Routing. It forms the routing table and
delivers the packets depending upon the routes in the tableeither directly or via an
intermediate devices. Each router initially has information about its all neighbors. Then this
information will be shared among nodes.
ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and
execute nam on trace file.
4. Create n number of nodes using for loop
5. Create duplex links between the nodes
6. Setup UDP Connection between n(0) and n(5)
7. Setup another UDP connection between n(1) and n(5)
8. Apply CBR Traffic over both UDP connections
9. Choose distance vector routing protocol to transmit data from sender to receiver.
10. Schedule events and run the program.
PROGRAM:
set ns [new Simulator]
set nr [open thro.tr w]
$ns trace-all $nr
set nf [open thro.nam w]
$ns namtrace-all
$nf proc finish { }
{ global ns nr nf
$ns flush-trace close
$nf close
$nr exec nam thro.nam &
exit 0
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {

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


$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 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(5) $null0
$ns connect $udp0 $null0
set udp1 [new Agent/UDP]
$ns attach-agent $n(1) $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1 set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp1 $null0
$ns rtproto DV
$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)
$udp0 set fid_ 1
$udp1 set fid_ 2
$ns color 1 Red
$ns color 2 Green
$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"
$ns at 45 "finish"
$ns run
OUTPUT:
RESULT:
EX.NO:6 IMPLEMENTATION OF LINK STATE ROUTING
DATE: ALGORITHM

Thus the Distance vector Routing Algorithm was Simulated and studied.

AIM:
To simulate and study the link state routing algorithm using simulation.
SOFTWARE
REQUIRE
D: NS-2
THEORY:
In link state routing, each router shares its knowledge of its neighborhood with every
other router in the internet work. (i) Knowledge about Neighborhood: Instead of
sending its entire routing table a router sends info about its neighborhood only. (ii) To all
Routers: each router sends this information to every other router on the internet work
not just to its neighbor .It does so by a process called flooding. (iii)Information sharing
when there is a change: Each router sends out information about the neighbors when
there is change.

PROCEDURE:
The Dijkstra algorithm follows four steps to discover what is called the shortest path
tree(routing table) for each router:The algorithm begins to build the tree by identifying
its roots. The root router’s trees the router itself. The algorithm then attaches all nodes
that can be reached from the root. The algorithm compares the tree’s temporary arcs and
identifies the arc with the lowest cumulative cost. This arc and the node to which it
connects are now a permanent part of the shortest path tree. The algorithm examines the
database and identifies every node that can be reached from its chosen node. These
nodes and their arcs are added temporarily to the tree.
The last two steps are repeated until every node in the network has become a permanent
part of the tree.

ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows.
3. Open a nam trace file and define finish procedure then close the trace file, and
execute nam on trace file.
4. Create n number of nodes using for loop
5. Create duplex links between the nodes
6. Setup UDP Connection between n(0) and n(5)
7. Setup another UDP connection between n(1) and n(5)
8. Apply CBR Traffic over both UDP connections
9. Choose Link state routing protocol to transmit data from sender to receiver.
10. Schedule events and run the program.
PROGRAM:

set ns [new Simulator]


set nr [open thro.tr w]
$ns trace-all $nr
set nf [open thro.nam w]
$ns namtrace-all $nf
proc finish { }
{ global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}
55
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }
$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 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(5) $null0
$ns connect $udp0 $null0
set udp1 [new Agent/UDP]
$ns attach-agent $n(1) $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp1 $null0
$ns rtproto LS
$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)
$udp0 set fid_ 1
$udp1 set fid_ 2
$ns color 1 Red
$ns color 2 Green
$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"
$ns at 45 "finish"
$ns run
OUTPUT:
RESULT:

Thus the Link State Routing Algorithm was Simulated and studied.
EX.NO:7 DATA ENCRYPTION AND DECRYPTION USING DATA ENCRYPTION
DATE: STANDARD ALGORITHM

AIM:
To implement Data encryption and decryption

SOFTWARE REQUIREMENTS:

Turbo C software

THEORY:

In encryption, each letter position in the file text which is given in encrypt mode is changed
according to the ascending order of the key text. In decryption each letter position in the encrypted
file text is changed according to the ascending order of the key text.
ALGORITHM-ENCRYPTION

Get the text to be encrypted (plain text) and key text.

a. Find the length of the plain text.

b. For i=1 to length of plain text

c. Find the binary equivalent of i th character of plain text.

d. Find the binary equivalent of i th character of key text


e. Find the XOR of above two values.

The resulting value will be the encrypted format (cipher text) of the plain text.

ALGORITHM-DECRYPTION

Get the text to be decrypted (cipher text) and key text. Find the length of the cipher text. For
i=1 to length of cipher text a. Find the binary equivalent of i th character of cipher text. b. Find the
binary equivalent of i th character of key text c. Find the XOR of above two values. The resulting value
will be the decrypted format (original plain text) of the cipher plain text.
PROGRAM

# include
#include

void main ( )

{ static int s, i, k,n,c[100];

printf(“\n program 1: encryption and 2. decryption”);

scanf (“%d”, &s);

switch (s)
{ case 1: printf(“enter the key value:”);

scanf(“%d”, &k);

printf(“enter the length of text:”);

scanf(“%d”, &n);

printf(“enter the data to be encrypted:”);

for (i=0;i<=n;i++) scanf(“%c”, &c[i]);


for (i=0;i<=n;i++) {c[i]=c[i]+k;

if (c[i]>90) c[i]=c[i]-26;}

printf(“encrypted data”);

for (i=1;i<=n;i+ +)

printf(“%c”, c[i]);

break;
case 2: printf(“enter the key value:”);

scanf(“%d”, &k);

printf(“enter the length of text:”);

scanf(“%d”, &n);

printf(“enter the data to be decrypted:”);

for (i=0;i<=n;i++) scanf(“%c”, &c[i]);


for (i=0;i<=n;i++) {c[i]=c[i]-k; if (c[i]<=n;i+ +) printf(“%c”, c[i]);

break;

case 3: break;

getch ();

}
OUTPUT:

Program 1: encryption and 2. decryption

1. ENCRYPTION

enter the key value: 1

enter the length of text: 5

enter the data to be encrypted: HELLO


encrypted data : IFMMP

2. DECRYPTION

enter the key value: 1

enter the length of text: 5

enter the data to be decrypted: IFMMP

decrypted data : HELLO


RESULT:

Thus the Data Encryption and Decryption was studied


EX.NO:8 DATA ENCRYPTION AND DECRYPTION USING RSA
DATE: ALGORITHM

AIM:

To implement Data encryption and decryption

SOFTWARE REQUIREMENTS:
Turbo C software

THEORY:

In encryption, each letter position in the file text which is given in encrypt mode is changed
according to the ascending order of the key text. In decryption each letter position in the encrypted
file text is changed according to the ascending order of the key text.

ALGORITHM-ENCRYPTION
Get the text to be encrypted (plain text) and key text.
a. Find the length of the plain text.
b. For i=1 to length of plain text
c. Find the binary equivalent of i th character of plain text.
d. Find the binary equivalent of i th character of key text
e. Find the XOR of above two values.
The resulting value will be the encrypted format (cipher text) of the plain text.
ALGORITHM-DECRYPTION
Get the text to be decrypted (cipher text) and key text.
Find the length of the cipher text.
For i=1 to length of cipher text
a. Find the binary equivalent of i th character of cipher text.
b. Find the binary equivalent of i th character of key text
c. Find the XOR of above two values.
The resulting value will be the decrypted format (original plain text) of the cipher plain text
PROGRAM FOR DATA ENCRYPTION AND DECRYPTION USING RSA:
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.*;
import java.util.Base64;

public class RSAEncryption {


public static void main(String[] args) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException, IOException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom secureRandom = new SecureRandom();

keyPairGenerator.initialize(2048, secureRandom);
KeyPair pair = keyPairGenerator.generateKeyPair();

PublicKey publicKey = pair.getPublic();

PrivateKey privateKey = pair.getPrivate();

// Encrypt Hello world message


Cipher encryptionCipher = Cipher.getInstance("RSA");
encryptionCipher.init(Cipher.ENCRYPT_MODE, privateKey);
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("Enter the data to encrypt. .. ");
String message = br.readLine();
byte[] encryptedMessage = encryptionCipher.doFinal(message.getBytes());
String encryption = Base64.getEncoder()
.encodeToString(encryptedMessage);
System.out.println("encrypted message = " + encryption);

// Decrypt Hello world message


Cipher decryptionCipher = Cipher.getInstance("RSA");
decryptionCipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] decryptedMessage = decryptionCipher.doFinal(encryptedMessage);
String decryption = new String(decryptedMessage);
System.out.println("decrypted message = " + decryption);

}
OUTPUT:
Enter the data to encrypt. ..
goodday
encrypted message =
SmpWNbMRWNDSO3x2pbGBAIZSFZn43gFXbpo4FNp9pzA7TbpvxoL/W5fJ1p+3wRfkJI/
rriHDQSyeNMEi9Itx2q0ZCI0BimbshggAgBw4ot20tE3Fkjl4gRzOuOTav3B+bGQUeXZMnO6z/
eiFN7tj5NkY8lgq6WZm+8DxGUcvEzkSv8w53EhXvc0wkjdYEhEsS1FEfsTuMOKpFsM6Q24dARTe
VpRwxM58Iw8iLVzg7knrYCSf5mrHomr2yoImG528i3gAk2ZsYByHB0dtbOEBRtkO74zexQyoItr
TuGqAwnRmRASvyPrWqmp3i2UBn9KQTfiGO70s7JUKWA+y01F1Yw==
decrypted message = goodday
RESULT:
Thus the Data Encryption and Decryption using RSA algorithm was studied.
EX.NO:9 IMPLEMENTATION OF CLIENT SERVER MODEL USING
DATE: FTP PROTOCOL

AIM:
To implement the client server model using FTP protocol.
APPARATUS REQUIRED:
Java outlook
Personal computer
ALGORITHM:
1. Enter local passive mode for data connection.
2. Set file type to be transferred to binary.
3. Create an InputStream for the local file.
4. Construct path of the remote file on the server. The path can be absolute or relative to
the current working directory.
5. Call one of the storeXXX()methods to begin file transfer. There are two scenario.
6. Using an InputStream-based approach: this is the simplest way, since we let the
system does the ins and outs. There is no additional code, just passing the InputStream
object into the appropriate method, such as storeFile(String remote, InputStream
local) method.
7. Using an OutputStream-based approach: this is more complex way, but more control.
Typically we have to write some code that reads bytes from the InputStream of the
local file and writes those bytes into the OutputStream which is returned by the
storeXXX() method, such as storeFileStream(String remote) method.
8. Close the opened InputStream and OutputStream.
9. Call completePendingCommand() method to complete transaction.
10. Logout and disconnect from the server.
PROGRAM FOR CLIENT SERVER MODEL USING FTP PROTOCOL:
CLIENT:
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
public class FileClient {
public static void main(String[] args) throws Exception {

Socket sock = new Socket("127.0.0.1", 13267);


System.out.println("Connecting to the server");
InputStream is = sock.getInputStream();
// receive file
new FileClient().receiveFile(is);

sock.close();
}

public void receiveFile(InputStream is) throws Exception {


int filesize = 6022386;
int bytesRead;
int current = 0;
byte[] mybytearray = new byte[filesize];
String outputFilePath = "C:/File_Inputs/FTP_Output.txt";

FileOutputStream fos = new FileOutputStream(outputFilePath);


BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray, 0, mybytearray.length);
current = bytesRead;

do {
bytesRead = is.read(mybytearray, current,
(mybytearray.length - current));
if (bytesRead >= 0)
current += bytesRead;
} while (bytesRead > -1);

bos.write(mybytearray, 0, current);
bos.flush();
bos.close();
System.out.println("Received and saved file as " + outputFilePath);
}
}

SERVER:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FileServer {


public static void main(String[] args) throws Exception {
// create socket
ServerSocket servsock = new ServerSocket(13267);
System.out.println("Listening on Port 13267");
System.out.println("Waiting for the client....");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
OutputStream os = sock.getOutputStream();
new FileServer().send(os);
sock.close();
}

public void send(OutputStream os) throws Exception {


// sendfile
File myFile = new File("C:/File_Inputs/DemoData.txt");
byte[] mybytearray = new byte[(int) myFile.length() + 1];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
System.out.println("Sending file " + myFile.getPath());
os.write(mybytearray, 0, mybytearray.length);
os.flush();
}
}
OUTPUT:
SERVER:
Listening on Port 13267
Waiting for the client....
Accepted connection : Socket[addr=/127.0.0.1,port=62166,localport=13267]
Sending file C:\File_Inputs\DemoData.txt

RECEIVER:
Connecting to the server
Received and saved file as C:/File_Inputs/FTP_Output.txt
RESULT:

Thus the Client sever model using FTP protocol was studied.
EX.NO:10 NETWORK TOPOLOGY
DATE: BUS TOPOLOGY

AIM:
To create scenario and study the performance of token bus protocol through
simulation.
HARDWARE / SOFTWARE REQUIREMENTS:
NS-2
THEORY:
Token bus is a LAN protocol operating in the MAC layer. Token bus is standardized
as per IEEE 802.4. Token bus can operate at speeds of 5Mbps, 10 Mbps and 20 Mbps. The
operation of token bus is as follows: Unlike token ring in token bus the ring topology is
virtually created and maintained by the protocol. A node can receive data even if it is not part
of the virtual ring, a node joins the virtual ring only if it has data to transmit. In token bus
data is transmitted to the destination node only where as other control frames is hop to hop.
After each data transmission there is a solicit_successsor control frame transmitted which
reduces the performance of the protocol.
ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and
execute nam on trace file.
4. Create five nodes that forms a network numbered from 0 to 4
5. Create duplex links between the nodes and add Orientation to the nodes for setting
a LAN topology
6. Setup TCP Connection between n(1) and n(3)
7. Apply CBR Traffic over TCP.
8. Schedule events and run the program.
PROGRAM:

#Create a simulator object

set ns [new Simulator]

#Open the nam trace file

set nf [open out.nam w]


$ns namtrace-all

$nf #Define a 'finish' procedure

proc finish {} {

global ns nf

$ns flush-trace

#Close the trace file


close $nf

#Executenam on the trace file

exec nam out.nam &

exit 0

#Create five nodes


set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

#Create Lan between the nodes


set lan0 [$ns newLan "$n0 $n1 $n2 $n3 $n4" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Cd
Channel]

#Create a TCP agent and attach it to node n0

set tcp0 [new Agent/TCP]

$tcp0 set class_ 1

$ns attach-agent $n1 $tcp0

#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3
set sink0 [new Agent/TCPSink]

$ns attach-agent $n3 $sink0

#Connect the traffic sources with the traffic sink

$ns connect $tcp0 $sink0

# Create a CBR traffic source and attach it to tcp0

set cbr0 [new Application/Traffic/CBR]


$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.01

$cbr0 attach-agent $tcp0

#Schedule events for the CBR agents

$ns at 0.5 "$cbr0 start"

$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:
RESULT:
Thus the Bus Topology was Simulated and studied.

EX.NO:10 NETWORK TOPOLOGY


DATE: STAR TOPOLOGY

AIM:
To create scenario and study the performance of token star protocol through
simulation.
HARDWARE / SOFTWARE REQUIREMENTS:
NS-2
THEORY:
Token ring is a LAN protocol operating in the MAC layer. Token ring is standardized
as per IEEE 802.5. Token ring can operate at speeds of 4mbps and 16 mbps. The operation of
token ring is as follows: When there is no traffic on the network a simple 3-byte token
circulates the ring. If the token is free (no reserved by a station of higher priority as explained
later) then the station may seize the token and start sending the data frame. As the frame
travels around the ring ach station examines the destination address and is either forwarded
(if the recipient is another node) or copied. After copying4 bits of the last byte is changed.
This packet then continues around the ring till it reaches the originating station. After the
frame makes a round trip the sender receives the frame and releases a new token onto the
ring.
ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and
execute nam on trace
file.
4. Create five nodes that forms a network numbered from 0 to 4
5. Create duplex links between the nodes to form a Ring Topology.
6. Setup TCP Connection between n(1) and n(3)
7. Apply CBR Traffic over TCP
8. Schedule events and run the program
PROGRAM:

#Create a simulator object

set ns [new Simulator]

#Open the nam trace file

set nf [open out.nam w]


$ns namtrace-all $nf

#Define a 'finish' procedure proc finish {} {

global ns nf

$ns flush-trace

#Close the trace file

close $nf
#Executenam on the trace file

exec nam out.nam &

exit0

#Create five nodes

set n0 [$ns node]


set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]

#Create links between the nodes


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

$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 $n0 1Mb 10ms DropTail


#Create a TCP agent and attach it to node n0

set tcp0 [new Agent/TCP]

$tcp0 set class_ 1

$ns attach-agent

$n1 $tcp0

#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3
set sink0 [new Agent/TCPSink]

$ns attach-agent $n3 $sink0

#Connect the traffic sources with the traffic sink

$ns connect $tcp0 $sink0

# Create a CBR traffic source and attach it to tcp0

set cbr0 [new Application/Traffic/CBR]


$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.01

$cbr0 attach-agent $tcp0

#Schedule events for the CBR agents

$ns at 0.5 "$cbr0 start"

$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:
RESULT:
Thus the Star Topology was Simulated and studied.
EX.NO:10 NETWORK TOPOLOGY
DATE: RING TOPOLOGY
AIM:
To create scenario and study the performance of token ring protocol through
simulation.
HARDWARE / SOFTWARE REQUIREMENTS:
NS-2
THEORY:
Token bus is a LAN protocol operating in the MAC layer. Token ring is standardized
as per IEEE 802.4. Token bus can operate at speeds of 5Mbps, 10 Mbps and 20 Mbps. The
operation of token bus is as follows: Unlike token ring in token bus the ring topology is
virtually created and maintained by the protocol. A node can receive data even if it is not part
of the virtual ring, a node joins the virtual ring only if it has data to transmit. In token bus
data is transmitted to the destination node only where as other control frames is hop to hop.
After each data transmission there is a solicit_successsor control frame transmitted which
reduces the performance of the protocol.
ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a name trace file and define finish procedure then close the trace file, and
execute nam on trace file.
4. Create five nodes that forms a network numbered from 0 to 4
5. Create duplex links between the nodes and add Orientation to the nodes for setting
a LAN topology
6. Setup TCP Connection between n(1) and n(3)
7. Apply CBR Traffic over TCP.
8. Schedule events and run the program.

PROGRAM:
#Create a simulator object

set ns [new Simulator]

#Open the nam trace file

set nf [open out.nam w]

$ns namtrace-all

$nf #Define a 'finish'


procedure proc finish {} {

global ns nf

$ns flush-trace

#Close the trace file

close $nf

#Executenam on the trace


file exec nam out.nam &

exit 0

#Create six nodes

set n0 [$ns node]

set n1 [$ns node]


set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]

#Change the shape of center node in a star topology

$n0 shape square


#Create links between the nodes

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

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

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

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

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


#Create a TCP agent and attach it to node n0

set tcp0 [new Agent/TCP] $tcp0 set class_ 1

$ns attach-agent $n1 $tcp0

#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3 set

sink0 [new Agent/TCPSink]

$ns attach-agent $n3 $sink0


#Connect the traffic sources with the traffic sink

$ns connect $tcp0 $sink0

# Create a CBR traffic source and attach it to tcp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.01


$cbr0 attach-agent $tcp0

#Schedule events for the CBR agents

$ns at 0.5 "$cbr0 start"

$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:
RESULT:
Thus the Ring Topology was Simulated and studied.
EX.NO:11 PERFORMANCE ANALYSIS OF CSMA/CA AND CSMA/CD PROTOCOLS
DATE:

AIM:

To create scenario and study the performance of CSMA / CD protocol through simulation.
SOFTWARE REQUIREMENTS:

Ns-2

THEORY:

Ethernet is a LAN (Local area Network) protocol operating at the MAC (Medium
Access Control) layer. Ethernet has been standardized as per IEEE 802.3. The underlying
protocol in Ethernet is known as the CSMA /CD – Carrier Sense Multiple Access / Collision
Detection. The working of the Ethernet protocol is as explained below, A node which has data
to transmit senses the channel. If the channel is idle then, the data is transmitted. If the
channel is busy then, the station defers transmission until the channel is sensed to be idle and
then immediately transmitted. If more than one node starts data transmission at the same
time, the data collides. This collision is heard by the transmitting nodes which enter into
contention phase. The contending nodes resolve contention using an algorithm called
Truncated binary exponential back off.
ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and
execute nam on trace file.
4. Create six nodes that forms a network numbered from 0 to 5
5. Create duplex links between the nodes and add Orientation to the nodes for setting
a LAN topology.
6. Setup TCP Connection between n(0) and n(4)
7. Apply FTP Traffic over TCP
8. Setup UDP Connection between n(1) and n(5)
9. Apply CBR Traffic over UDP.
10. Apply CSMA/CA and CSMA/CD mechanisms and study their performance
11. Schedule events and run the program.
PROGRAM:

CSMA/CA

set ns [new Simulator]

#Define different colors for data flows (for NAM)

$ns color 1 Blue


$ns color 2 Red

#Open the Trace files

set file1 [open out.tr w]

set winfile [open WinFile w]

$ns trace-all $file1

#Open the NAM trace file


set file2 [open out.nam w]

$ns namtrace-all $file2

#Define a 'finish' procedure

proc finish {} {

global ns file1 file2

$ns flush-trace
close $file1

close $file2

exec nam out.nam &

exit 0

39

}
#Create six nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]


set n5 [$ns node]

$n1 color red

$n1 shape box

#Create links between the nodes

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

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


$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail

$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail

set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Ca Channel]

Setup a TCP connection

set tcp [new Agent/TCP/Newreno]

$ns attach-agent $n0 $tcp


set sink [new Agent/TCPSink/DelAck]

$ns attach-agent $n4 $sink

$ns connect $tcp $sink

$tcp set fid_ 1

$tcp set window_ 8000

$tcp set packetSize_ 552


#Setup a FTP over TCP connection

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ftp set type_ FTP

#Setup a UDP connection

set udp [new Agent/UDP]


$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n5 $null

$ns connect $udp $null

$udp set fid_ 2

#Setup a CBR over UDP connection


set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set type_ CBR

$cbr set packet_size_ 1000

$cbr set rate_ 0.01mb

$cbr set random_ false


$ns at 0.1 "$cbr start"

$ns at 1.0 "$ftp start"

$ns at 124.0 "$ftp stop"

$ns at 124.5 "$cbr stop"

# next procedure gets two arguments: the name of the

# tcp source node, will be called here "tcp",


# and the name of output file.

proc plotWindow {tcpSource file} {

global ns

set time 0.1

set now [$ns now]

set cwnd [$tcpSource set cwnd_]


set wnd [$tcpSource set window_]

puts $file "$now $cwnd"

$ns at [expr $now+$time] "plotWindow $tcpSource $file" }

$ns at 0.1 "plotWindow $tcp $winfile"

$ns at 5 "$ns trace-annotate \"packet drop\""

# PPP
$ns at 125.0 "finish"

$ns run
OUTPUT:
CSMA/CD

set ns [new Simulator]

#Define different colors for data flows (for NAM)

$ns color 1 Blue

$ns color 2 Red

#Open the Trace files


set file1 [open out.tr w]

set winfile [open WinFile w]

$ns trace-all $file1

#Open the NAM trace file

set file2 [open out.nam w]

$ns namtrace-all $file2


#Define a 'finish' procedure

proc finish {} {

global ns file1 file2

$ns flush-trace

close $file1

close $file2
exec nam out.nam &

exit 0

#Create six nodes

set n0 [$ns node]

set n1 [$ns node]


set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]

$n1 color red

$n1 shape box 41


#Create links between the nodes

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

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

$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail

$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail

set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Cd Channel]
Setup a TCP connection

set tcp [new Agent/TCP/Newreno]

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink/DelAck]

$ns attach-agent $n4 $sink

$ns connect $tcp $sink


$tcp set fid_ 1

$tcp set window_ 8000

$tcp set packetSize_ 552

#Setup a FTP over TCP connection

set ftp [new Application/FTP]

$ftp attach-agent $tcp


$ftp set type_ FTP

#Setup a UDP connection

set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n5 $null


$ns connect $udp $null

$udp set fid_ 2

#Setup a CBR over UDP connection

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set type_ CBR


$cbr set packet_size_ 1000

$cbr set rate_ 0.01mb

$cbr set random_ false

$ns at 0.1 "$cbr start"

$ns at 1.0 "$ftp start"

$ns at 124.0 "$ftp stop"


$ns at 124.5 "$cbr stop"

# next procedure gets two arguments: the name of the

# tcp source node, will be called here "tcp",

# and the name of output file.

proc plotWindow {tcpSource file} {

global ns
set time 0.1

set now [$ns now]

set cwnd [$tcpSource set cwnd_]

set wnd [$tcpSource set window_]

puts $file "$now $cwnd"

$ns at [expr $now+$time] "plotWindow $tcpSource $file" }


$ns at 0.1 "plotWindow $tcp $winfile"

$ns at 5 "$ns trace-annotate \"packet drop\""

# PPP

$ns at 125.0 "finish"

$ns run

OUTPUT
RESULT:

Thus the performance of CSMA / CD protocol was studied through simulation

You might also like