Sunteți pe pagina 1din 15

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

#include<stdio.h> #include<string.h> #include<conio.h> #define N strlen(g) char t[128], cs[128], g[]="10001000000100001"; int a, e, c; void xor() { for(c=1;c<N;c++) cs[c]=((cs[c]==g[c])?'0':'1'); } void crc() { for(e=0;e<N;e++) cs[e]=t[e]; do { if(cs[0]=='1') xor(); for(c=0;c<N-1;c++) cs[c]=cs[c+1]; cs[c]=t[e++]; }while(e<=a+N-1); } void main() { clrscr(); printf("\nEnter poly : "); scanf("%s",t); printf("\nGenerating Polynomial is : %s",g); a=strlen(t); for(e=a;e<a+N-1;e++) t[e]='0'; printf("\nModified t[u] is : %s",t); crc(); printf("\nChecksum is : %s",cs); for(e=a;e<a+N-1;e++) t[e]=cs[e-a]; printf("\nFinal Codeword is : %s",t); printf("\nTest Error detection 0(yes) 1(no) ? : "); scanf("%d",&e); if(e==0) { printf("Enter position where error is to inserted : "); scanf("%d",&e); t[e]=(t[e]=='0')?'1':'0'; printf("Errorneous data : %s\n",t); } crc(); for (e=0;(e<N-1)&&(cs[e]!='1');e++); if(e<N-1) printf("Error detected."); else printf("No Error Detected."); }

Output Enter poly : 1011101 Generating Polynomial is : 10001000000100001 Modified t[u] is : 10111010000000000000000 Checksum is : 1000101101011000 Final Codeword is : 10111011000101101011000 Test Error detection 0(yes) 1(no) ? : 0 Enter position where you want to insert error : 3 Errorneous data : 10101011000101101011000 Error detected. Enter poly : 1011101 Generating Polynomial is : 10001000000100001 Modified t[u] is : 10111010000000000000000 Checksum is : 1000101101011000 Final Codeword is : 10111011000101101011000 Test Error detection 0(yes) 1(no) ? : 1 No Error Detected.

2. Write a program for frame sorting technique used in buffers.


#include <stdlib.h> #include <time.h> #include <stdio.h> #include <conio.h> #include <string.h> #define FSize 5 typedef struct packet { int SeqNum; char Data[FSize+1]; }packet; struct packet *readdata, *transdata;

int divide(char *msg) { int msglen, NoOfPacket, i, j; msglen = strlen(msg); NoOfPacket = msglen/FSize; if((msglen%FSize)!=0) NoOfPacket++; readdata = (struct packet *)malloc(sizeof(packet) * NoOfPacket); for(i = 0; i < NoOfPacket; i++) { readdata[i].SeqNum = i + 1; for (j = 0; (j < FSize) && (*msg != '\0'); j++, msg++) readdata[i].Data[j] = *msg; readdata[i].Data[j] = '\0'; } printf("\nThe Message has been divided as follows\n"); printf("\nPacket No. Data\n\n"); for (i = 0; i < NoOfPacket; i++) printf(" %2d %s\n", readdata[i].SeqNum, readdata[i].Data); return NoOfPacket; } void shuffle(int NoOfPacket) { int *Status; int i, j, trans; randomize(); Status=(int * )calloc(NoOfPacket, sizeof(int)); transdata = (struct packet *)malloc(sizeof(packet) * NoOfPacket); for (i = 0; i < NoOfPacket;) { trans = rand()%NoOfPacket; if (Status[trans]!=1) { transdata[i].SeqNum = readdata[trans].SeqNum; strcpy(transdata[i].Data, readdata[trans].Data); i++; Status[trans] = 1; }

} free(Status); } void sortframes(int NoOfPacket) { packet temp; int i, j; for (i = 0; i < NoOfPacket; i++) for (j = 0; j < NoOfPacket i-1; j++) if (transdata[j].SeqNum > transdata[j + 1].SeqNum) { temp.SeqNum = transdata[j].SeqNum; strcpy(temp.Data, transdata[j].Data); transdata[j].SeqNum = transdata[j + 1].SeqNum; strcpy(transdata[j].Data, transdata[j + 1].Data); transdata[j + 1].SeqNum = temp.SeqNum; strcpy(transdata[j + 1].Data, temp.Data); } } void receive(int NoOfPacket) { int i; printf("\nPackets received in the following order\n"); for (i = 0; i < NoOfPacket; i++) printf("%4d", transdata[i].SeqNum); sortframes(NoOfPacket); printf("\n\nPackets in order after sorting..\n"); for (i = 0; i < NoOfPacket; i++) printf("%4d", transdata[i].SeqNum); printf("\n\nMessage received is :\n"); for (i = 0; i < NoOfPacket; i++) printf("%s", transdata[i].Data); } void main() { char *msg; int NoOfPacket; clrscr(); printf("\nEnter The message to be Transmitted :\n"); scanf("%[^\n]", msg); NoOfPacket = divide(msg); shuffle(NoOfPacket); receive(NoOfPacket); free(readdata); free(transdata); getch(); }

Output Enter The messgae to be Transmitted : hi, it was nice meeting u on sunday The Message has been divided as follows Packet No. Data 1 hi, i 2 t was 3 nice 4 meet 5 ing u 6 on s 7 unday Packets received in the following order 4 2 6 3 5 1 7 Packets in order after sorting.. 1 2 3 4 5 6 7 Message received is : hi, it was nice meeting u on sunday

3.Write a program for distance vector algorithm to find suitable path for transmission.
#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=0; printf("\nEnter the number of nodes : "); scanf("%d",&n); printf("\nEnter the 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("\n State value for router %d is \n",i+1); for(j=0;j<n;j++) printf("\t\nnode %d via %d Distance %d",j+1,rt[i].from[j]+1,rt[i].dist[j]); } printf("\n\n"); } Output Enter the number of nodes : 5 Enter the cost matrix : 0 2 1 4 999 2 0 999 3 4 1 999 0 3 2 4 3 3 0 999 999 4 2 5 0

State value for router 1 is node 1 via 1 Distance 0 node 2 via 2 Distance 2 node 3 via 3 Distance 1 node 4 via 4 Distance 4 node 5 via 3 Distance 3 State value for router 2 is node 1 via 1 Distance 2 node 2 via 2 Distance 0 node 3 via 1 Distance 3 node 4 via 4 Distance 3 node 5 via 5 Distance 4 State value for router 3 is node 1 via 1 Distance 1 node 2 via 1 Distance 3 node 3 via 3 Distance 0 node 4 via 4 Distance 3 node 5 via 5 Distance 2 State value for router 4 is node 1 via 1 Distance 4 node 2 via 2 Distance 3 node 3 via 3 Distance 3 node 4 via 4 Distance 0 node 5 via 3 Distance 5 State value for router 5 is node 1 via 3 Distance 3 node 2 via 2 Distance 4 node 3 via 3 Distance 2 node 4 via 4 Distance 5 node 5 via 5 Distance 0

4.Using TCP/IP sockets, write a client-server program to make client sending the file name and the server to send back the contents of the requested file if present.
CLIENT: #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<unistd.h> #include<stdlib.h> #include<stdio.h> int main(int argc,char *argv[]) { int create_socket,cont; int bufsize = 1024; char *buffer = malloc(bufsize); char fname[256]; struct sockaddr_in,address; if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0) printf("The Socket was created\n"); address.sin_family = AF_INET; address.sin_port = htons(15000); inet_pton(AF_INET,argv[1],&address.sin_addr); if (connect(create_socket,(struct sockaddr *) &address, sizeof(address)) == 0) printf("The connection was accepted with the server %s...\n", argv[1]); printf("Enter The Filename to Request : "); scanf("%s",fname); send(create_socket, fname, sizeof(fname), 0); printf("Request Accepted... Receiving File...\n\n"); printf("The contents of file are...\n\n"); while((cont=recv(create_socket, buffer, bufsize, 0))>0) { write(1, buffer, cont); } printf("\nEOF\n"); return close(create_socket); } SERVER: #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<sys/stat.h> #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<fcntl.h> int main() { int cont,create_socket,new_socket,addrlen,fd; int bufsize = 1024; char *buffer = malloc(bufsize); char fname[256];

struct sockaddr_in address; if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0) printf("The socket was created\n"); address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(15000); if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0) printf("Binding Socket\n"); listen(create_socket,3); addrlen = sizeof(struct sockaddr_in); new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen); if (new_socket > 0) printf("The Client %s is Connected...\n", inet_ntoa(address.sin_addr)); recv(new_socket,fname, 255,0); printf("A request for filename %s Received..\n", fname); if ((fd=open(fname, O_RDONLY))<0) perror("File Open Failed"); exit(0); while((cont=read(fd, buffer, bufsize))>0) send(new_socket,buffer,cont,0); printf("Request Completed\n"); close(new_socket); return close(create_socket); } Output SERVER: cc server.c ./a.out The socket was created Binding Socket The Client 127.0.0.1 is Connected... A request for filename farha.swx Received.. Request Completed CLIENT: cc client.c ./a.out 127.0.0.1 The Socket was created The connection was accepted with the server 127.0.0.1... Enter The Filename to Request : farha.swx Request Accepted... Receiving File... The contents of file are... This is sample text file to check socket programming.. ............WELCOME TO SOCKET PROGRAMMING............. EOF

5.Implement the above program using message queues or FIFO as IPC channels.
SERVER: #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<string.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main() { char filename[25],buf[300],buf1[2000]; int num,num2,n,filesize,fl,fd,fd2; mknod("fifo1",S_IFIFO | 0666,0); mknod("fifo2",S_IFIFO | 0666,0); printf("\nSERVER :Server Online\n"); fd=open("fifo1",O_RDONLY); printf("SERVER : Client online .. Waiting for request...\n"); num=read(fd,filename,25); filename[num]='\0'; if((fl=open(filename,O_RDONLY))!=-1) { printf("\nSERVER : %s is found \nTransfering the contents..\n",filename); filesize=lseek(fl,0,2); printf("\nFile size is %d\n",filesize); lseek(fl,0,0); n=read(fl,buf1,filesize); buf1[n]='\0'; fd2=open("fifo2",O_WRONLY); write(fd2,buf1,strlen(buf1)); printf("\nSERVER : Traansfer completed\n"); } else { printf("SERVER : File %s not found\n",filename); fd2=open("fifo2",O_WRONLY); write(fd2,NULL,0); } unlink("fifo1"); unlink("fifo2"); return 0; } CLIENT: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<fcntl.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main() { char filename[25],buf[2000]; int num,num2,fl,fd,fd2; mknod("fifo1",S_IFIFO | 0660,0);

mknod("fifo2",S_IFIFO | 0660,0); fd=open("fifo1",O_WRONLY); printf("CLIENT : Online... ! \nCLIENT : Enter the filename :"); scanf("%s",filename); write(fd,filename,strlen(filename)); printf("\nWaiting for reply"); fd2=open("fifo2",O_RDONLY); num2=read(fd2,buf,2000); if(num2==0) { printf("CLIENT : File not found.... Reply from server\n); return 0; } buf[num2]='\0'; printf("\nFile recieved...\n The contents are...\n"); fputs(buf,stdout); unlink("fifo1"); unlink("fifo2"); return 0; } Output SERVER: cc server.c ./a.out SERVER : Server Online SERVER : Client online .. Waiting for request . . SERVER : myfile.c is found Transferring the contents. . File size is 112 SERVER : Transfer completed CLIENT: cc client.c ./a.out CLIENT : Online... ! CLIENT : Enter the filename :myfile.c Waiting for reply File recieved... The contents are... This is sample text file to check socket programming.. ............WELCOME TO SOCKET PROGRAMMING.............

6.Write a program for simple RSA algorithm to encrypt and decrypt the data.
#include<stdio.h> #include<string.h> #include<stdlib.h> int mult(unsigned int x, unsigned int y,unsigned int n) { long int k=1,j; for(j=1;j<=y;j++) k=(k*x)%n; return (unsigned int } int main() { char msg[100]; unsigned int pt[100],ct[100],n,d,e,p,q,l; int i,len; printf("\nEnter message : "); gets(msg); len=strlen(msg); for(i=0;i<len;i++) pt[i]=msg[i], n=253 , d=17 , e=13; printf("\nPublic key : (%d %d)\n",e,n); printf("\nPrivate key : (%d %d)\n",d,n); for(i=0;i<len;i++) ct[i]=mult(pt[i],e,n); printf("\n\tPlain text\t\tCipher text\n\n"); for(i=0;i<len;i++) printf("\t%c \t\t\t%d %d\n",pt[i],pt[i],ct[i]); for(i=0;i<len;i++) pt[i]=mult(ct[i],d,n); printf("\n\tCipher text\t\tPlain text\n"); for(i=0;i<len;i++) printf("\t%d \t\t%d %c\n",ct[i],pt[i],pt[i]); return 0; } Output cc rsa.c ./a.out Enter message : College Public key : (13 253) Private key : (17 253) Plain text Cipher text C 67 111 o 111 122 l 108 3 l 108 3 e 101 173 g 103 86 e 101 173 Cipher text 111 67 122 111 3 108 3 108 173 101 86 103 173 101 Plain text C o l l e g e

7. Write a program for congestion control using Leaky bucket algorithm.


#include<iostream.h> #include<dos.h> #include<stdlib.h> #define bucketSize 512 void bktInput(int a,int b) { if(a>bucketSize) cout<<"\n\t\tBucket overflow"; else { delay(500); while(a>b) { cout<<"\n\t\t"<<b<<" bytes outputted."; a-=b; delay(500); } if (a>0) cout<<"\n\t\tLast "<<a<<" bytes sent\t"; cout<<"\n\t\tBucket output successful"; } } void main() { int op, pktSize; randomize(); cout<<"Enter output rate : "; cin>>op; for(int i=1;i<=5;i++) { delay(random(1000)); pktSize=random(1000); cout<<"\nPacket no "<<i<<"\tPacket size = "<<pktSize; bktInput(pktSize,op); } } Output Enter output rate : 100 Packet no 0 Packet size = 3 Bucket output successful Last 3 bytes sent Packet no 1 Packet size = 33 Bucket output successful Last 33 bytes sent Packet no 2 Packet size = 117 Bucket output successful 100 bytes outputted. Last 17 bytes sent Packet no 3 Packet size = 95 Bucket output successful Last 95 bytes sent Packet no 4 Packet size = 949 Bucket overflow

8.Write a program for Hamming Code generation for error detection and correction
#include<iostream.h> #include<conio.h> #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"}; void main() { int i,j; clrscr(); cout<<"Hamming Code --- Encoding\n"; cout<<"Enter 4 bit data : "; cin>>data; cout<<"Generator Matrix\n"; for(i=0;i<4;i++) cout<<"\t"<<gmatrix[i]<<"\n"; cout<<"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; cout<<encoded[i]<<" "; } cout<<"\nHamming code --- Decoding\n"; cout<<"Enter Encoded bits as received : "; for(i=0;i<7;i++) cin>>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) cout<<"Data is error free!!\n"; else { cout<<"Error received at bit number "<<j+1<<" of the data\n"; edata[j]=!edata[j]; cout<<"The Correct data Should be : "; for(i=0;i<7;i++) cout<<edata[i]<<" "; } } }

Output Hamming code----- Encoding Enter 4 bit data : 1010 Generator matrix 0111000 1010100 1100010 1110001 Encoded data 1 0 1 1 0 1 0 Hamming code----- Decoding Enter encoded bits as recieved : 1 1 1 1 0 1 0 Error recieved at bit number 2 of data Correct data should be : 1011010

S-ar putea să vă placă și