1. write a program for error detecting code using crc ... · 3 8 1 4 6 4 the routing table for node...
TRANSCRIPT
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);
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 1 of 39
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';
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 2 of 39
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';
}
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 3 of 39
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
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 4 of 39
PROGRAM # 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() //function which splits message
{ int k=0,i,j; //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) //generate array of random nos
{ 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 ) // function shuffles the frames
{
int i, k=0, random_ary[no_frames];
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 5 of 39
generate(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:
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 6 of 39
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 Technology
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 7 of 39
3. 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)
{
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 8 of 39
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++)
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 9 of 39
{
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++)
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 10 of 39
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:3
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 11 of 39
Enter 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:
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 12 of 39
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:
1 0 1
2 5 2
3 3 3
4 7 4
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 no link no hop
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 no link no hop
3 0 3
4 no link no hop
The routing table for node 4 is as follows:
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 13 of 39
1 no link no hop
2 no link no hop
3 no link no hop
4 0 4
The configuration of the nodes after computation of paths is as follows:
The routing table for node 1 is as follows:
1 0 1
2 5 2
3 3 3
4 7 4
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 8 1
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 8 1
3 0 3
4 10 1
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 14 of 39
The routing table for node 4 is as follows:
1 no link no hop
2 no link no hop
3 no link no hop
4 0 4
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:7
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 15 of 39
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: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:7
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 16 of 39
Enter 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:
1 0 1
2 5 2
3 3 3
4 7 4
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 no link no hop
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 no link no hop
3 0 3
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 17 of 39
4 2 4
The routing table for node 4 is as follows:
1 7 1
2 6 2
3 2 3
4 0 4
The configuration of the nodes after computation of paths is as follows:
The routing table for node 1 is as follows:
1 0 1
2 5 2
3 3 3
4 5 3
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 8 1
4 6 4
The routing table for node 3 is as follows:
1 3 1
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 18 of 39
2 8 1
3 0 3
4 2 4
The routing table for node 4 is as follows:
1 5 3
2 6 2
3 2 3
4 0 4
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found:
1 4
The length of the shortest path is 7
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found:
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 19 of 39
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");
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 20 of 39
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);
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 21 of 39
}
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
{
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 22 of 39
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++)
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 23 of 39
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
ar
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 24 of 39
5. 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");
}
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 25 of 39
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)
{
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 26 of 39
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++)
{
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 27 of 39
for(j=0;j<n-i-1;j++)
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
ar
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 28 of 39
6. 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;
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 29 of 39
}
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.out
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 30 of 39
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 31 of 39
Enter 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 u
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 32 of 39
decrypted value and its char representation:111 o
encrypted value and its char representation:1093 E
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
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 33 of 39
decrypted value and its char representation:114 r
put:
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 34 of 39
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];
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 35 of 39
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
0 0 1 0 0 1
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
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 36 of 39
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);
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 37 of 39
}
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[1]:10 bytes packet[2]:10 bytes packet[3]:50 bytes
packet[4]:30 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!!
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 38 of 39
www.bookspar.com | VTU NOTES | QUESTION PAPERS | NEWS | RESULTS | FORUMS 39 of 39