Sie sind auf Seite 1von 10

File: /home/pradyot/Desktop/CN FINAL/crc.

c
//7. Write a program for error detecting code using CRC-CCITT (16- bits).
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char rem[50],a[50],s[50],c,msg[50],gen[]="111";
int i,genlen,t,j,flag=0,k,n;
printf("Generator polynomial is CRC-CCITT %s\n",gen);
genlen=strlen(gen);
k=genlen-1;
printf("\nEnter the message\n");
n=0;
while((c=getchar())!='\n')
{
msg[n]=c;
n++;
}
msg[n]='\0';
for(i=0;i<n;i++)
a[i]=msg[i];
for(i=0;i<k;i++)
a[n+i]='0';
a[n+k]='\0';
printf("\nMessage Polynomial attended with zeros:");
puts(a);
for(i=0;i<n;i++)
{
if(a[i]=='1')
{
t=i;
for(j=0;j<k;j++)
{
if(a[t]==gen[j])
a[t]='0';
else
a[t]='1';
t++;
}
}
}
for(i=0;i<k;i++)
rem[i]=a[n+i];
rem[k]='\0';
printf("\nThe check sum appended:");
puts(rem);
printf("\nMesage with checksum appended:");
for(i=0;i<n;i++)
a[i]=msg[i];
for(i=0;i<k;i++)
a[n+i]=rem[i];
a[n+k]='\0';
puts(a);
n=0;
printf("\nEnter the received polynomial:");
while((c=getchar())!='\n')
{
s[n]=c;
n++;
}
s[n]='\0';

Page 1 of 2

File: /home/pradyot/Desktop/CN FINAL/crc.c


for(i=0;i<n;i++)
{
if(s[i]=='1')
{
t=i;
for(j=0;j<k;j++,t++)
{
if(s[t]==gen[j])
s[t]='0';
else
s[t]='1';
}
}
}
for(i=0;i<k;i++)
rem[i]=s[n+i];
rem[k]='\0';
for(i=0;i<k;i++)
{
if(rem[i]=='1')
flag=1;
}
if(flag==0)
printf("\nThe received polynomial is error free\n");
else
printf("\nAlert the received polynomial has error");
return 0;
}
/*
pradyot@pradyot:~$ cd cn
pradyot@pradyot:~/cn$ cc crc.c
pradyot@pradyot:~/cn$ ./a.out
Generator polynomial is CRC-CCITT 111
Enter the message
1011
Message Polynomial attended with zeros:101100
The check sum appended:10
Mesage with checksum appended:101110
Enter the received polynomial:1011
Alert the received polynomial has error
pradyot@pradyot:~/cn$ ./a.out
Generator polynomial is CRC-CCITT 111
Enter the message
1011
Message Polynomial attended with zeros:101100
The check sum appended:10
Mesage with checksum appended:101110
Enter the received polynomial:101110
The received polynomial is error free
pradyot@pradyot:~/cn$
*/

Page 2 of 2

File: /home/pradyot/Desktop/CN FINAL/dist1.c

Page 1 of 1

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int min(int a,int b)
{
return (a<b)?a:b;
}
void dist(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(j=1;j<=n;j++)
for(i=1;i<=n;i++)
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
void main()
{
int a[10][10],i,j,n,flag,temp=1;
printf("enter the number of nodes\n");
scanf("%d",&n);
printf("enter the matrix 0 for self loop and 999 for no path \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dist(a,n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d to %d is %d\n",i,j,a[i][j]);
}
printf("\n");
}
}
/*
pradyot@pradyot:~$ cc dist1.c
pradyot@pradyot:~$ ./a.out
enter the number of nodes
4
enter the matrix 0 for self loop and 999 for no path
0 4 1 4
4 0 2 1
1 2 0 5
4 1 5 0
1 to 1 is 0
1 to 2 is 3
1 to 3 is 1
1 to 4 is 4
2
2
2
2

to
to
to
to

1
2
3
4

is
is
is
is

3
0
2
1

3
3
3
3

to
to
to
to

1
2
3
4

is
is
is
is

1
2
0
3

4
4
4
4

to
to
to
to

1
2
3
4

is 4
is 1
is 3
is 0
*/

File: /home/pradyot/Desktop/cn nal for pdf/9.c

Page 1 of 2

/*9. Using TCP/IP sockets, write a client server program to make the client
send the file name and to make the server send back the contents of the
requested file if present.
CLIENT:
*/
#include<stdio.h>
#include<sys/fcntl.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<netinet/in.h>
#include<sys/types.h>
#define MAX 100
#define MAXBUFF 1000
int main(int argc,char* argv[])
{
int n,c,sid;
char buf[MAX],buf1[MAXBUFF];
struct sockaddr_un client;
sid=socket(AF_UNIX,SOCK_STREAM,0);
if(sid<0)
printf("socket creation error\n");
client.sun_family=AF_UNIX;
strcpy(client.sun_path,"ftsocket");
c=connect(sid,(struct sockaddr*)&client,sizeof(client));
if(c<0)
printf("connection error\n");
if(argc==1)
{
printf("enter the filename\n");
scanf("%s",buf);
}
else
{
strcpy(buf,argv[1]);
}
write(sid,buf,strlen(buf));
while((n=read(sid,buf1,MAXBUFF))>0)
{
write(1,buf1,n);
}
printf("\n");
close(sid);
unlink("ftsocket");
return 0;
}
/*
[student@localhost 9]$ cc -o client client.c
[student@localhost 9]$ ./client
enter the filename
hi
hi how are u i am fine
[student@localhost 9]$ ./client
connection error
enter the filename
hi
[student@localhost 9]$ ./client
enter the filename
hi
hi how are u i am fine fuck you
[student@localhost 9]$
*/

File: /home/pradyot/Desktop/cn nal for pdf/9.c


SERVER:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<sys/types.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<string.h>
#define MAX 100
#define MAXBUFF 1000
int main()
{
int sid,fd,a,l,n,b,length;
char buf[MAX],buf1[MAXBUFF];
struct sockaddr_un server,client;
sid=socket(AF_UNIX,SOCK_STREAM,0);
if(sid<0)
printf("Socket creation error\n");
server.sun_family=AF_UNIX;
strcpy(server.sun_path,"ftsocket");
b=bind(sid,(struct sockaddr*)&server,sizeof(server));
if(b<0)
printf("Bind error\n");
l=listen(sid,5);
if(l<0)
printf("listen error\n");
length=sizeof(client);
a=accept(sid,(struct sockaddr*)&client,&length);
if(a<0)
printf("accept error\n");
n=read(a,buf,MAX);
buf[n]='\0';
printf("file recieved by client is %s",buf);
fd=open(buf,0,O_RDONLY);
if(fd<0)
{
printf("file open error or file not present\n");
strcpy(buf,"file open error");
n=strlen(buf);
buf[n]='\0';
write(a,buf,n);
}
while((n=read(fd,buf1,MAXBUFF))>0)
{
write(a,buf1,n);
}
printf("\n");
close(a);
close(sid);
close(fd);
unlink("ftsocket");
return 0;
}
/*
[student@localhost 9]$ cc -o server server.c
[student@localhost 9]$ ./server
file recieved by client is hi
*/

Page 2 of 2

File: /home/pradyot/Desktop/cn nal for pdf/10.c


10. Implement the above program using as message queues or FIFOs as IPC
channels.
CLIENT:
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#define FIFO1 "fifo1"
#define FIFO2 "fifo2"
#define PERMS 0666
char fname[256];
int main()
{
int readfd,writefd,fd;
ssize_t n;
char buff[512];
printf("Trying To Connect To SERVER!!!\n");
writefd=open(FIFO1,O_WRONLY,0);
readfd=open(FIFO2,O_RDONLY,0);
if(readfd<0)
{
printf("run server 1st\n");
return(0);
}
printf("Connection Established\n");
printf("Enter the File Name to request from Server:\n");
scanf("%s",fname);
write(writefd,fname,strlen(fname));
printf("Waiting for Server to Reply...\n");
while((n=read(readfd,buff,512))>0)
write(1,buff,n);
close(readfd);
close(writefd);
return(0);
}
/*
pradyot@pradyot:~/cn/10$ cc -o client fifoclient.c
pradyot@pradyot:~/cn/10$ ./client
Trying To Connect To SERVER!!!
Connection Established
Enter the File Name to request from Server:
hi
Waiting for Server to Reply...
hi ho w are you
i am fine
pradyot@pradyot:~/cn/10$
*/
SERVER:
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#define FIFO1 "fifo1"
#define FIFO2 "fifo2"
#define PERMS 0666
char fname[256];
int main()
{
int readfd,writefd,fd;
ssize_t n;
char buff[512];
if(mkfifo(FIFO1,PERMS)<0)
{
perror("Cannot Create FIFO1\n");
return -1;
}

Page 1 of 2

File: /home/pradyot/Desktop/cn nal for pdf/10.c


if(mkfifo(FIFO2,PERMS)<0)
{
perror("Cannot Create FIFO2\n");
return -1;
}
printf("Waiting For Connection Request!!!\n");
readfd=open(FIFO1,O_RDONLY,0);
if(readfd < 0 )
{
perror("Cannot Open FIFO1 for Read\n");
return -1;
}
writefd=open(FIFO2,O_WRONLY,0);
if(writefd < 0 )
{
perror("Cannot Open FIFO1 for write\n");
return -1;
}
printf("Connection Established\n");
read(readfd,fname,255);
printf("Client has Requested for this file %s\n",fname);
if((fd=open(fname,O_RDWR))<0)
{
strcpy(buff,"File Doesn't Exist\n");
write(writefd,buff,strlen(buff));
}
else
{
while((n=read(fd,buff,512))>0)
write(writefd,buff,n);
}
close(readfd);
unlink(FIFO1);
close(writefd);
unlink(FIFO2);
return(0);
}
/*
pradyot@pradyot:~/cn/10$ cc -o server fifoserver.c
pradyot@pradyot:~/cn/10$ ./server
Waiting For Connection Request!!!
Connection Established
Client has Requested for this file hi
pradyot@pradyot:~/cn/10$
*/

Page 2 of 2

File: /home/pradyot/Desktop/cn nal for pdf/11.c


11. Write a program for simple RSA algorithm to encrypt and decrypt the
data.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
long int e,d,n;
long int val[50];
char decode(long int ch)
{
int i;
long int temp=ch;
for(i=1;i<d;i++)
ch=temp*ch%n;
return(ch);
}
int gcd(long a,long b)
{
if(b==0)
return a;
else
return(gcd(a,a%b));
}
int prime(int a)
{
int i;
for(i=2;i<a;i++)
{
if((a%i)==0)
return 0;
}
return 1;
}
int encode(char ch)
{
int i;
long int temp=ch;
for(i=1;i<e;i++)
temp=temp*ch%n;
return(temp);
}
int main()
{
int i;
long int p,q,phi,c[50];
char text[50],ctext[50];
printf("Enter the text to be encoded \n");
gets(text);
printf("%s \n",text);
do
{
p=rand()%30;
}while(!prime(p));
do
{
q=rand()%30;
}while(!prime(q));
n=p*q;
phi=(p-1)*(q-1);
printf("\n p=%d\t q=%d n=%d phi=%d \n",p,q,n,phi);
do
{
e=rand()%phi;
}while(!gcd(e,phi));

Page 1 of 2

File: /home/pradyot/Desktop/cn nal for pdf/11.c


do
{
d=rand()%phi;
}while((d*e%phi)!=1);
printf("\n**** encoding message*** \n");
sleep(2);
for(i=0;text[i]!='\0';i++)
val[i]=encode(text[i]);
val[i]=-999;
printf("\n**** encoded message *** \n");
for(i=0;val[i]!=-999;i++)
printf("%ld",val[i]);
printf("\n decoded message");
sleep(2);
for(i=0;val[i]!=-999;i++)
ctext[i]=decode(val[i]);
ctext[i]='\0';
printf("\n decoded message :%s\n\n\n",ctext);
}
/*pradyot@pradyot:~/cn$ ./a.out
Enter the text to be encoded
hi how are u
hi how are u
p=13

q=23 n=299 phi=264

**** encoding message***


**** encoded message ***
13248981322714198111114759839
decoded message
decoded message :hi how are u

pradyot@pradyot:~/cn$
*/

Page 2 of 2

File: /home/pradyot/Desktop/cn nal for pdf/12.c

Page 1 of 1

//12. Write a program for congestion control using leaky bucket algorithm.
#include<stdio.h>
int main()
{
int max_buf_size=400, out_flow_rate=100, i, in_flow[10], out_flow=0, dis_arr
[10], cur_buf_used=0;
printf("Enter the inflow:\n");
for(i=0;i<5;i++)
scanf("%d",&in_flow[i]);
printf("\nINFLOW OUTFLOW CURBUFUSED DISCARDED\n" );
printf("-----------------------------------\n");
for(i=0;i<5;i++)
{
printf("%d",in_flow[i]);
if((cur_buf_used+in_flow[i])<=max_buf_size)
{
cur_buf_used+=in_flow[i];
dis_arr[i]=0;
}
else
{
dis_arr[i]=((in_flow[i]+cur_buf_used)-max_buf_size);
cur_buf_used=max_buf_size;
}
if(cur_buf_used>=out_flow_rate)
{
out_flow=out_flow_rate;
cur_buf_used-=out_flow_rate;
}
else
{
out_flow=cur_buf_used-out_flow;
cur_buf_used=0;
}
printf("%12d",out_flow);
printf("%12d",cur_buf_used);
printf("%15d",dis_arr[i]);
printf("\n");
}
while(cur_buf_used>=out_flow_rate)
{
out_flow=out_flow_rate;
cur_buf_used-=out_flow_rate;
printf("%14d",out_flow);
printf("%15d\n",cur_buf_used);
}
out_flow=cur_buf_used;
cur_buf_used=0;
printf("%14d",out_flow);
printf("%15d\n",cur_buf_used);
}
/*
pradyot@pradyot:~/cn$ cc leaky.c
pradyot@pradyot:~/cn$ ./a.out
Enter the inflow:
80
90
100
110
120
INFLOW OUTFLOW CURBUFUSED DISCARDED
----------------------------------80
80
0
90
10
0
100
100
0
110
100
10
120
100
30
30
*/

0
0
0
0
0
0

Das könnte Ihnen auch gefallen