SlideShare a Scribd company logo
1 of 40
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 />
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester
Network lap pgms 7th semester

More Related Content

What's hot

Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]DEEPIKA T
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expressionAkhil Ahuja
 
Set data structure
Set data structure Set data structure
Set data structure Tech_MX
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programsKandarp Tiwari
 
Introduction to Data Structures & Algorithms
Introduction to Data Structures & AlgorithmsIntroduction to Data Structures & Algorithms
Introduction to Data Structures & AlgorithmsAfaq Mansoor Khan
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queuezzzubair
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2Tech_MX
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp AlgorithmSohail Ahmed
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignmentAbdullah Al Shiam
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
آموزش ساختمان داده ها - بخش سوم
آموزش ساختمان داده ها - بخش سومآموزش ساختمان داده ها - بخش سوم
آموزش ساختمان داده ها - بخش سومfaradars
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest pathArafat Hossan
 

What's hot (20)

Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
Set data structure
Set data structure Set data structure
Set data structure
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
Introduction to Data Structures & Algorithms
Introduction to Data Structures & AlgorithmsIntroduction to Data Structures & Algorithms
Introduction to Data Structures & Algorithms
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2
 
Python: Polymorphism
Python: PolymorphismPython: Polymorphism
Python: Polymorphism
 
8 number-system
8 number-system8 number-system
8 number-system
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithms
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Linked list
Linked listLinked list
Linked list
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp Algorithm
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
آموزش ساختمان داده ها - بخش سوم
آموزش ساختمان داده ها - بخش سومآموزش ساختمان داده ها - بخش سوم
آموزش ساختمان داده ها - بخش سوم
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
 

Similar to Network lap pgms 7th semester

Similar to Network lap pgms 7th semester (20)

pointers 1
pointers 1pointers 1
pointers 1
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab File
 
Vcs16
Vcs16Vcs16
Vcs16
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
 
Os lab upto_1st_mid
Os lab upto_1st_midOs lab upto_1st_mid
Os lab upto_1st_mid
 
Os lab upto 1st mid
Os lab upto 1st midOs lab upto 1st mid
Os lab upto 1st mid
 
C program
C programC program
C program
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Unix Programs
Unix ProgramsUnix Programs
Unix Programs
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
Dti2143 lab sheet 8
Dti2143 lab sheet 8Dti2143 lab sheet 8
Dti2143 lab sheet 8
 
pattern-printing-in-c.pdf
pattern-printing-in-c.pdfpattern-printing-in-c.pdf
pattern-printing-in-c.pdf
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 

Recently uploaded

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 

Recently uploaded (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

Network lap pgms 7th semester

  • 1. 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 />