1. Write a program for error detecting code using CRC-CCITT (16bit)
#include
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);
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
temp[i]=m[i];
for(i=0;i<16;i++)
r[i]=m[i];
printf("\nintermediate remainder\n");
for(i=0;i
{
if(r[0]=='1')
{
q[i]='1';
calram();
}
else
{
q[i]='0';
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
m[i]=((int)m[i]-48)^((int)r[k++]-48)+48;
m[i]='\0';
}
2. Write a program for frame sorting technique used in buffer
#include
struct frame
{
int fslno;
char finfo[20];
};
struct frame arr[10];
int n;
void sort()
{
int i,j,ex;
struct frame temp;
for(i=0;i
{
ex=0;
for(j=0;j
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
ex++;
}
if(ex==0)
break;
}
}
int main()
{
int i;
system("clear");
printf("enter no of frames\n");
scanf("%d",&n);
printf("enter frame sequence no and frame contents\n");
for(i=0;i
scanf("%d%s",&arr[i].fslno,&arr[i].finfo);
sort();
printf("frames in sequence\n");
for(i=0;i
{
printf("01111110 %d lt %s 01111110\n",arr[i].fslno,arr[i].finfo);
}
}
3. Write a program for distance vector algorithm to find suitable path for transmission
#include
#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)
{
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++)
{
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++)
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]);
}
}
4. Write a program for spanning tree algorithm to find loop less path
#include
#include
#define MAX 20
#define INFINITY 999
enum boolean {false,true};
void prim(int c[][MAX],int t[MAX],int n);
int mincost=0;
int main()
{
int n,c[MAX][MAX],t[2*(MAX-1)];
int i,j;
printf("this pgm implements prim's algom\n");
printf("how many nodes does the graph have?\n");
scanf("%d",&n);
printf("enter cost adjacency matrix\n");
printf("999 indicates no connection\n");
for(i=0;i
for(j=0;j
scanf("%d",&c[i][j]);
prim(c,t,n);
printf("spanning tree\n");
for(i=0;i<2*(n-1);i+=2)
printf("%d%d\n",t[i]+1,t[i+1]+1);
printf("mincost=%d",mincost);
return 0;
}
void prim(int c[][MAX],int t[MAX],int n)
{
int i,j;
enum boolean v[MAX];
int u,s,min,v1,v2;
for(i=0;i
v[i]=false;
v[0]=true;
u=0;
t[u]=1;
s=0;
u++;
while(u
{
min=INFINITY;
for(i=0;i
for(j=1;j
if(v[i]==true&&v[j]==false&&c[i][j]
{
min=c[i][j];
v1=i;
v2=j;
}
mincost=mincost+min;
if(min==INFINITY)
{
printf("graph disconnected:spanning tree impossible\n");
exit(1);
}
v[v2]=true;
u++;
t[s++]=v1;
t[s++]=v2;
}
}
5. 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
#include
#include
#include
#include
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");
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
#include
#include
#include
#include
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);
}
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
{
printf("server:file not found\n");
n=write(newsockfd,"file not foung",15);
if(n<0)
printf("error: writing to socket..\n");
}
return 0;
}
6. Implement the above program using as Message queue or FIFOs as IPC channel.
Server Side:
#include
#include
#include
#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");
}
Client Side:
#include
#include
#include
#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)
{
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");
}
7. Write a program for simple RSA algorithm to encrypt and decrypt the data
#include
#include
#include
#include
unsigned long modexp(unsigned long msg,unsigned long exp,unsigned long n)
{
unsigned long i,k=1;
for(i=0;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
{
if((z%i)==0)
continue;
else
break;
}
e=i;
printf("\nEncryption key is :%lu",e);
for(i=1;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
{
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;
}
8. Write a program for hamming code generation for error detection/correction
Encoding.
#include
#include
#include
int gmatrix[4][7]={0,1,1,1,0,0,0,
1,0,1,0,1,0,0,
1,1,1,0,0,1,0,
1,1,1,0,0,0,1};
char data[5];
int encode[8];
int con(char x);
int main()
{
int i,j;
system("clear");
printf("Enter the Hamming code\n");
scanf("%s",&data);
for(i=0;i<7;i++)
for(j=0;j<4;j++)
encode[i]=encode[i]+con(data[j])*gmatrix[j][i];
puts(" ENCODE");
for(i=0;i<7;i++)
{
encode[i]=encode[i]%2;
printf("%i",encode[i]);
}
puts(" ");
return 0;
}
int con(char x)
{
if(x == '1')
return 1;
else
return 0;
}
Decoding
#include
#include
#include
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 edata[8];
int syndrome[3];
int errdig;
int con(char x)
{
if(x=='1')
return 1;
else
return 0;
}
int main()
{
int i,j,err;
system("system");
printf("\nEnter the encoded bit\n");
scanf("%s",edata);
for(i=0;i<3;i++)
for(j=0;j<7;j++)
syndrome[i]=syndrome[i]+con(edata[j])*hmatrix[i][j];
for(i=0;i<3;i++)
syndrome[i]=syndrome[i]%2;
err=4*syndrome[0]+2*syndrome[1]+1*syndrome[2];
printf("\nhi...%d\n",err);
if(0==err)
printf("\nError free data\n");
else
{ switch(err)
{
case 4:errdig=1;
break;
case 3:errdig=4;
break;
case 1:errdig=3;
break;
default:errdig=err;
}
printf("\nError in bit %d----%c",errdig,edata[errdig]);
errdig--;
if('1'==edata[errdig])
edata[errdig]='0';
else
edata[errdig]='1';
}
printf("\nThe data is:");
for(i=3;i<7;i++)
printf("%c",edata[i]);
puts(" ");
}
9. Write a program for congestion control using Leaky Bucket algorithm
#include
#define buffersize 10
struct buff
{
int pno;
struct buff *link;
};
struct buff *front,*rear,*temp;
main()
{
front=NULL;
rear=NULL;
int n=0,i,timer=0,npacks=0;
for(i=1;i<=buffersize;i++)
{
n++;
insert(n);
npacks++;
}
while(n<20)
{
n++;
if(timer==5)
{
delete();
npacks--;
if(npacks<=10)
insert(n);
timer=0;
}
else
{
printf("\nPacket %d is discarded..\n",n);
timer++;
sleep(1);
}
}
}
insert(int n)
{
struct buff *temp;
if(front==NULL)
{
printf("\nPacket %d is queued..\n",n);
temp=(struct buff*)malloc(sizeof(struct buff));
temp->pno=n;
temp->link=NULL;
front=temp;
rear=temp;
}
else
{
printf("\npacket %d queued..\n",n);
temp=(struct buff*)malloc(sizeof(struct buff));
temp->pno=n;
temp->link=NULL;
rear->link=temp;
rear=temp;
}
}
delete()
{
struct buff *temp;
if(front==NULL)
printf("\nBuffer is empty..\n");
else
{
temp=front;
printf("\nPacket %d id deleted",temp->pno);
front=front->link;
free(temp);
}
}