Professional Documents
Culture Documents
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);
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';
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';
}
3 of 39
OUTPUT:-
4 of 39
#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
char text[FRAM_TXT_SIZ];
int seq_no;
}fr[MAX_NOF_FRAM], shuf_ary[MAX_NOF_FRAM];
int assign_seq_no()
{
int k=0,i,j;
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:
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
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)
{
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++)
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++)
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]);
}
}
11 of 39
12 of 39
no link
no hop
no link
no hop
no link
no hop
13 of 39
no link
no hop
no link
no hop
no link
no hop
10
14 of 39
no link
no hop
no link
no hop
no link
no hop
15 of 39
16 of 39
no link
no hop
no link
no hop
17 of 39
18 of 39
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");
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>
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
{
22 of 39
Output:
23 of 39
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
ar
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");
}
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)
{
26 of 39
Output:
Server part output
27 of 39
for(j=0;j<n-i-1;j++)
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
ar
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>
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;
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
30 of 39
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
32 of 39
33 of 39
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];
35 of 39
36 of 39
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[4]:30 bytes
packet[1]:10 bytes
packet[2]:10 bytes
packet[3]:50 bytes
38 of 39
39 of 39