0% found this document useful (0 votes)
275 views40 pages

Network Lab Programs For 7th Sem Vtu

The document contains program code for 10 different network lab programs: 1) CRC calculation, 2) Distance vector routing, 3) FIFO client, 4) FIFO server, 5) Frame sorting, 6) Hamming code encoding/decoding, 7) Leaky bucket algorithm, 8) RSA encryption/decryption, 9) TCP client, and 10) TCP server. The programs demonstrate various networking concepts like error detection, routing, inter-process communication, data structures, encryption, and transport layer protocols.

Uploaded by

Reshma BJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
275 views40 pages

Network Lab Programs For 7th Sem Vtu

The document contains program code for 10 different network lab programs: 1) CRC calculation, 2) Distance vector routing, 3) FIFO client, 4) FIFO server, 5) Frame sorting, 6) Hamming code encoding/decoding, 7) Leaky bucket algorithm, 8) RSA encryption/decryption, 9) TCP client, and 10) TCP server. The programs demonstrate various networking concepts like error detection, routing, inter-process communication, data structures, encryption, and transport layer protocols.

Uploaded by

Reshma BJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

NETWORK LAB PROGRAMS

1.CRC
#include<stdio.h>
#include<string.h>
char
data[100],concatdata[117],src_crc[17],dest_crc[17],frame[120],divident[18],divisor[18]="100010000001
00001",res[17]="0000000000000000";

void crc_cal(int node)


{
int i,j;
for(j=17;j<=strlen(concatdata);j++)
{
if(divident[0]=='1')
{
for(i=1;i<=16;i++)
if(divident[i]!=divisor[i])
divident[i-1]='1';
else
divident[i-1]='0';
}
else
{
for(i=1;i<=16;i++)
divident[i-1]=divident[i];
}

if(node==0)
divident[i-1]=concatdata[j];
else
divident[i-1]=frame[j];
}
divident[i-1]='\0';

printf("\ncrc is %s\n",divident);
if(node==0)
{
strcpy(src_crc,divident);
}
else
strcpy(dest_crc,divident);
}

int main()
{
int i,len,rest;
printf("\n\t\t\tAT SOURCE NODE\n\nenter the data to be send :");
gets(data);
strcpy(concatdata,data);
strcat(concatdata,"0000000000000000");
for(i=0;i<=16;i++)
divident[i]=concatdata[i];

divident[i+1]='\0';
crc_cal(0);

printf("\ndata is :\t");
puts(data);
printf("\nthe frame transmitted is :\t");
printf("\n%s%s",data,src_crc);
printf("\n\t\tSOURCE NODE TRANSMITTED THE FRAME ---->");

printf("\n\n\n\n\t\t\tAT DESTINATION NODE\nenter the received frame:\t");


gets(frame);
for(i=0;i<=16;i++)
divident[i]=frame[i];
divident[i+1]='\0';
crc_cal(1);

if(strcmp(dest_crc,res)==0)
printf("\nreceived frame is error free ");
else
printf("\nreceived frame has one or more error");

return 1;
}

2.Distance vector
#include<stdio.h>
struct rtable
{
int dist[20],nextnode[20];
}table[20];
int cost[10][10],n;
void distvector()
{
int i,j,k,count=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
table[i].dist[j]=cost[i][j];
table[i].nextnode[j]=j;
}
}

do
{
count=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)

{
for(k=0;k<n;k++)
{
if(table[i].dist[j]>cost[i][k]+table[k].dist[j])
{
table[i].dist[j]=table[i].dist[k]+table[k].dist[j];
table[i].nextnode[j]=k;
count++;
}
}
}
}
}while(count!=0);
}
int main()
{
int i,j;
printf("\nenter the no of vertices:\t");
scanf("%d",&n);
printf("\nenter the cost matrix\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
distvector();
for(i=0;i<n;i++)

{
printf("\nstate value for router %c \n",i+65);
printf("\ndestnode\tnextnode\tdistance\n");
for(j=0;j<n;j++)
{
if(table[i].dist[j]==99)
printf("%c\t\t-\t\tinfinite\n",j+65);
else
printf("%c\t\t%c\t\t%d\n",j+65,table[i].nextnode[j]+65,table[i].dist[j]);
}
}
return 0;
}

3.FIFO Client
#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#define FIFO1_NAME "fifo1"


#define FIFO2_NAME "fifo2"

int main()
{
char p[100],f[100],c[100];

int num,num2,fl,fd,fd2;

mknod(FIFO1_NAME,S_IFIFO | 0666, 0);

mknod(FIFO2_NAME,S_IFIFO | 0666,0);

printf("\n Waiting for SERVER ! \n\n");

fd=open(FIFO1_NAME,O_WRONLY);

printf("Server online !\n client:Enter the path \n");

while(gets(p), !feof(stdin))

if ((num = write(fd,p,strlen(p))) == -1)

perror("write error\n");

else
{
printf("waiting for reply ...\n");

fd2=open(FIFO2_NAME, O_RDONLY);

if ((num2=read(fd2,c,300))== -1)

perror("Transfer error\n");

else

printf("File receiver ! Displaying the contents :\n");

if (fputs(c,stdout)== EOF)

perror("print error \n");

exit(1);

}
}

4.FIFO Server
#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#define FIFO1_NAME "fifo1"

#define FIFO2_NAME "fifo2"

int main()

char p[100],f[100],c[100];

int num,num2,fl,fd,fd2;

mknod(FIFO1_NAME,S_IFIFO | 0666, 0);

mknod(FIFO2_NAME,S_IFIFO | 0666,0);

printf("SERVER Online ! \n\n");

fd=open(FIFO1_NAME,O_RDONLY);

printf("client online !@\n waiting for request ...\n\n");

while(1)

if ((num = read(fd,p,100)) == -1)

perror("read error\n");

else

p[num]='\0';

if (( fl=open(p,O_RDONLY)) <0)

printf("\n SERVER : %s not found\n");

exit(1);

else

printf("SERVER : %s found ! \n transfering the contents \n",p);

stdin=fdopen(fl,"r");

if(fgets(c,300,stdin) != NULL)

fd2=open(FIFO2_NAME, O_WRONLY);

if(num2=write(fd2,c,strlen(c))== -1)

perror("Transfer error\n");

else

printf("SERVER : Transfer completed \n");

else

perror("read error\n");

exit(1);

}
}
}

5.Frame
#include<stdio.h>

struct frame

int fslno;

char finfo[20];

};

struct frame arr[10];

int n;

void sort()
{

int i,j,ex;

struct frame temp;

for(i=0;i<n-1;i++)

ex=0;

for(j=0;j<n-i-1;j++)

if(arr[j].fslno>arr[j+1].fslno)

temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

ex++;

int main()

int i;

system("clear");

printf("enter the number of frames\n");

scanf("%d",&n);

printf("enter the frame sequence number and frame contents\n");

for(i=0;i<n;i++)

scanf("%d%s",&arr[i].fslno,&arr[i].finfo);

sort();
printf("the frames in sequence\n");

for(i=0;i<n;i++)

printf("01111110 %d\t%s 01111110\n",arr[i].fslno,arr[i].finfo);

printf("|---------------------------------------------|\n");

6. Hamming code
#include<stdio.h>

#include<math.h>

main()

int i,j,k,count,error_pos=0,flag=0;

char dw[20],cw[20],data[20];

printf("enter the data as binary bit stream 7 bits\n");

scanf("%s",data);

for(i=1,j=0,k=0;i<12;i++)

if(i==(int)pow(2,j))

dw[i]='?';

j++;

else

dw[i]=data[k];

k++;

for(i=0;i<4;i++)

count=0;

for(j=(int)pow(2,i);j<12;j+=(int)pow(2,i))

for(k=0;k<(int)pow(2,i);k++)

if(dw[j]=='1')

count++;

j++;

if(count%2 == 0)

dw[(int)pow(2,i)]='0';

else

dw[(int)pow(2,i)]='1';

printf("\n CODE WORD is \n\n");

for(i=1;i<12;i++)

printf("%c",dw[i]);

printf("\n\nenter the received hamming code\n\n");

scanf("%s",cw);

for(i=12;i>0;i--)

cw[i]=cw[i-1];

for(i=0;i<4;i++)

count=0;

for(j=(int)pow(2,i);j<12;j+=(int)pow(2,i))

for(k=0;k<(int)pow(2,i);k++)

if(cw[j]=='1')

count++;

j++;

if(count%2 != 0)

error_pos=error_pos+(int)pow(2,i);

if(error_pos ==0)

printf("\n\n There is no ERROR in received Code Word\n");

else
{

if(cw[error_pos]==dw[error_pos])

printf("\n\nThere are TWO or MORE Errors in received Code Word\n\n");

printf("SORRY........! Hamming code cannot correct TWO or MORE Errors\n");

flag=1;

else

printf("\nThere is an Error in bit position %d of received Code Word \n\n",error_pos);

if(flag==0)

if(cw[error_pos]=='1')

cw[error_pos]='0';

else

cw[error_pos]='1';

printf("\n\nCORRECTED CODE WORD is \n\n");

for(i=1;i<12;i++)

printf("%c",cw[i]);

}
}

printf("\n\n");

7. Leaky bucket

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

int t_rand(int a)

int rn;

rn=random()%10;

rn=rn%a;

if(rn==0)

rn=1;

return(rn);

int main()

int packets[5],i,j,clk,b_size,o_rate,p_sz_rm=0,p_sz,p_time;

int flag=0;

system("clear");

for(i=0;i<5;++i)

packets[i]=t_rand(6)*10;

printf("\n enter the output rate\n");

scanf("%d",&o_rate);

printf("\n enter the bucket size\n");

scanf("%d",&b_size);

for(i=0;i<5;++i)

if((packets[i]+p_sz_rm)>b_size)

if(packets[i]>b_size)

printf("\n incoming packet size (%d) is greater than bucket capacity %d\n p/c
rejected",packets[i],b_size);

else
printf("\n bucket capacity exceeded");

else

for(j=0;;++j)

p_sz=packets[i];

p_sz_rm+=p_sz;

printf("\nnew incoming packet size %d",p_sz);

printf("\nbyte-transmissions left:%d",p_sz_rm);

p_time=t_rand(5)*10;

printf("\n next packet will come at %d",p_time);

for(clk=0;clk<=p_time;clk+=1)

printf("\n time left:%d",p_time-clk);

sleep(1);

if(p_sz_rm>=o_rate)

printf("\n%d bytes transmitted",o_rate);

p_sz_rm-=o_rate;

printf("\n bytes remaining:%d",p_sz_rm);

else

printf("\nno packets to transmit!!");

if(p_sz_rm!=0)

flag=1;

break;
}
}
}
return(0);
}

8. RSA
#include<stdio.h>

#include<string.h>

#include<math.h>

int mul(unsigned int x,unsigned int y,unsigned int n)

unsigned long int k=1;

int j;

for(j=1;j<=y;j++)

k=(k*x)%n;

return(unsigned int)k;

int main()

int i;

char msg[100];

unsigned int pt[100],ct[100],n,d,e;

printf("enter msg \n");

scanf("%[^\n]",&msg);

for(i=0;i<strlen(msg);i++)

pt[i]=msg[i];

n=253;

d=17;

e=13;

for(i=0;i<strlen(msg);i++)

ct[i]=mul(pt[i],e,n);

printf("\n Cipher Text=");

for(i=0;i<strlen(msg);i++)

printf("%d",ct[i]);

for(i=0;i<strlen(msg);i++)

pt[i]=mul(ct[i],d,n);

printf("\n Plain Text=");

for(i=0;i<strlen(msg);i++)

printf("%c",pt[i]);

printf("\n");

9. TCP client
#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<netdb.h>

#include<string.h>

int main(int argc,char *argv[])

int sockfd,newsockfd,portno,len,n;
char buffer[256],c[20000];

struct sockaddr_in serv,cli;

FILE *fd;

if(argc<2)

printf("error :no port no\n usage:\n ./client portnumber\n ex:./client 7777\n");

exit(1);

sockfd=socket(AF_INET , SOCK_STREAM,0);

bzero((char *)&serv,sizeof(serv));

portno=atoi(argv[1]);

serv.sin_family=AF_INET;

serv.sin_port=htons(portno);

if(connect(sockfd,(struct sockaddr *)&serv,sizeof(serv))<0)

printf("server not responding.. \n\n\n\t i am to terminate.\n");

exit(1);

printf("enter the file with complete path\n");

scanf("%s",&buffer);

if(write(sockfd,buffer,strlen(buffer))<0)

printf("error writing to sock...");

bzero(c,2000);

printf(" reading......\n...\n");

if(read(sockfd,c,1999)<0)

printf("error:read error..\n");
printf("client :displaying content of %s\n....\n",buffer);
fputs(c,stdout);
printf("\n....\n");
return 0;
}

10. TCP server


#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<netdb.h>

int main(int argc ,char *argv[])

int sockfd,newsockfd,portno,len,n;

char buffer[256],c[2000],cc[20000];

struct sockaddr_in serv,cli;

FILE *fd;
if(argc<2)

printf(" error: no port no\n usage:\n ./server port num \n");

exit(1);

sockfd=socket(AF_INET,SOCK_STREAM,0);

portno=atoi(argv[1]);

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=INADDR_ANY;
serv.sin_port=htons(portno);

bind(sockfd,(struct sockaddr *)&serv,sizeof(serv));

listen(sockfd,10);
len=sizeof(cli);

printf("server:\n waiting for connection..");

newsockfd=accept(sockfd,(struct sockaddr *)&cli,&len);

bzero(buffer,255);

n=read(newsockfd,buffer,255);

printf("\n server recv:%s\n",buffer);

if((fd=fopen(buffer,"r"))!=NULL)

printf("server:%s found \n opening and reading...\n",buffer);

printf("reading....\n.....reading complete");

fgets(cc,2000,fd);

while(!feof(fd))

fgets(c,2000,fd);
strcat(cc,c);

n=write(newsockfd,cc,strlen(cc));

if(n<0)
printf("error writing to socket..");

printf("\n transfer complete.\n");

else
{
printf("\n server:file not found!\n");

n=write(newsockfd,"file not found!",15);

if(n<0)
printf("error: writing to socket..");

return 0;

You might also like