2
#include<stdio.h>
int main()
{
int a[20], b[30];
int i, j = 0, k, n, count;
printf(“Enter frame length: “);
scanf(“%d”, &n);
printf(“Enter input frame (0s and 1s only):n”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
i = 0;
count = 0;
while(i < n)
{
if(a[i] == 1)
{
b[j++] = a[i];
count = 1;
for(k = i + 1; k < n && a[k] == 1 && count < 5; k++)
{
b[j++] = a[k];
count++;
if(count == 5)
{
b[j++] = 0;
count = 0;
}
i = k;
}
}
else
{
b[j++] = a[i];
}
i++;
}
printf(“After stuffing: “);
for(i = 0; i < j; i++)
{
printf(“%d”, b[i]);
}
return 0;
}
3
#include<stdio.h>
#include<string.h>
int main()
{
char a[20], b[50], ch;
int i = 0, j = 0, n, pos;
printf(“Enter string: “);
scanf(“%s”, a);
n = strlen(a);
printf(“Enter position: “);
scanf(“%d”, &pos);
if(pos > n)
{
printf(“Invalid positionn”);
return 0;
}
getchar();
ch = getchar();
b[j++] = ‘d’;
b[j++] = ‘l’;
b[j++] = ‘e’;
b[j++] = ‘s’;
b[j++] = ‘t’;
b[j++] = ‘x’;
while(i < n)
{
if(i == pos – 1)
{
b[j++] = ‘d’;
b[j++] = ‘l’;
b[j++] = ‘e’;
b[j++] = ch;
b[j++] = ‘d’;
b[j++] = ‘l’;
b[j++] = ‘e’;
}
if(a[i] == ‘d’ && a[i+1] == ‘l’ && a[i+2] == ‘e’)
{
b[j++] = ‘d’;
b[j++] = ‘l’;
b[j++] = ‘e’;
}
b[j++] = a[i];
i++;
}
b[j++] = ‘d’;
b[j++] = ‘l’;
b[j++] = ‘e’;
b[j++] = ‘e’;
b[j++] = ‘t’;
b[j++] = ‘x’;
b[j] = ”;
printf(“Frame after stuffing:n%s”, b);
return 0;
}
4
#include<stdio.h>
#include<string.h>
int main()
{
char data[100];
int i, len;
printf(“Enter data string: “);
scanf(“%s”, data);
len = strlen(data);
printf(“nFrame (Character Count Framing):n”);
// First byte = count of characters
printf(“%d”, len);
// Print actual data
for(i = 0; i < len; i++)
{
printf(“%c”, data[i]);
}
return 0;
}
5 #include<stdio.h>
#include<string.h>
int main()
{
char data[100], gen[100];
int mes[200], div[100], zero[100];
int i, j, data_len, gen_len;
int temp, temp1, king;
printf(“Enter your data string: “);
scanf(“%s”, data);
printf(“Enter generator: “);
scanf(“%s”, gen);
data_len = strlen(data);
gen_len = strlen(gen);
// Convert generator to integer array
for(i = 0; i < gen_len; i++)
{
div[i] = gen[i] – ‘0’;
zero[i] = 0;
}
// Copy data into message array
for(i = 0; i < data_len; i++)
{
mes[i] = data[i] – ‘0’;
}
// Append zeros
for(i = data_len; i < data_len + gen_len – 1; i++)
{
mes[i] = 0;
}
// CRC division (XOR operation)
for(i = 0; i < data_len; i++)
{
temp = i;
king = mes[temp];
temp1 = temp;
for(j = 0; j < gen_len; j++)
{
if(king != 0)
mes[temp1] = mes[temp1] ^ div[j];
else
mes[temp1] = mes[temp1] ^ zero[j];
temp1++;
}
}
printf(“nCRC code is: “);
for(i = 0; i < data_len + gen_len – 1; i++)
{
printf(“%d”, mes[i]);
}
return 0;
}
6
#include<stdio.h>
int main()
{
int path[5][5], a[5][5], t[5];
int i, j, p, st = 1, ed = 5;
int stp, edp, min, index;
printf(“Enter cost matrix:n”);
for(i = 1; i <= 5; i++)
{
for(j = 1; j <= 5; j++)
{
scanf(“%d”, &a[i][j]);
}
}
printf(“Enter number of paths: “);
scanf(“%d”, &p);
printf(“Enter possible paths:n”);
for(i = 1; i <= p; i++)
{
for(j = 1; j <= 5; j++)
{
scanf(“%d”, &path[i][j]);
}
}
for(i = 1; i <= p; i++)
{
t[i] = 0;
stp = st;
for(j = 1; j <= 5; j++)
{
edp = path[i][j + 1];
t[i] = t[i] + a[stp][edp];
if(edp == ed)
break;
else
stp = edp;
}
}
min = t[1];
index = 1;
for(i = 1; i <= p; i++)
{
if(t[i] < min)
{
min = t[i];
index = i;
}
}
printf(“Minimum cost: %dn”, min);
printf(“Minimum cost path: “);
for(i = 1; i <= 5; i++)
{
printf(” –> %d”, path[index][i]);
if(path[index][i] == ed)
break;
}
return 0;
}
7
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
} rt[10];
int main()
{
int dmat[20][20];
int n, i, j, k, count;
printf(“Enter number of nodes: “);
scanf(“%d”, &n);
printf(“Enter cost matrix:n”);
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf(“%d”, &dmat[i][j]);
dmat[i][i] = 0;
rt[i].dist[j] = dmat[i][j];
rt[i].from[j] = j;
}
}
do
{
count = 0;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
for(k = 0; k < n; k++)
{
if(rt[i].dist[j] > dmat[i][k] + rt[k].dist[j])
{
rt[i].dist[j] = rt[i].dist[k] + rt[k].dist[j];
rt[i].from[j] = k;
count++;
}
}
}
}
} while(count != 0);
for(i = 0; i < n; i++)
{
printf(“nRouter %d table:n”, i + 1);
for(j = 0; j < n; j++)
{
printf(“Node %d via %d Distance %dn”,
j + 1, rt[i].from[j] + 1, rt[i].dist[j]);
}
}return 0;
}
8A]#include <stdio.h> #include <stdlib.h> #include <string.h> int main(){int choice,i;char plain[100],cipher[100],key[100];while(1){printf(“n—– MENU —–n1: Data Encryptionn2: Data Decryptionn3: ExitnEnter your choice: “);scanf(“%d”,&choice);switch(choice){case 1:printf(“nEnter plain text: “);scanf(” %[^n]”,plain);printf(“Enter key: “);scanf(” %[^n]”,key);for(i=0;plain[i]!=”;i++){cipher[i]=plain[i]^key[i%strlen(key)];}cipher[i]=”;printf(“Encrypted text: %sn”,cipher);break;case 2:printf(“nEnter encrypted text: “);scanf(” %[^n]”,cipher);printf(“Enter key: “);scanf(” %[^n]”,key);for(i=0;cipher[i]!=”;i++){plain[i]=cipher[i]^key[i%strlen(key)];}plain[i]=”;printf(“Decrypted text: %sn”,plain);break;case 3:printf(“Exiting…n”);exit(0);default:printf(“Invalid choice!n”);}}return 0;}
8B]#include <stdio.h> #include <string.h> #include <ctype.h> int main(){char pwd[20],alpha[26]=”abcdefghijklmnopqrstuvwxyz”;int num[20],i,n,key;printf(“nEnter the password:”);scanf(“%s”,pwd);n=strlen(pwd);for(i=0;i<n;i++)num[i]=tolower(pwd[i])-‘a’;printf(“nEnter the key:”);scanf(“%d”,&key);for(i=0;i<n;i++)num[i]=(num[i]+key)%26;for(i=0;i<n;i++)pwd[i]=alpha[num[i]];printf(“nThe key is:%d”,key);printf(“nEncrypted text is:%s”,pwd);for(i=0;i<n;i++){num[i]=(num[i]-key)%26;if(num[i]<0)num[i]+=26;pwd[i]=alpha[num[i]];}printf(“nDecrypted text is:%s”,pwd);return 0;}
set ns [new Simulator]; set nf [open out.nam w]; $ns namtrace-all $nf; proc finish {} {global ns nf; $ns flush-trace; close $nf; exec nam out.nam &; exit 0}; set n0 [$ns node]; set n1 [$ns node]; set n2 [$ns node]; set n3 [$ns node]; set n4 [$ns node]; set n5 [$ns node]; $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; set tcp0 [new Agent/TCP]; $tcp0 set class_ 1; $ns attach-agent $n0 $tcp0; set sink0 [new Agent/TCPSink]; $ns attach-agent $n3 $sink0; $ns connect $tcp0 $sink0; set cbr0 [new Application/Traffic/CBR]; $cbr0 set packetSize_ 500; $cbr0 set interval_ 0.01; $cbr0 attach-agent $tcp0; $ns at 0.5 “$cbr0 start”; $ns at 4.5 “$cbr0 stop”; $ns at 5.0 “finish”; $ns run
UTP set ns [new Simulator]; $ns color 1 Blue; $ns color 2 Red; set nf [open out.nam w]; $ns namtrace-all $nf; set f0 [open out.tr w]; $ns trace-all $f0; proc finish {} {global ns nf f0; $ns flush-trace; close $f0; close $nf; exec nam out.nam &; exit 0}; set n0 [$ns node]; set n1 [$ns node]; set n2 [$ns node]; set n3 [$ns node]; $ns duplex-link $n0 $n2 2Mb 10ms DropTail; $ns duplex-link $n1 $n2 2Mb 10ms DropTail; $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail; $ns queue-limit $n2 $n3 10; $ns duplex-link-op $n0 $n2 orient right-down; $ns duplex-link-op $n1 $n2 orient right-up; $ns duplex-link-op $n2 $n3 orient right; $ns duplex-link-op $n2 $n3 queuePos 0.5; set tcp [new Agent/TCP]; $tcp set class_ 2; $ns attach-agent $n0 $tcp; set sink [new Agent/TCPSink]; $ns attach-agent $n3 $sink; $ns connect $tcp $sink; $tcp set fid_ 1; set ftp [new Application/FTP]; $ftp attach-agent $tcp; $ftp set type_ FTP; set udp [new Agent/UDP]; $ns attach-agent $n1 $udp; set null [new Agent/Null]; $ns attach-agent $n3 $null; $ns connect $udp $null; $udp set fid_ 2; set cbr [new Application/Traffic/CBR]; $cbr attach-agent $udp; $cbr set type_ CBR; $cbr set packet_size_ 1000; $cbr set rate_ 1mb; $cbr set random_ false; $ns at 0.1 “$cbr start”; $ns at 1.0 “$ftp start”; $ns at 4.0 “$ftp stop”; $ns at 4.5 “$cbr stop”; $ns at 4.5 “$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink”; $ns at 5.0 “finish”; puts “CBR packet size = [$cbr set packet_size_]”; puts “CBR interval = [$cbr set interval_]”; $ns run
PROTOCOLSset ns [new Simulator]; $ns rtproto DV; $ns macType Mac/Sat/UnslottedAloha; set nf [open aloha.nam w]; $ns namtrace-all $nf; set f0 [open aloha.tr w]; $ns trace-all $f0; proc finish {} {global ns nf f0; $ns flush-trace; close $f0; close $nf; exec nam aloha.nam &; exit 0}; set n0 [$ns node]; set n1 [$ns node]; set n2 [$ns node]; set n3 [$ns node]; set n4 [$ns node]; set n5 [$ns node]; $ns duplex-link $n0 $n4 1Mb 50ms DropTail; $ns duplex-link $n1 $n4 1Mb 50ms DropTail; $ns duplex-link $n2 $n5 1Mb 1ms DropTail; $ns duplex-link $n3 $n5 1Mb 1ms DropTail; $ns duplex-link $n4 $n5 1Mb 50ms DropTail; $ns duplex-link $n2 $n3 1Mb 50ms DropTail; $ns duplex-link-op $n4 $n5 queuePos 0.5; set udp0 [new Agent/UDP]; $ns attach-agent $n0 $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 $n2 $null0; $ns connect $udp0 $null0; $ns at 0.5 “$cbr0 start”; $ns rtmodel-at 1.0 down $n5 $n2; $ns rtmodel-at 2.0 up $n5 $n2; $ns at 4.5 “$cbr0 stop”; $ns at 5.0 “finish”; $ns run
set ns [new Simulator]; set n0 [$ns node]; set n1 [$ns node]; $ns at 0.0 “$n0 label Sender”; $ns at 0.0 “$n1 label Receiver”; set nf [open stop.nam w]; $ns namtrace-all $nf; set f [open stop.tr w]; $ns trace-all $f; $ns duplex-link $n0 $n1 0.2Mb 200ms DropTail; $ns duplex-link-op $n0 $n1 orient right; $ns queue-limit $n0 $n1 10; Agent/TCP set nam_tracevar_ true; set tcp [new Agent/TCP]; $tcp set window_ 1; $tcp set maxcwnd_ 1; $ns attach-agent $n0 $tcp; set sink [new Agent/TCPSink]; $ns attach-agent $n1 $sink; $ns connect $tcp $sink; set ftp [new Application/FTP]; $ftp attach-agent $tcp; $ns add-agent-trace $tcp tcp; $ns monitor-agent-trace $tcp; $tcp tracevar cwnd_; $ns at 0.1 “$ftp start”; $ns at 3.0 “$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink”; $ns at 3.5 “finish”; $ns at 0.0 “$ns trace-annotate “Stop and Wait with normal operation””; $ns at 0.05 “$ns trace-annotate “FTP starts at 0.1″”; $ns at 0.11 “$ns trace-annotate “Send Packet_0″”; $ns at 0.35 “$ns trace-annotate “Receive Ack_0″”; $ns at 0.56 “$ns trace-annotate “Send Packet_1″”; $ns at 0.79 “$ns trace-annotate “Receive Ack_1″”; $ns at 0.99 “$ns trace-annotate “Send Packet_2″”; $ns at 1.23 “$ns trace-annotate “Receive Ack_2 “”; $ns at 1.43 “$ns trace-annotate “Send Packet_3″”; $ns at 1.67 “$ns trace-annotate “Receive Ack_3″”; $ns at 1.88 “$ns trace-annotate “Send Packet_4″”; $ns at 2.11 “$ns trace-annotate “Receive Ack_4″”; $ns at 2.32 “$ns trace-annotate “Send Packet_5″”; $ns at 2.55 “$ns trace-annotate “Receive Ack_5 “”; $ns at 2.75 “$ns trace-annotate “Send Packet_6″”; $ns at 2.99 “$ns trace-annotate “Receive Ack_6″”; $ns at 3.1 “$ns trace-annotate “FTP stops””; proc finish {} {global ns nf; $ns flush-trace; close $nf; puts “running nam…”; exec nam stop.nam &; exit 0}; $ns run