More Related Content Similar to Network lap pgms 7th semester (20) Network lap pgms 7th semester1. NETWORK LAB PROGRAMS<br />1.CRC<br />#include<stdio.h><br />#include<string.h><br />char data[100],concatdata[117],src_crc[17],dest_crc[17],frame[120],divident[18],divisor[18]=quot;
10001000000100001quot;
,res[17]=quot;
0000000000000000quot;
;<br />void crc_cal(int node)<br />{<br />int i,j;<br />for(j=17;j<=strlen(concatdata);j++)<br />{<br />if(divident[0]=='1')<br />{<br />for(i=1;i<=16;i++)<br />if(divident[i]!=divisor[i])<br />divident[i-1]='1';<br />else<br />divident[i-1]='0';<br />}<br />else<br />{<br />for(i=1;i<=16;i++)<br />divident[i-1]=divident[i];<br />}<br />if(node==0)<br />divident[i-1]=concatdata[j];<br />else<br />divident[i-1]=frame[j];<br />}<br />divident[i-1]='';<br />printf(quot;
crc is %squot;
,divident);<br />if(node==0)<br />{<br />strcpy(src_crc,divident);<br />}<br />else<br />strcpy(dest_crc,divident);<br />}<br />int main()<br />{<br /> int i,len,rest;<br /> printf(quot;
AT SOURCE NODEenter the data to be send :quot;
);<br /> gets(data);<br /> strcpy(concatdata,data);<br /> strcat(concatdata,quot;
0000000000000000quot;
);<br /> for(i=0;i<=16;i++)<br />divident[i]=concatdata[i];<br /> divident[i+1]='';<br /> crc_cal(0);<br /> printf(quot;
data is :quot;
);<br /> puts(data);<br /> printf(quot;
the frame transmitted is :quot;
);<br /> printf(quot;
%s%squot;
,data,src_crc);<br /> printf(quot;
SOURCE NODE TRANSMITTED THE FRAME ---->quot;
);<br /> printf(quot;
AT DESTINATION NODEenter the received frame:quot;
);<br /> gets(frame);<br /> for(i=0;i<=16;i++)<br />divident[i]=frame[i];<br /> divident[i+1]='';<br /> crc_cal(1);<br /> if(strcmp(dest_crc,res)==0)<br />printf(quot;
received frame is error free quot;
);<br /> else<br />printf(quot;
received frame has one or more errorquot;
);<br /> return 1;<br />}<br />2.Distance vector<br />#include<stdio.h><br />struct rtable<br />{<br />int dist[20],nextnode[20];<br />}table[20];<br />int cost[10][10],n;<br />void distvector()<br />{<br />int i,j,k,count=0;<br />for(i=0;i<n;i++)<br />{<br />for(j=0;j<n;j++)<br />{<br />table[i].dist[j]=cost[i][j];<br />table[i].nextnode[j]=j;<br />}<br />}<br /> do<br /> {<br /> count=0;<br />for(i=0;i<n;i++)<br />{<br />for(j=0;j<n;j++)<br />{<br />for(k=0;k<n;k++)<br />{<br />if(table[i].dist[j]>cost[i][k]+table[k].dist[j])<br />{<br />table[i].dist[j]=table[i].dist[k]+table[k].dist[j];<br />table[i].nextnode[j]=k;<br />count++;<br />}<br />}<br />}<br />}<br />}while(count!=0);<br />}<br />int main()<br />{<br />int i,j;<br />printf(quot;
enter the no of vertices:quot;
);<br />scanf(quot;
%dquot;
,&n);<br />printf(quot;
enter the cost matrixquot;
);<br />for(i=0;i<n;i++)<br />for(j=0;j<n;j++)<br />scanf(quot;
%dquot;
,&cost[i][j]);<br />distvector();<br />for(i=0;i<n;i++)<br />{<br />printf(quot;
state value for router %c quot;
,i+65);<br />printf(quot;
destnodenextnodedistancequot;
);<br />for(j=0;j<n;j++)<br />{<br />if(table[i].dist[j]==99)<br />printf(quot;
%c-infinitequot;
,j+65);<br />else<br />printf(quot;
%c%c%dquot;
,j+65,table[i].nextnode[j]+65,table[i].dist[j]);<br />}<br />}<br />return 0;<br />}<br />3.FIFO Client<br />#include <stdio.h><br />#include <stdlib.h><br />#include <errno.h><br />#include <string.h><br />#include <fcntl.h><br />#include <sys/types.h><br />#include <sys/stat.h><br />#include <unistd.h><br />#define FIFO1_NAME quot;
fifo1quot;
<br />#define FIFO2_NAME quot;
fifo2quot;
<br />int main()<br />{<br /> char p[100],f[100],c[100];<br />int num,num2,fl,fd,fd2;<br />mknod(FIFO1_NAME,S_IFIFO | 0666, 0);<br />mknod(FIFO2_NAME,S_IFIFO | 0666,0);<br />printf(quot;
Waiting for SERVER ! quot;
);<br />fd=open(FIFO1_NAME,O_WRONLY);<br />printf(quot;
Server online ! client:Enter the path quot;
);<br />while(gets(p), !feof(stdin))<br />{<br /> <br />if ((num = write(fd,p,strlen(p))) == -1)<br /> <br /> perror(quot;
write errorquot;
);<br /> <br /> else<br /> {<br /> printf(quot;
waiting for reply ...quot;
);<br /> <br /> fd2=open(FIFO2_NAME, O_RDONLY);<br /> <br /> if ((num2=read(fd2,c,300))== -1)<br /> <br /> perror(quot;
Transfer errorquot;
);<br /> <br /> else<br /> <br /> {<br /> <br /> printf(quot;
File receiver ! Displaying the contents :quot;
);<br /> <br /> if (fputs(c,stdout)== EOF)<br /> <br /> perror(quot;
print error quot;
);<br /> <br /> exit(1);<br />}<br /> }<br />}<br />}<br />4.FIFO Server<br />#include <stdio.h><br />#include <stdlib.h><br />#include <errno.h><br />#include <string.h><br />#include <fcntl.h><br />#include <sys/types.h><br />#include <sys/stat.h><br />#include <unistd.h><br />#define FIFO1_NAME quot;
fifo1quot;
<br />#define FIFO2_NAME quot;
fifo2quot;
<br />int main()<br />{<br /> <br />char p[100],f[100],c[100];<br />int num,num2,fl,fd,fd2;<br />mknod(FIFO1_NAME,S_IFIFO | 0666, 0);<br />mknod(FIFO2_NAME,S_IFIFO | 0666,0);<br />printf(quot;
SERVER Online ! quot;
);<br />fd=open(FIFO1_NAME,O_RDONLY);<br />printf(quot;
client online !@ waiting for request ...quot;
);<br />while(1)<br />{<br /> <br />if ((num = read(fd,p,100)) == -1)<br /> <br /> perror(quot;
read errorquot;
);<br /> <br /> else<br /> <br />{<br /> <br /> p[num]='';<br /> <br /> if (( fl=open(p,O_RDONLY)) <0)<br /> <br /> {<br /> <br /> printf(quot;
SERVER : %s not foundquot;
);<br /> <br /> exit(1);<br /> <br /> }<br /> <br /> else<br /> <br /> {<br /> <br /> printf(quot;
SERVER : %s found ! transfering the contents quot;
,p);<br /> <br /> stdin=fdopen(fl,quot;
rquot;
);<br /> <br /> if(fgets(c,300,stdin) != NULL)<br /> <br /> {<br /> <br /> fd2=open(FIFO2_NAME, O_WRONLY);<br /> <br /> if(num2=write(fd2,c,strlen(c))== -1)<br /> perror(quot;
Transfer errorquot;
);<br /> <br /> else<br /> <br /> printf(quot;
SERVER : Transfer completed quot;
);<br /> <br /> }<br /> <br /> else<br /> <br /> perror(quot;
read errorquot;
);<br /> <br /> exit(1);<br /> }<br /> }<br />}<br />}<br />5.Frame<br />#include<stdio.h><br />struct frame<br />{<br />int fslno;<br />char finfo[20];<br />};<br />struct frame arr[10];<br />int n;<br />void sort()<br />{<br />int i,j,ex;<br />struct frame temp;<br />for(i=0;i<n-1;i++)<br />{<br />ex=0;<br />for(j=0;j<n-i-1;j++)<br />if(arr[j].fslno>arr[j+1].fslno)<br />{<br />temp=arr[j];<br />arr[j]=arr[j+1];<br />arr[j+1]=temp;<br />ex++;<br />}<br />}<br />}<br />int main()<br />{<br />int i;<br />system(quot;
clearquot;
);<br />printf(quot;
enter the number of framesquot;
);<br />scanf(quot;
%dquot;
,&n);<br />printf(quot;
enter the frame sequence number and frame contentsquot;
);<br />for(i=0;i<n;i++)<br />scanf(quot;
%d%squot;
,&arr[i].fslno,&arr[i].finfo);<br />sort();<br />printf(quot;
the frames in sequencequot;
);<br />for(i=0;i<n;i++)<br />{<br />printf(quot;
01111110 %d%s 01111110quot;
,arr[i].fslno,arr[i].finfo);<br />printf(quot;
|---------------------------------------------|quot;
);<br />}<br />}<br />6. Hamming code<br />#include<stdio.h><br />#include<math.h><br />main()<br />{<br />int i,j,k,count,error_pos=0,flag=0;<br />char dw[20],cw[20],data[20];<br />printf(quot;
enter the data as binary bit stream 7 bitsquot;
);<br />scanf(quot;
%squot;
,data);<br />for(i=1,j=0,k=0;i<12;i++)<br />{<br />if(i==(int)pow(2,j))<br />{<br />dw[i]='?'; <br />j++;<br />}<br />else<br />{<br />dw[i]=data[k];<br />k++;<br />}<br />}<br />for(i=0;i<4;i++)<br /> <br /> {<br />count=0;<br />for(j=(int)pow(2,i);j<12;j+=(int)pow(2,i))<br />{<br />for(k=0;k<(int)pow(2,i);k++)<br />{<br />if(dw[j]=='1')<br /> count++; <br />j++; <br />}<br />}<br />if(count%2 == 0)<br />dw[(int)pow(2,i)]='0';<br />else<br />dw[(int)pow(2,i)]='1';<br />}<br />printf(quot;
CODE WORD is quot;
);<br />for(i=1;i<12;i++)<br />printf(quot;
%cquot;
,dw[i]);<br />printf(quot;
enter the received hamming codequot;
);<br />scanf(quot;
%squot;
,cw);<br />for(i=12;i>0;i--)<br />cw[i]=cw[i-1];<br />for(i=0;i<4;i++)<br />{<br />count=0;<br /> for(j=(int)pow(2,i);j<12;j+=(int)pow(2,i))<br /> <br /> {<br /> <br /> for(k=0;k<(int)pow(2,i);k++)<br /> <br /> {<br /> <br />if(cw[j]=='1')<br /> <br /> count++;<br /> <br /> j++;<br /> <br /> }<br />}<br /> <br /> if(count%2 != 0)<br /> <br /> error_pos=error_pos+(int)pow(2,i);<br />}<br />if(error_pos ==0)<br />printf(quot;
There is no ERROR in received Code Wordquot;
);<br />else<br />{ <br />if(cw[error_pos]==dw[error_pos])<br />{<br /> <br /> printf(quot;
There are TWO or MORE Errors in received Code Wordquot;
);<br /> <br /> printf(quot;
SORRY........! Hamming code cannot correct TWO or MORE Errorsquot;
);<br /> <br /> flag=1;<br />} <br />else<br />printf(quot;
There is an Error in bit position %d of received Code Word quot;
,error_pos);<br />if(flag==0)<br />{<br />if(cw[error_pos]=='1')<br />cw[error_pos]='0';<br />else<br />cw[error_pos]='1';<br />printf(quot;
CORRECTED CODE WORD is quot;
);<br />for(i=1;i<12;i++)<br />printf(quot;
%cquot;
,cw[i]);<br />}<br />}<br />printf(quot;
quot;
);<br />}<br />7. Leaky bucket<br />#include<stdio.h><br />#include<math.h><br />#include<stdlib.h><br />int t_rand(int a)<br />{<br />int rn;<br />rn=random()%10;<br />rn=rn%a;<br />if(rn==0)<br />rn=1;<br />return(rn);<br />}<br />int main()<br />{<br />int packets[5],i,j,clk,b_size,o_rate,p_sz_rm=0,p_sz,p_time;<br />int flag=0;<br />system(quot;
clearquot;
);<br />for(i=0;i<5;++i)<br />packets[i]=t_rand(6)*10;<br />printf(quot;
enter the output ratequot;
);<br />scanf(quot;
%dquot;
,&o_rate);<br />printf(quot;
enter the bucket sizequot;
);<br />scanf(quot;
%dquot;
,&b_size);<br />for(i=0;i<5;++i)<br />{<br />if((packets[i]+p_sz_rm)>b_size)<br />{<br />if(packets[i]>b_size)<br />printf(quot;
incoming packet size (%d) is greater than bucket capacity %d p/c rejectedquot;
,packets[i],b_size);<br />else<br />printf(quot;
bucket capacity exceededquot;
);<br />}<br />else<br />{<br /> <br /> for(j=0;;++j)<br />{<br />p_sz=packets[i];<br />p_sz_rm+=p_sz;<br />printf(quot;
new incoming packet size %dquot;
,p_sz);<br />printf(quot;
byte-transmissions left:%dquot;
,p_sz_rm);<br />p_time=t_rand(5)*10;<br />printf(quot;
next packet will come at %dquot;
,p_time);<br />for(clk=0;clk<=p_time;clk+=1)<br />{<br />printf(quot;
time left:%dquot;
,p_time-clk);<br />sleep(1);<br />if(p_sz_rm>=o_rate)<br />{<br />printf(quot;
%d bytes transmittedquot;
,o_rate);<br />p_sz_rm-=o_rate;<br />printf(quot;
bytes remaining:%dquot;
,p_sz_rm);<br />}<br />else<br />printf(quot;
no packets to transmit!!quot;
);<br />}<br />if(p_sz_rm!=0)<br />flag=1;<br />break;<br />}<br />}<br />}<br />return(0);<br />}<br />8. RSA<br />#include<stdio.h><br />#include<string.h><br />#include<math.h><br />int mul(unsigned int x,unsigned int y,unsigned int n)<br />{<br />unsigned long int k=1;<br />int j;<br />for(j=1;j<=y;j++)<br />k=(k*x)%n;<br />return(unsigned int)k;<br />}<br />int main()<br />{<br />int i;<br />char msg[100];<br />unsigned int pt[100],ct[100],n,d,e;<br />printf(quot;
enter msg quot;
);<br />scanf(quot;
%[^]quot;
,&msg);<br />for(i=0;i<strlen(msg);i++)<br />{<br />pt[i]=msg[i];<br />}<br />n=253;<br />d=17;<br />e=13;<br />for(i=0;i<strlen(msg);i++)<br />{<br />ct[i]=mul(pt[i],e,n);<br />}<br />printf(quot;
Cipher Text=quot;
);<br />for(i=0;i<strlen(msg);i++)<br />{<br />printf(quot;
%dquot;
,ct[i]);<br />}<br />for(i=0;i<strlen(msg);i++)<br />{<br />pt[i]=mul(ct[i],d,n);<br />}<br />printf(quot;
Plain Text=quot;
);<br />for(i=0;i<strlen(msg);i++)<br />{<br />printf(quot;
%cquot;
,pt[i]);<br />}<br />printf(quot;
quot;
);<br />}<br />9. TCP client<br />#include<stdio.h><br />#include<sys/types.h><br />#include<sys/socket.h><br />#include<netinet/in.h><br />#include<netdb.h><br />#include<string.h><br />int main(int argc,char *argv[])<br />{<br />int sockfd,newsockfd,portno,len,n;<br />char buffer[256],c[20000];<br />struct sockaddr_in serv,cli;<br />FILE *fd;<br />if(argc<2)<br />{<br />printf(quot;
error :no port no usage: ./client portnumber ex:./client 7777quot;
);<br />exit(1);<br />}<br />sockfd=socket(AF_INET , SOCK_STREAM,0);<br />bzero((char *)&serv,sizeof(serv));<br />portno=atoi(argv[1]);<br />serv.sin_family=AF_INET;<br />serv.sin_port=htons(portno);<br />if(connect(sockfd,(struct sockaddr *)&serv,sizeof(serv))<0)<br />{<br />printf(quot;
server not responding.. i am to terminate.quot;
);<br />exit(1);<br />}<br />printf(quot;
enter the file with complete pathquot;
);<br />scanf(quot;
%squot;
,&buffer);<br />if(write(sockfd,buffer,strlen(buffer))<0)<br />printf(quot;
error writing to sock...quot;
);<br />bzero(c,2000);<br />printf(quot;
reading.........quot;
);<br />if(read(sockfd,c,1999)<0)<br />printf(quot;
error:read error..quot;
);<br />printf(quot;
client :displaying content of %s....quot;
,buffer);<br />fputs(c,stdout);<br />printf(quot;
....quot;
);<br />return 0;<br />}<br />10. TCP server<br />#include<stdio.h><br />#include<sys/types.h><br />#include<sys/socket.h><br />#include<netinet/in.h><br />#include<netdb.h><br />int main(int argc ,char *argv[])<br />{<br />int sockfd,newsockfd,portno,len,n;<br />char buffer[256],c[2000],cc[20000];<br />struct sockaddr_in serv,cli;<br />FILE *fd;<br />if(argc<2)<br />{<br />printf(quot;
error: no port no usage: ./server port num quot;
);<br />exit(1);<br />}<br />sockfd=socket(AF_INET,SOCK_STREAM,0);<br />portno=atoi(argv[1]);<br />serv.sin_family=AF_INET;<br />serv.sin_addr.s_addr=INADDR_ANY;<br />serv.sin_port=htons(portno);<br />bind(sockfd,(struct sockaddr *)&serv,sizeof(serv));<br />listen(sockfd,10);<br />len=sizeof(cli);<br />printf(quot;
server: waiting for connection..quot;
);<br />newsockfd=accept(sockfd,(struct sockaddr *)&cli,&len);<br />bzero(buffer,255);<br />n=read(newsockfd,buffer,255);<br />printf(quot;
server recv:%squot;
,buffer);<br />if((fd=fopen(buffer,quot;
rquot;
))!=NULL)<br />{<br />printf(quot;
server:%s found opening and reading...quot;
,buffer);<br />printf(quot;
reading.........reading completequot;
);<br />fgets(cc,2000,fd);<br />while(!feof(fd))<br />{<br />fgets(c,2000,fd);<br />strcat(cc,c);<br />}<br />n=write(newsockfd,cc,strlen(cc));<br />if(n<0)<br />printf(quot;
error writing to socket..quot;
);<br />printf(quot;
transfer complete.quot;
);<br />}<br />else<br />{<br />printf(quot;
server:file not found!quot;
);<br />n=write(newsockfd,quot;
file not found!quot;
,15);<br />if(n<0)<br />printf(quot;
error: writing to socket..quot;
);<br />}<br />return 0;<br />}<br />