Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Networks Laboratory (06CSL77)

NETWORKS LABORATORY PART B PROGRAMS IN C(06CSL77) Program 1. Write a C program for error detecting code-using CRC-CCITT (16-bits). #include<stdio.h> #include<unistd.h> #include<string.h> int crc(char *input, char *output, char *gp, int mode) { int j, k; strcpy(output, input); if(mode) { for(j=1; j<strlen(gp); j++) strcat(output,"0"); } for(j=0; j<strlen(input); j++) if(*(output+j) == '1') for(k=0; k<strlen(gp); k++) { if (((*(output+j+k) =='0') && (gp[k] == '0')|| (*(output+j+k) == '1') && (gp[k] =='1'))) *(output+j+k)='0'; else *(output+j+k)='1'; } for(j=0; j<strlen(output); j++) if(output[j] == '1') return 1; return 0; } int main() { char input[50],output[50]; char recv[50], gp[50]; system("clear");
Dept. of CSE, NHCE Aug-Nov 2012 Page 1

Networks Laboratory (06CSL77)


printf("\n Enter the input message in binary\n"); scanf("%s",input); printf("\n Enter the generator polynomial\n"); scanf("%s",gp); crc(input,output,gp,1); printf("\n The transmitted message is %s %s\n",input, output+strlen (input)); printf("\n\n Enter the received message in binary \n"); scanf("%s",recv); if(!crc(recv,output,gp,0)) printf("\n No error in data\n"); else printf("\n Error in data transmission has occurred\n"); } Compile and run $ cc o crc crc.c $ ./crc

$ Enter the input message in binary 1101011011 $ Enter the generator polynomial 10011 The transmitted message is 1101011011 1110 Enter the received message in binary 11010110111110 No error in data Enter the received message in binary 11011110111110 Error in data transmission has occurred

Dept. of CSE, NHCE

Aug-Nov 2012

Page 2

Networks Laboratory (06CSL77)


Program 2. Write a program for frame sorting technique used in the buffers. #include<stdio.h> #include<string.h> struct frame { int fno; char fdata[25]; }; struct frame arr[20]; int n; void sort() { int i,j; struct frame temp; for(i=0;i<n;i++) { for(j=0;j<n-i-1;j++) if(arr[j].fno > arr[j+1].fno) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } int main() { int i; system("clear"); printf("\n Enter the number of frames :"); scanf("%d",&n); printf("\n Enter the frame sequence number and frame content\n"); for(i=0;i<n;i++) scanf("%d%s",&arr[i].fno,arr[i].fdata); sort(); printf("\n The frames in sequence are\n"); for(i=0;i<n;i++)
Dept. of CSE, NHCE Aug-Nov 2012 Page 3

Networks Laboratory (06CSL77)


{ printf(" %d\t%s\n",arr[i].fno,arr[i].fdata); } } Compile and run $ cc o frame frame.c $ ./frame $ Enter the number of frames : 3 $ Enter the frame sequence number and frame content 3 abc 1 pqr 2 xyz The frames in sequence are 1 pqr 2 xyz 3 abc

Program 3.Write a program for Distance Vector Algorithm to find suitable path for transmission. #include<stdio.h> #include<string.h> struct node { int dist[20]; int from[20]; }rt[10]; int main() { int dmat[20][20]; int n=0, i=0, j=0, k=0,count=0; system("clear"); printf("Enter The Number Of Nodes\n"); scanf("%d",&n);
Dept. of CSE, NHCE Aug-Nov 2012 Page 4

Networks Laboratory (06CSL77)


printf("Enter The Cost Matrix\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { scanf("%d",&dmat[i][j]); dmat[i][i]=0; rt[i].dist[j]=dmat[i][j]; rt[i].from[j]=j; } do { count=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) for(k=1;k<=n;k++) if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j]) { rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j]; rt[i].from[j]=k; count++; } }while (count!=0); for(i=1;i<=n;i++) { printf("State Value From Router %d \n",i); for(j=0;j<n;j++) { printf("\t\t Via %d Is %d", rt[i].from[j],rt[i].dist[j]); } } } Compile and run $ cc o dv dv.c $ ./dv Enter The Number Of Nodes 3
Dept. of CSE, NHCE Aug-Nov 2012 Page 5

Networks Laboratory (06CSL77)


Enter The Cost Matrix 036 709 570 State Value From Router 1 Via 1 Is 0 Via 2 Is 3 Via 3 Is 6 State Value From Router 2 Via 1 Is 7 Via 2 Is 0 Via 3 Is 9 State Value From Router 3 Via 1 Is 5 Via 2 Is 7 Via 3 Is 0 Program 4. Using TCP/IP Sockets, write a client-server program to make client sending the file name and the server to send back the contents of the requested file if present. Header Filename: inet.h #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<fcntl.h> #include<string.h> #include<stdlib.h> #define SERV_TCP_PORT 6880 #define SERV_HOST_ADDR "127.0.0.1"

Dept. of CSE, NHCE

Aug-Nov 2012

Page 6

Networks Laboratory (06CSL77)

Program-Name: Client.c #include"inet.h" int main() { int sockfd; struct sockaddr_in serv_addr,cli_addr; char filename[100],buf[1000]; int n; serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=inet_addr(SERV_HOST_ADDR); serv_addr.sin_port=htons(SERV_TCP_PORT); if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) { printf("Client:cant open stream socket\n"); exit(0); } else printf("Client:stream socket opened successfully\n"); if(connect(sockfd,(struct sockaddr *)&serv_addr, sizeof(serv_addr))<0) { printf("Client:cant connect to server\n"); exit(0); } else printf("Client:connected to server successfully\n"); printf("\n Enter the file name to be displayed :"); scanf("%s",filename);
Dept. of CSE, NHCE Aug-Nov 2012 Page 7

Networks Laboratory (06CSL77)


write(sockfd,filename,strlen(filename)); printf("\n filename transferred to server\n"); n=read(sockfd,buf,1000); buf[n]=\0; printf("\n Client : Displaying file content of %s\n", filename); fputs(buf,stdout); close(sockfd); exit(0); } Program-Name: Server.c #include"inet.h" int main() { int sockfd,newsockfd,clilen; struct sockaddr_in cli_addr,serv_addr; char filename[25],buf[1000]; int n,m,fd,size; serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=htonl(INADDR_ANY); serv_addr.sin_port=htons(SERV_TCP_PORT);

if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) { printf("Server:cant open stream socket\n"); exit(0); } else printf("Server:stream socket opened successfully\n");

if((bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)))<0) { printf("Server:cant bind local address\n"); exit(0);


Dept. of CSE, NHCE Aug-Nov 2012 Page 8

Networks Laboratory (06CSL77)


} else printf("Server: bind to local address\n"); listen(sockfd,5); printf("\n SERVER : Waiting for client...\n"); clilen=sizeof(cli_addr); newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clilen); if(newsockfd<0) { printf("server:accept error\n"); exit(0); } else printf("Server: accepted\n"); n=read(newsockfd,filename,25); filename[n]='\0'; printf("\n SERVER : %s is found and ready to transfer \n",filename); fd=open(filename,O_RDONLY); if(fd==-1) { write(newsockfd,File doesnt exists,25); exit(0); } size=lseek(fd,0,2); lseek(fd,0,0); n=read(fd,buf,size); buf[n]='\0'; write(newsockfd,buf,n); printf("\n transfer success\n"); puts(buf); close(newsockfd); exit(0); }

Dept. of CSE, NHCE

Aug-Nov 2012

Page 9

Networks Laboratory (06CSL77)


Compile and run - open first terminal, compile and run server there - open second terminal, compile and run client there $ cc o ser Server.c $ ./ser Server: stream socket opened successfully Server: bind to local address SERVER : Waiting for client... Server: accepted SERVER : abc.txt is found and ready to transfer transfer success Computer science engineering $ cc o cli Client.c $ ./cli Client:stream socket opened successfully Client:connected to server successfully Enter the file name to be displayed : abc.txt filename transferred to server Client : Displaying file content of abc.txt Computer science engineering NOTE : If the program has to be executed on the network say on two different machines then Find out the IP address of the server machine(where server.c is typed) and on the client machine(where client.c is typed), change the IP address in the header file(inet.h) from 127.0.0.1 to the IP address of the server

Dept. of CSE, NHCE

Aug-Nov 2012

Page 10

Networks Laboratory (06CSL77)


Program5. Using Fifos, write a client-server program to make client sending the file name and the server to send back the contents of the requested file if present. Program-Name: fclient.c #include<stdio.h> #include<fcntl.h> #include<string.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> int main() { char filename[100],buf[300]; int n,fd,fd2; mknod("fifo1",S_IFIFO | 0666,0); mknod("fifo2",S_IFIFO | 0666,0); fd=open("fifo1",O_WRONLY); printf("CLient Online! \n CLIENT : enter the filename...\n\n"); scanf("%s",filename); write(fd,filename,strlen(filename)); printf("\n waiting for reply...\n"); fd2=open("fifo2",O_RDONLY); n=read(fd2,&buf,300); buf[n]='\0'; printf("\n File received ..the contents are...\n"); puts(buf); unlink("fifo1"); unlink("fifo2"); }

Dept. of CSE, NHCE

Aug-Nov 2012

Page 11

Networks Laboratory (06CSL77)


Program-Name: fserver.c #include<stdio.h> #include<fcntl.h> #include<stdlib.h> #include<string.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main() { char filename[100], buf1[300]; int m,n,filesize,fl,fd,fd2; mknod("fifo1",S_IFIFO | 0666,0); mknod("fifo2",S_IFIFO | 0666,0); printf("\n Server Online\n"); fd=open("fifo1",O_RDONLY); n = read(fd,&filename,100); filename[n]='\0'; fl=open(filename,O_RDONLY); if(f1==-1) { fd2=open("fifo2",O_WRONLY); write(fd2,File doesnt exists,25); exit(0); } printf("\n Sever: %s is found!\n transferring the contents \n",filename); filesize=lseek(fl,0,2); lseek(fl,0,0); n=read(fl,buf1,filesize); buf1[n]='\0'; fd2=open("fifo2",O_WRONLY); write(fd2,buf1,n); printf("\n SERVER :Transfer completed\n"); puts(buf1); unlink("fifo1"); unlink("fifo2"); }
Dept. of CSE, NHCE Aug-Nov 2012 Page 12

Networks Laboratory (06CSL77)

Compile and run - open first terminal, compile and run server there - open second terminal, compile and run client there $ cc o ser fserver.c $ ./ser Server Online Sever: abc.txt is found! transferring the contents SERVER :Transfer completed Computer Science Engineering $ cc o cli fclient.c $ ./cli Client Online! CLIENT : enter the filename... abc.txt File received ..the contents are... Computer Science Engineering

Program6. Write a program for simple RSA algorithm to encrypt and decrypt the data. #include<stdio.h> int phi, M, n, e, d, C; void encrypt() { int i; C = 1; for(i=0;i< e;i++) C=C*M%n; C = C%n; printf("\n\tEncrypted keyword : %d",C); }

Dept. of CSE, NHCE

Aug-Nov 2012

Page 13

Networks Laboratory (06CSL77)


void decrypt() { int i; M = 1; for(i=0;i< d;i++) M=M*C%n; M = M%n; printf("\n\tDecrypted keyword : %d",M); } int main() { int p,q,rem; printf("Enter Two Distinct Prime Numbers\t: "); scanf("%d%d",&p,&q); n = p*q; phi=(p-1)*(q-1); printf("\n\tPhi(n)\t= %d",phi); printf("\n\nEnter e coprime with Phi(1<e<%d)\t: ",phi); scanf("%d",&e); d = 1; do { rem = (d*e)%phi; d++; }while(rem!=1); d = d-1; printf("\n\tPublic Key\t:{%d,%d}",e,n); printf("\n\tPrivate Key\t:{%d,%d}",d,n); printf("\n\nEnter The Plain Text(0<PT<%d)\t: ",n); scanf("%d",&M); encrypt(); printf("\n\nEnter the Cipher text\t: "); scanf("%d",&C); decrypt(); }
Dept. of CSE, NHCE Aug-Nov 2012 Page 14

Networks Laboratory (06CSL77)


Compile and run $ cc o rsa rsa.c $ ./rsa Enter Two Distinct Prime Numbers 5 11 Phi = 40 Enter e coprime with Phi(1<e<40) 7 Public Key : {7,55} Private Key : {23, 55} Enter The Plain Text(0<PT<55) 42 Encrypted keyword : 48 Enter the Cipher text 48 Decrypted keyword : 42

Program 7. Write a program for Congestion control using the leaky bucket algorithm

#include<stdio.h> #include<string.h> int min(int x,int y) { if(x<y) return x; else return y; } int main() { int drop=0,mini,nsec,cap,count=0,i,inp[25],process; system("clear"); printf("Enter The Bucket Size\n");
Dept. of CSE, NHCE Aug-Nov 2012 Page 15

Networks Laboratory (06CSL77)


scanf("%d",&cap); printf("Enter The Processing Rate\n"); scanf("%d",&process); printf("Enter The No. Of Seconds You Want To Stimulate\n"); scanf("%d",&nsec); for(i=0;i<nsec;i++) { printf("Enter The Size Of The Packet Entering At %d sec\n",i+1); scanf("%d",&inp[i]); } printf("\nSecond|Packet Recieved|Packet Sent|Packet Left|Packet Dropped|\n"); printf("--------------------------------------------------------------\n"); for(i=0;i<nsec;i++) { count+=inp[i]; if(count>cap) { drop=count-cap; count=cap; } printf("%d",i+1); printf("\t%d",inp[i]); mini=min(count,process); printf("\t\t%d",mini); count=count-mini; printf("\t\t%d",count); printf("\t\t%d\n",drop); drop=0; }

for(;count!=0;i++) { if(count>cap) { drop=count-cap;


Dept. of CSE, NHCE Aug-Nov 2012 Page 16

Networks Laboratory (06CSL77)


count=cap; } printf("%d",i+1); printf("\t0"); mini=min(count,process); printf("\t\t%d",mini); count=count-mini; printf("\t\t%d",count); printf("\t\t%d\n",drop); } } Compile and run $ cc o leaky leaky.c $ ./leaky Enter The Bucket Size 5 Enter The Processing Rate 2 Enter The No. Of Seconds You Want To Stimulate 3 Enter The Size Of The Packet Entering At 1 sec 5 Enter The Size Of The Packet Entering At 2 sec 4 Enter The Size Of The Packet Entering At 3 sec 3 Second|Packet Recieved|Packet Sent|Packet Left|Packet Dropped| ---------------------------------------------------------------------------------------1 5 2 3 0 2 4 2 3 2 3 3 2 3 1 4 0 2 1 0 5 0 1 0 0

Dept. of CSE, NHCE

Aug-Nov 2012

Page 17

Networks Laboratory (06CSL77)


Program 8. Write a program for hamming code generation for error detection and correction #include<stdio.h> #include<stdlib.h> char data[5]; int encoded[8],edata[7],syndrome[3]; int hmatrix[3][7] = { 1,0,0,0,1,1,1, 0,1,0,1,0,1,1, 0,0,1,1,1,0,1 }; char gmatrix[4][8]={"0111000","1010100","1100010","1110001"}; int main() { int i,j; system("clear"); printf("\nHamming code----- Encoding\n"); printf("Enter 4 bit data : "); scanf("%s",data); printf("\nGenerator matrix\n"); for(i=0;i<4;i++) printf("%s\n",gmatrix[i]); printf("\nEncoded data "); for(i=0;i<7;i++) { for(j=0;j<4;j++) encoded[i]+=((data[j]-'0')*(gmatrix[j][i]-'0')); encoded[i]=encoded[i]%2; printf("%d ",encoded[i]); } printf("\nHamming code----- Decoding\n"); printf("Enter encoded bits as recieved : "); for(i=0;i<7;i++) scanf("%d",&edata[i]); for(i=0;i<3;i++) { for(j=0;j<7;j++)
Dept. of CSE, NHCE Aug-Nov 2012 Page 18

Networks Laboratory (06CSL77)


syndrome[i]+=(edata[j]*hmatrix[i][j]); syndrome[i]=syndrome[i]%2; } for(j=0;j<7;j++) if((syndrome[0]==hmatrix[0][j]) && (syndrome[1]==hmatrix[1][j])&& (syndrome[2]==hmatrix[2][j])) break; if(j==7) printf("\nError free\n"); else { printf("\nError recieved at bit number %d of data\n",j+1); edata[j]=!edata[j]; printf("\nCorrect data should be : "); for(i=0;i<7;i++) printf("%d",edata[i]); } return 0; } Compile and run $ cc o ham ham.c $ ./ham Hamming code----- Encoding Enter 4 bit data : 1010 Generator matrix 0111000 1010100 1100010 1110001 Encoded data 1011010 Hamming code----- Decoding Enter encoded bits as received 1011010 Error free $ ./ham
Dept. of CSE, NHCE Aug-Nov 2012 Page 19

Networks Laboratory (06CSL77)


Hamming code----- Encoding Enter 4 bit data : 1010 Generator matrix 0111000 1010100 1100010 1110001 Encoded data 1011010 Hamming code----- Decoding Enter encoded bits as received 1011011 Error recieved at bit number 7 of data Correct data should be : 1011010 ..

Dept. of CSE, NHCE

Aug-Nov 2012

Page 20

You might also like