Beruflich Dokumente
Kultur Dokumente
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
Page 2 of 2
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
*/
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]$
*/
Page 2 of 2
Page 1 of 2
Page 2 of 2
Page 1 of 2
pradyot@pradyot:~/cn$
*/
Page 2 of 2
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