• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Network lap pgms 7th semester

Network lap pgms 7th semester






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds


Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Network lap pgms 7th semester Network lap pgms 7th semester Document Transcript

    • 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]="10001000000100001",res[17]="0000000000000000";<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]='0';<br />printf("ncrc is %sn",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("ntttAT SOURCE NODEnnenter the data to be send :");<br /> gets(data);<br /> strcpy(concatdata,data);<br /> strcat(concatdata,"0000000000000000");<br /> for(i=0;i<=16;i++)<br />divident[i]=concatdata[i];<br /> divident[i+1]='0';<br /> crc_cal(0);<br /> printf("ndata is :t");<br /> puts(data);<br /> printf("nthe frame transmitted is :t");<br /> printf("n%s%s",data,src_crc);<br /> printf("nttSOURCE NODE TRANSMITTED THE FRAME ---->");<br /> printf("nnnntttAT DESTINATION NODEnenter the received frame:t");<br /> gets(frame);<br /> for(i=0;i<=16;i++)<br />divident[i]=frame[i];<br /> divident[i+1]='0';<br /> crc_cal(1);<br /> if(strcmp(dest_crc,res)==0)<br />printf("nreceived frame is error free ");<br /> else<br />printf("nreceived frame has one or more error");<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("nenter the no of vertices:t");<br />scanf("%d",&n);<br />printf("nenter the cost matrixn");<br />for(i=0;i<n;i++)<br />for(j=0;j<n;j++)<br />scanf("%d",&cost[i][j]);<br />distvector();<br />for(i=0;i<n;i++)<br />{<br />printf("nstate value for router %c n",i+65);<br />printf("ndestnodetnextnodetdistancen");<br />for(j=0;j<n;j++)<br />{<br />if(table[i].dist[j]==99)<br />printf("%ctt-ttinfiniten",j+65);<br />else<br />printf("%ctt%ctt%dn",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 "fifo1"<br />#define FIFO2_NAME "fifo2"<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("n Waiting for SERVER ! nn");<br />fd=open(FIFO1_NAME,O_WRONLY);<br />printf("Server online !n client:Enter the path n");<br />while(gets(p), !feof(stdin))<br />{<br /> <br />if ((num = write(fd,p,strlen(p))) == -1)<br /> <br /> perror("write errorn");<br /> <br /> else<br /> {<br /> printf("waiting for reply ...n");<br /> <br /> fd2=open(FIFO2_NAME, O_RDONLY);<br /> <br /> if ((num2=read(fd2,c,300))== -1)<br /> <br /> perror("Transfer errorn");<br /> <br /> else<br /> <br /> {<br /> <br /> printf("File receiver ! Displaying the contents :n");<br /> <br /> if (fputs(c,stdout)== EOF)<br /> <br /> perror("print error n");<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 "fifo1"<br />#define FIFO2_NAME "fifo2"<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("SERVER Online ! nn");<br />fd=open(FIFO1_NAME,O_RDONLY);<br />printf("client online !@n waiting for request ...nn");<br />while(1)<br />{<br /> <br />if ((num = read(fd,p,100)) == -1)<br /> <br /> perror("read errorn");<br /> <br /> else<br /> <br />{<br /> <br /> p[num]='0';<br /> <br /> if (( fl=open(p,O_RDONLY)) <0)<br /> <br /> {<br /> <br /> printf("n SERVER : %s not foundn");<br /> <br /> exit(1);<br /> <br /> }<br /> <br /> else<br /> <br /> {<br /> <br /> printf("SERVER : %s found ! n transfering the contents n",p);<br /> <br /> stdin=fdopen(fl,"r");<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("Transfer errorn");<br /> <br /> else<br /> <br /> printf("SERVER : Transfer completed n");<br /> <br /> }<br /> <br /> else<br /> <br /> perror("read errorn");<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("clear");<br />printf("enter the number of framesn");<br />scanf("%d",&n);<br />printf("enter the frame sequence number and frame contentsn");<br />for(i=0;i<n;i++)<br />scanf("%d%s",&arr[i].fslno,&arr[i].finfo);<br />sort();<br />printf("the frames in sequencen");<br />for(i=0;i<n;i++)<br />{<br />printf("01111110 %dt%s 01111110n",arr[i].fslno,arr[i].finfo);<br />printf("|---------------------------------------------|n");<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("enter the data as binary bit stream 7 bitsn");<br />scanf("%s",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("n CODE WORD is nn");<br />for(i=1;i<12;i++)<br />printf("%c",dw[i]);<br />printf("nnenter the received hamming codenn");<br />scanf("%s",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("nn There is no ERROR in received Code Wordn");<br />else<br />{ <br />if(cw[error_pos]==dw[error_pos])<br />{<br /> <br /> printf("nnThere are TWO or MORE Errors in received Code Wordnn");<br /> <br /> printf("SORRY........! Hamming code cannot correct TWO or MORE Errorsn");<br /> <br /> flag=1;<br />} <br />else<br />printf("nThere is an Error in bit position %d of received Code Word nn",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("nnCORRECTED CODE WORD is nn");<br />for(i=1;i<12;i++)<br />printf("%c",cw[i]);<br />}<br />}<br />printf("nn");<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("clear");<br />for(i=0;i<5;++i)<br />packets[i]=t_rand(6)*10;<br />printf("n enter the output raten");<br />scanf("%d",&o_rate);<br />printf("n enter the bucket sizen");<br />scanf("%d",&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("n incoming packet size (%d) is greater than bucket capacity %dn p/c rejected",packets[i],b_size);<br />else<br />printf("n bucket capacity exceeded");<br />}<br />else<br />{<br /> <br /> for(j=0;;++j)<br />{<br />p_sz=packets[i];<br />p_sz_rm+=p_sz;<br />printf("nnew incoming packet size %d",p_sz);<br />printf("nbyte-transmissions left:%d",p_sz_rm);<br />p_time=t_rand(5)*10;<br />printf("n next packet will come at %d",p_time);<br />for(clk=0;clk<=p_time;clk+=1)<br />{<br />printf("n time left:%d",p_time-clk);<br />sleep(1);<br />if(p_sz_rm>=o_rate)<br />{<br />printf("n%d bytes transmitted",o_rate);<br />p_sz_rm-=o_rate;<br />printf("n bytes remaining:%d",p_sz_rm);<br />}<br />else<br />printf("nno packets to transmit!!");<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("enter msg n");<br />scanf("%[^n]",&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("n Cipher Text=");<br />for(i=0;i<strlen(msg);i++)<br />{<br />printf("%d",ct[i]);<br />}<br />for(i=0;i<strlen(msg);i++)<br />{<br />pt[i]=mul(ct[i],d,n);<br />}<br />printf("n Plain Text=");<br />for(i=0;i<strlen(msg);i++)<br />{<br />printf("%c",pt[i]);<br />}<br />printf("n");<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("error :no port non usage:n ./client portnumbern ex:./client 7777n");<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("server not responding.. nnnt i am to terminate.n");<br />exit(1);<br />}<br />printf("enter the file with complete pathn");<br />scanf("%s",&buffer);<br />if(write(sockfd,buffer,strlen(buffer))<0)<br />printf("error writing to sock...");<br />bzero(c,2000);<br />printf(" reading......n...n");<br />if(read(sockfd,c,1999)<0)<br />printf("error:read error..n");<br />printf("client :displaying content of %sn....n",buffer);<br />fputs(c,stdout);<br />printf("n....n");<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(" error: no port non usage:n ./server port num n");<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("server:n waiting for connection..");<br />newsockfd=accept(sockfd,(struct sockaddr *)&cli,&len);<br />bzero(buffer,255);<br />n=read(newsockfd,buffer,255);<br />printf("n server recv:%sn",buffer);<br />if((fd=fopen(buffer,"r"))!=NULL)<br />{<br />printf("server:%s found n opening and reading...n",buffer);<br />printf("reading....n.....reading complete");<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("error writing to socket..");<br />printf("n transfer complete.n");<br />}<br />else<br />{<br />printf("n server:file not found!n");<br />n=write(newsockfd,"file not found!",15);<br />if(n<0)<br />printf("error: writing to socket..");<br />}<br />return 0;<br />}<br />