Write A Program For Error Detecting Code Using CRC-CCITT (16bit)
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);
#include<stdio.h>
#include<string.h>
#define FRAM_TXT_SIZ 3
#define MAX_NOF_FRAM 127
char str[FRAM_TXT_SIZ*MAX_NOF_FRAM];
{ char text[FRAM_TXT_SIZ];
int seq_no;
}fr[MAX_NOF_FRAM], shuf_ary[MAX_NOF_FRAM];
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;
} }
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:
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
#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)
{
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++)
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++)
1 0 1
2 5 2
3 3 3
4 7 4
1 5 1
2 0 2
3 no link no hop
4 6 4
1 3 1
2 no link no hop
3 0 3
4 no link no hop
2 no link no hop
3 no link no hop
4 0 4
1 0 1
2 5 2
3 3 3
4 7 4
1 5 1
2 0 2
3 8 1
4 6 4
1 3 1
2 8 1
3 0 3
4 10 1
1 no link no hop
2 no link no hop
3 no link no hop
4 0 4
^C
1 0 1
2 5 2
3 3 3
4 7 4
1 5 1
2 0 2
3 no link no hop
4 6 4
1 3 1
2 no link no hop
3 0 3
1 7 1
2 6 2
3 2 3
4 0 4
1 0 1
2 5 2
3 3 3
4 5 3
1 5 1
2 0 2
3 8 1
4 6 4
1 3 1
3 0 3
4 2 4
1 5 3
2 6 2
3 2 3
4 0 4
14
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");
Server Side:
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netdb.h>
Output:
server:(null) found!
transfering the contentserver:transfer completed
Segmentation fault
[root@localhost ~]#
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++)
#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)
{
Output:
Server part output
server:(null) found!
transfering the contentserver:transfer completed
Segmentation fault
[root@localhost ~]#
sort(int n)
{
struct frame temp;
int i,j,ex;
for(i=0;i<n-1;i++)
{
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
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;
31
37
Encryption key is :7
put:
#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};
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];
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
001001
Data is error free!!
#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);
OUTPUT: