Darshan M Sonde

CCN LAB PROGRAMS

1

// program to stuff and destuff bits #include <iostream.h> #include <string.h> #include <conio.h> void stuff(); void destuff(); char bits[100]; //MSG format : 01111110 ..message.. 01111110 void main() { char ch[100]; clrscr(); cout<<"Enter Bits:"; cin>>ch; strcpy(bits,"01111110"); strcat(bits,ch); strcat(bits,"01111110"); cout<<"\ndata with flag:"<<bits; stuff(); cout<<"\n Stuffed data:"<<bits; destuff(); cout<<"\nDestuffed data:"<<bits; getch();

}

void stuff() { int n=strlen(bits),oneCount=0; for(int i=8;i<n-8;i++) //neglect first & last 8 flag bits { if(bits[i]=='1') { oneCount++; if(oneCount==5) { for(int j=n;j>i;j--) //copy array from i:n to j+1:n+1 bits[j+1]=bits[j]; //make room to stuff i.e shift right bits[i+1]='0'; //stuff '0' n++; oneCount=0; } } else oneCount=0; } } void destuff() { //function similar to stuffing int n=strlen(bits),oneCount=0; //remove first and last 8 bits = flag for(int i=0;i<n-16;i++) bits[i]=bits[i+8]; bits[i]='###BOT_TEXT###'; n=strlen(bits);

} } else oneCount=0.Darshan M Sonde CCN LAB PROGRAMS 2 } for(i=0. if(oneCount==5) { for(int j=i+1.i<n.//copy such that '0' is destuffed n--. } OUTPUT Enter Bits:111111 data with flag: 0111111011111101111110 Stuffed data: 01111110111110101111110 Destuffed data: 111111 . oneCount=0.j<n.j++) //copy array from i+2:n to i+1:n-1 bits[j]=bits[j+1].i++) { if(bits[i]=='1') { oneCount++.

bits[4]=0x10.//DLE = bits[1]=0x02. for(i=0.j>=i. cin>>ch.h> #include <string. //remove first and last 2 bits = flag for(int i=0.j++) bits[j]=bits[j+1].i<n. DLE ETX void main() { char ch[100]. char bits[100].Darshan M Sonde CCN LAB PROGRAMS 3 // program to stuff and destuff bits #include <iostream.//STX = strcat(bits. } } } void destuff() { //function similar to stuffing int n=strlen(bits).i++) bits[i]=bits[i+2].h> #include <conio.e shift right bits[i]=0x10. bits[0]=0x10. n=strlen(bits). getch().//DLE = bits[n+1]=0x03. n--. int n=strlen(bits). bits[n]=0x10.//put a ► ☺ ► = ♥ random DLE in message } void stuff() { int n=strlen(bits).h> #include <stdlib. //make room to stuff i.. cout<<"\nDestuffed data:"<<bits. bits[i]='###BOT_TEXT###'. stuff(). cout<<"Enter chars:".i<n-2.j<n. void destuff(). destuff().h> void stuff(). for(int i=2. //copy such that DLE is destuffed } . //MSG format : DLE STX .ch).j--) bits[j+1]=bits[j]. clrscr().ETX) or ESC in msg stuff a ESC for(int j=n.message.i++) //neglect first & last 2 flag bits { if(bits[i]==0x10||bits[i]==0x02||bits[i]==0x03) {//if you get FLAG(STX. i++.i<n-4. cout<<"\n Stuffed data:"<<bits.//stuff DLE n++.i++) { if(bits[i]==0x10) { for(int j=i+1.//ETX bits[n+2]='###BOT_TEXT###'.. } } cout<<"\ndata with flag:"<<bits.

Darshan M Sonde CCN LAB PROGRAMS 4 OUTPUT: aim is to transmit and receive me►sage Enter chars:message data with flag: ►☺me►sage►♥ Stuffed data: ►☺me►►sage►♥ Destuffed data: me►sage .

length + dist[k][i].Darshan M Sonde CCN LAB PROGRAMS 5 //program for finding shortest path //using function given in CCN . do { for(i=0.i<n.i<n. k.length. }while(k>=0).tentative} . state[t].label == tentative) { if(state[k]. struct state *p.length+dist[k][i]<state[i].TANENBAUM #include <stdio.i++) if(state[i].length<min) { min = state[i].h> #include <iostream. for(p=&state[0]. k=t.label = permanent.h> #include <conio.predecessor. } .label == tentative && state[i]. }state[MAX_NODES]. p<&state[n]. } state[t]. i=0.length) { state[i].min=INFINITY.k=state[k].int t.k=s.h> #define MAX_NODES 100 #define INFINITY 10000 int n.predecessor = k. } } k=0.h> #include <stdlib. p->label = tentative. do{ path[i++]=k.p++) { p->predecessor = -1.length = state[k]. }while( k!= s). void shortest_path(int s. lbl label. int i.i++) if(dist[k][i] != 0 && state[i]. } state[k]. k=i. p->length = INFINITY. state[i]. min.dist[MAX_NODES][MAX_NODES]. for(i=0. int length. struct state { int predecessor.length = 0.int path[]) { enum lbl{permanent.label = permanent.

p[MAX_NODES]. cout<<"Enter src. } OUTPUT 0 1 1 1 2 2 3 Shortest path connect 4 4 1 3 Enter no of nodes:5 Enter src.x.tmp2. cin>>tmp. for(i=0. cout<<"Destination:".j<MAX_NODES.j++) dist[i][j]=INFINITY.dest. printf("\nEnter no of nodes:").distance(-1. return 0.tmp.distance(-1.i<n. }while(i>=0).&n).j. } getch(). for(i=0.i++) for(j=0. j=dest. break. do{ cin>>i>>j>>tmp.i<MAX_NODES.i++) { printf("%d ".tmp2. scanf("%d". if(p[i]==tmp2) //if p[i]==destination stop. cout<<"\nSource node:". tmp=distance if(i>=0) dist[i][j] = dist[j][i] = tmp.x to end): 0 1 1 1 2 2 2 3 3 3 4 1 4 1 4 1 4 1 -1 0 0 Source node:0 Destination:3 0 1 4 3 .p).Darshan M Sonde CCN LAB PROGRAMS 6 int main() { int i. //p is path returned from function printf("\n").p[i]). shortest_path(tmp. //i=src.x to end):\n". cin>>tmp2.dest.x.

h> #include <string.i++) msg[i] = table[msg[i]-'a'].i<strlen(msg).. cin>>msg. //DECRYPTION------------------------------------------for(i=0..h> #include <conio. } cout<<"\nDecrypted message:"<<msg. } OUTPUT Enter msg:message Encrypted message:dtllqut Decrypted message:message .m=25 .i++) { for(int j=0..b.i<strlen(msg). //ENCRYPTION------------------------------------------int i. char table[26] = "qwertyuiopasdfghjklzxcvbnm". getch().h> void main() { char msg[100].//get the look up value cout<<"Encrypted message:"<<msg.j++) if(msg[i]==table[j])break...Darshan M Sonde CCN LAB PROGRAMS 7 //program for encryption and decryption using substitution #include <iostream.//look up table cout<<"\nEnter msg:".z msg[i]=j+'a'. //gets position of char i inside table into j //ex: pos of q=0 w=1 e=2 .which correspond to a..j<26. for(i=0.c.

cout<<"Enter keyword:". } void crypt(int en) { j=0. } } } OUTPUT Enter Message:message Enter Keyword:key Encrypted Message:eamsesg Decrypted Message:message . [i]]. cin.i++) { count=1.h> #include <conio.i<n.99).Darshan M Sonde CCN LAB PROGRAMS 8 //program for encryption and decryption of a message using transposition #include <iostream.count. cout<<"\nEncrypted Message:"<<msg.h> #include <string.i++) { count=0. char msgCopy[100].j++) if(key[j]<key[i]) //get the no of chars less than key[i] count++. cout<<"\nDecrypted Message:"<<msg.m=strlen(msg).h> #define NCRYPT 0 #define DCRYPT 1 void crypt(int en). for(j=0.m. //this gives position of key[i] pos[count-1]=i. void main() { cout<<"\nEnter Message:". int i. crypt(DCRYPT).msg). //just reverse of encryption line above count+=n.i<n. n=strlen(key).pos[80]. getch().msg).key[80]. int n. to output msg[pos } crypt(NCRYPT). strcpy(msgCopy. char msg[100]. cin>>key. for(i=0.//encrypt else msg[count+pos[i]]=msgCopy[j++]. while(count+pos[i]<m) { if(en==NCRYPT) msg[j++]=msgCopy[count+pos[i]].j.j<n. //now store offset required. //count is position of cursor for(i=0.getline(msg. strcpy(msgCopy.

edges[a][b]=edges[b][a]=INF. } .edges[MAX][MAX]. getch(). for(i=0.&i.i<n.j<MAX.b.count).b. } printf("\nEnter no of nodes:"). int count=0. } //find tree in_tree[0]=1.i++) for(j=0. printf("\nEnter a.x to end):\n").b). initially make any node // selected/attached(=1) to tree //while(tree is not complete) //{ // find least cost edge with one node free(=0)& one node attached to tree(=1) // ^^^^^^^^^^ // if(no such node) error in network // add edge to tree or display // delete edge from network //} #define MAX 100 #define INF 10000 int n. scanf("%d%d%d". scanf("%d".b=j.&n).i<MAX.i++) for(j=0. while(1) { int tmp.cost=0.x. void main() { int i.a. break.&tmp). in_tree[i]=0.h> #include <conio. } printf("\ntotal cost=%d". edges[i][j]=edges[j][i]=tmp.cost).in_tree[MAX]. for(i=0.j++) { edges[i][j]=INF.h> //using prim's algorithm //in_tree is set of selected nodes.min.j++) if(in_tree[j]&&!in_tree[i]&&min>edges[i][j]) { //i th node is not attached //j th node is in tree min=edges[i][j].a. cost+=edges[a][b].j. in_tree[a]=1. //attach node to tree printf("\n%d--%d".j<n. while(count<n-1) { min=INF.dist (-1.Darshan M Sonde CCN LAB PROGRAMS 9 //minimum spanning tree #include <stdio. if(i<0)break. } if(min==INF) { printf("Error in network:count=%d".&j. a=i. } count++.

Darshan M Sonde CCN LAB PROGRAMS 10 0 10 5 25 6 28 1 14 16 2 12 5 0 10 14 6 25 4 22 3 Min spanning tree 1 16 2 12 24 18 4 22 3 tree Enter no of nodes:7 Enter a.x to end): 0 1 28 1 2 16 2 3 12 3 4 22 4 5 25 5 0 10 4 6 24 6 3 18 6 1 14 -1 0 0 5—0 4—5 3—4 2—3 1—2 6—1 total cost=99 .b.x.dist(-1.

i<16. printf("\nEnter data length(1-16):"). //crc-generation for(i=0. ch-='0'. } .h> #include <conio. for(i=0.&n). //check if msb is '1' if(ch) data=data^CRC_CODE. disp(data>>16).. //input to LSB bit. data<<=1.i<n. unsigned long data_copy. data<<=1.:"). //check if msb is '1' if(ch) data=data^CRC_CODE. data = data ^ 0x0002. //crc-checking printf("\nchecking crc of data.i++) { ch=(data>>31)&1.i++) { ch=(data>>31)&1. //if 0 no need to add it is zero from shifting } data<<=16. //ccitt polynomial x^16 + x^12 + x^5 + 1 = 1000 1000 0001 0000 1 //CRC code is in MSB 17 bits.. clrscr().i. //data>>16 because remainder is in MSB 16 bits printf("\ngenerated data+checksum:"). scanf("%d". else printf("ERROR DETECTED"). getch().i++) { data<<=1.h> void disp(long). data = data_copy^(data>>16). void main() { unsigned long data=0.ch. printf("Enter msg:"). hence remainder in MSB 16 bits int n. if(ch) data|=1. unsigned long CRC_CODE=0x88108000.i<16. } printf("\nchecksum code :"). } if(data==0) printf("NO ERRORS FOUND"). ch=getche(). //padding 16 zeros data_copy=data.Darshan M Sonde CCN LAB PROGRAMS 11 // crc program #include <stdio. disp(data). //introduce error for(i=0.

0000.for.1101. } } OUTPUT Enter dta length(1-16):4 Enter msg:1101 checksum code :0000.0001.. else printf("0").ch.1010. ch=ch&1.just.clarity .Darshan M Sonde CCN LAB PROGRAMS 12 void disp(long val) { //displays contents of val all 32 bits int i.1101 checking crc of data :ERROR DETECTED //commenting the line in program used to introduce error gives checking crc of data.0000..1101.:NO ERROR DETECTED //the . if(ch) printf("1").0000.0001.1101.0000.1101 generated data+checksum:0000. in above output is.0000. for(i=0.1010.i++) { ch=val>>(31-i).i<32.