Sunteți pe pagina 1din 33

NETWORKS LABORATORY

(COMMON TO CSE & ISE)

Sub Code : CSL77 Note : Student is required to solve one problem from PART-A and one problem from PART-B. Both the parts have equal weightage. PART A Simulation Exercises The following experiments shall be conducted using either NS228/OPNET or any other simulators. 1. 2. 3. 4. 5. 6. 7. 8. Simulate a three nodes point-to-point network with duplex links between them. Set the queue size vary the bandwidth and find the number of packets dropped. Simulate a four node point-to-point network, and connect the links as follows: n0-n2, n1-n2 and n2-n3. Apply TCP agent between n0-n3 and UDP n1-n3. Apply relevant applications over TCP and UDP agents changing the parameter and determine the number of packets by TCP/UDP. Simulate the different types of Internet traffic such as FTP a TELNET over a network and analyze the throughput. Simulate the transmission of ping messaged over a network topology consisting of 6 nodes and find the number of packets dropped due to congestion. Simulate an Ethernet LAN using N-nodes(6-10), change error rate and data rate and compare the throughput. Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and determine collision across different nodes. Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and plot congestion window for different source/destination. Simulate simple ESS and with transmitting nodes in wire-less LAN by simulation and determine the performance with respect to transmission of packets. PART B The following experiments shall be conducted using C/C++. 1. 2. 3. 4. 5. 6. 7. 8. Write a program for error detecting code using CRC-CCITT (16-bits). Write a program for frame sorting technique used in buffers. Write a program for distance vector algorithm to find suitable path for transmission. 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. Implement the above program using as message queues or FIFOs as IPC channels. Write a program for simple RSA algorithm to encrypt and decrypt the data. Write a program for Hamming Code generation for error detection and correction. Write a program for congestion control using Leaky bucket algorithm.

PART B 1.Write a program for error detecting code using CRC-CCITT (16-bits). #include<stdio.h> #include<string.h> #define N strlen(g) char t[128], cs[128], g[]="1001"; int a, e, c; void xor() { for(c=1;c<N;c++) cs[c]=((cs[c]==g[c])?'0':'1'); } void crc() { for(e=0;e<N;e++) cs[e]=t[e]; do { if(cs[0]=='1') xor(); for(c=0;c<N-1;c++) cs[c]=cs[c+1]; cs[c]=t[e++]; }while(e<=a+N-1); } int main() { printf("\nEnter poly : "); scanf("%s",t); printf("\nGenerating Polynomial is : %s",g); a=strlen(t); for(e=a;e<a+N-1;e++) t[e]='0'; printf("\nModified t[u] is : %s",t); crc(); printf("\nChecksum is : %s",cs); for(e=a;e<a+N-1;e++) t[e]=cs[e-a]; printf("\nFinal Codeword is : %s",t); printf("\nTest Error detection 0(yes) 1(no) ? : "); scanf("%d",&e); if(e==0) { printf("Enter position where error is to inserted : "); scanf("%d",&e); t[e]=(t[e]=='0')?'1':'0'; printf("Errorneous data : %s\n",t); } crc(); for (e=0;(e<N-1)&&(cs[e]!='1');e++); if(e<N-1) printf("Error detected."); else printf("No Error Detected."); }

OUTPUT: /*Enter poly : 1001110 * * Generating Polynomial is : 1001 * Modified t[u] is : 1001110000 * Checksum is : 110 * Final Codeword is : 1001110110 * Test Error detection 0(yes) 1(no) ? : 0 * Enter position where error is to inserted : 1 * Errorneous data : 1101110110 Error detected.*/

2.Write a program for frame sorting technique used in buffers. #include<stdio.h> #include<string.h> struct frame { int seq; int len; int flag; char data[10]; }n[20],m[20],temp; char str[100]; int count; void frames() { int i,j,s,size,total=0; s=strlen(str); while(total<s) { size=rand()%10+1; n[count].seq=count+1; n[count].len=size; n[count].flag=0; if((total+size)<s) { for(i=total,j=0;j<size;i++,j++) n[count].data[j]=str[i]; total+=size; } else { n[count].len=s-total; for(j=0;j<n[count].len;j++) n[count].data[j]=str[total++]; } count+=1; } printf("show the packets\n"); for(i=0;i<count;i++) { printf("%d:%d\t",n[i].seq,n[i].len); for(j=0;j<n[i].len;j++) printf("%c",n[i].data[j]); printf("\n"); } }

void trans() { int i,j; int c=0; while(c<count) { i=rand()%count; if(n[i].flag==0) { m[c++]=n[i]; n[i].flag=1; } } printf("\n show the random packets\n"); for(i=0;i<count;i++) { printf("%d:%d\t",m[i].seq,m[i].len); for(j=0;j<m[i].len;j++) printf("%c",m[i].data[j]); printf("\n"); } } void sort() { int i,j; for(i=0;i<count;i++) for(j=i+1;j<count;j++) if(m[i].seq>m[j].seq) { temp=m[i]; m[i]=m[j]; m[j]=temp; } printf("show the sequence packets\n"); for(i=0;i<count;i++) { printf("%d:%d\t",m[i].seq,m[i].len); for(j=0;j<m[i].len;j++) printf("%c",m[i].data[j]); printf("\n"); } } int main() { printf("enter the data\n"); scanf("%[^\n]",str);

frames(); trans(); sort(); return 0; }

OUTPUT: /*enter the data * hai hello * show the packets * 1:4 hai * 2:5 hello * * show the random packets * 2:5 hello * 1:4 hai * show the sequence packets * 1:4 hai * 2:5 hello * [jacintha@localhost cnnew]$ ./a.out * enter the data * hai hello how are u * show the packets * 1:4 hai * 2:7 hello h * 3:8 ow are u *

* show the random packets * 2:7 hello h * 3:8 ow are u * 1:4 hai * show the sequence packets * 1:4 hai * 2:7 hello h * 3:8 ow are u * */

3.Write a program for distance vector algorithm to find suitable path for transmission. #include<stdio.h> #include<stdlib.h> int main() { int d[10][10],i,j,n; int k,min=999; int a[10][10]; char x='A',b='A',c='A'; printf("enter the no of nodes\n"); scanf("%d",&n); printf("enter the matrix\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) scanf("%d",&d[i][j]); } for(i=0;i<n;i++) { for(j=0;j<n;j++) { min=d[i][j]; a[i][j]=j; for(k=0;k<n;k++) { if(k==i) continue; else if(min>(d[i][k]+d[k][j])) { min=d[i][k]+d[k][j]; if(i==j) a[i][k]=i; else a[i][j]=k; } } d[i][j]=min; } } printf("the............\n"); for(i=0;i<n;i++) { b='A';

printf("routing table of %d:\n\n",i); for(j=0;j<n;j++) { c='A'; printf("%c->%c=%d\t",x,b,d[i][j]); b++; c=c+a[i][j]; printf("node=%c\n",c); } x++; printf("\n\n"); } return 0; } OUTPUT: /* enter the no of nodes of nodes 4 enter the matrix 0413 4 0 999 3 1 999 0 1 33 10 the............ routing table of 0:

A->A=0 A->B=4 A->C=1 A->D=2

node=A node=B node=C node=C

routing table of 1:

B->A=4 B->B=0 B->C=4 B->D=3

node=A node=B node=D node=D

routing table of 2:

C->A=1 C->B=4 C->C=0 C->D=1

node=A node=D node=C node=D

routing table of 3:

D->A=2 D->B=3 D->C=1 D->D=0 */

node=C node=B node=C node=D

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. CLIENT.C #include<stdio.h>

#include<string.h> #include<stdlib.h> //include<sys/socket.h> //include<netinet/in.h> #include<sys/fcntl.h> #include<netdb.h> #define SERVER_PORT 2234 #define BUF_SIZE 4096 //for sockaddr //for sockaddr_in //for O_RDONLY

int main() { int c,s,bytes; char buf[BUF_SIZE],fname[255]; struct hostent *h; struct sockaddr_in channel;

printf("Enter the file name:"); gets(fname);

h=gethostbyname("localhost"); if(!h) printf("gethostbyname failed"), exit(0);

//get server address

memset(&channel,0,sizeof(channel)); channel.sin_family=AF_INET;

//allocate memory for 'channel' //assign values

memcpy(&channel.sin_addr.s_addr,h->h_addr,h->h_length); channel.sin_port=htons(SERVER_PORT);

s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(s<0) printf("socket creation failed"), exit(0);

c=connect(s,(struct sockaddr*) &channel, sizeof(channel)); if(c<0) printf("connect failed"), exit(0);

write(s, fname, strlen(fname));

//write the file name to channel

while(1) { bytes=read(s,buf,BUF_SIZE); if (bytes<=0) exit(0); write(1,buf,bytes); } } //read file contents form the channel //sent by server

SERVER.C #include<stdio.h> #include<string.h> #include<stdlib.h> //include<sys/socket.h> //include<netinet/in.h> #include<sys/fcntl.h> #include<netdb.h> #define SERVER_PORT 2234 #define BUF_SIZE 4096 int main() { int s,b,l,fd,sa,bytes,on=1; char buf[BUF_SIZE],fname[255]; struct sockaddr_in channel; //for sockaddr //for sockaddr_in //for O_RDONLY

s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(s<0) printf("socket failed"), exit(0);

setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char*)&on,sizeof(on));

memset(&channel,0,sizeof(channel)); channel.sin_family=AF_INET;

//allocate memory for 'channel' //assign values

channel.sin_addr.s_addr=htonl(INADDR_ANY); channel.sin_port=htons(SERVER_PORT);

b=bind(s,(struct sockaddr*)&channel,sizeof(channel)); if(b<0) printf("bind failed"), exit(0);

listen(s,5);

//listen channel for any request

while(1) { printf("\n\nWaiting for request:\n"); sa=accept(s,0,0); //create a socket for communication

if(sa<0) printf("accept failed");

memset(fname,0,sizeof(fname)); read(sa,fname,BUF_SIZE); //read file name from channel

printf("requested filename: %s",fname); fd=open(fname,O_RDONLY); if(fd<0) { printf("\nError message sent to client\n"); write(sa,"could not open requested file",40); } else { while(1) { bytes = read(fd,buf,BUF_SIZE); if(bytes<=0) break; write(sa,buf,bytes); } //write the contents to channel //read file & store in buf //open file to READ

close(fd); } close(sa); } } OUTPUT Server side: Waiting for request: requested filename: span.c client side: Enter the file name:span.c #include <stdio.h> //#include <conio.h> #define MAX 10

//close connection

prims(int cost[MAX][MAX], int n, int source) { int d[MAX] .......................................................... ............................................

5.Implement the above program using as message queues or FIFOs as IPC channels. Client.c #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<errno.h> extern int errno; int main() { int readfd,writefd;

//now we open the 2 fifo's if((writefd = open("fifoa",1))<0) printf("cannot open fifo1 for write");

//open fifp for read if((readfd = open("fifob",0))<0) printf("cannot open fifo for write");

client(readfd,writefd);

//we now delete the 2 fifo's if(unlink("fifoa")<0) printf("client cannot delete fifo1");

if(unlink("fifob")<0) printf("client cannot delete fifo2");

return 1;

} client(int readfd,int writefd) { int n,choice; char buff[1024],msg[1024];

while(1) { while(choice != 4) { printf("\n"); printf("1.DATE\n2.TIME\n3.ECHO MESSAGE\n4.QUIT\n"); printf("Enter your choice\n"); scanf("%d",&choice); switch(choice) { case 1: write(writefd,"date",4); while((n = read(readfd,buff,1024)) > 0) {

write(1,buff,n); if(n < 1024) break; } break;

case 2: write(writefd,"time",4); while((n = read(readfd,buff,1024)) > 0) { write(1,buff,n); if(n < 1024) break; } break;

case 3: printf("\nMessage:"); scanf("%s",msg); n = strlen(msg);

write(writefd,msg,n); while((n = read(readfd,buff,1024))>0) { write(1,buff,n); if(n < 1024) break; } break;

case 4:return; } } } }

server.c #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<errno.h>

#include<unistd.h> #include<fcntl.h>

extern int errno; int main() { int readfd,writefd; printf("server is runnig\n"); //first we create 2 fifo's if((mkfifo("fifoa",S_IFIFO|0666)<0) && (errno!=EEXIST)) { printf("cannot create fifoa"); } if((mkfifo("fifob",S_IFIFO|0666)<0) && (errno!=EEXIST)) { printf("cannot create fifob"); } //now we open the 2 fifo's if((readfd = open("fifoa",0))<0) printf("cannot open fifoa");

if((writefd = open("fifob",1))<0) printf("cannot open fifob");

server(readfd,writefd);

return 1; }

int server(int readfd,int writefd) { int n, fd1, fd2; char buff[1024],msg[20], res[1024];

while(1) { n = read(readfd,buff,1024); buff[n] = '\0';

if(!strcmp(buff,"date")) { system("date +'%d %m %y' >fdate"); fd1 = open("fdate",O_RDONLY); while((n = read(fd1,res,1024)) > 0) { write(writefd,res,n); if(n < 1024)break; }

else if(!strcmp(buff,"time")) { system("date +'%H %M %S' >ftime"); fd2 = open("ftime",O_RDONLY); while((n = read(fd2,res,1024)) > 0) { write(writefd,res,n); if(n < 1024)break; } }

else { write(writefd,buff,n); } }

return 1; }

6.Write a program for simple RSA algorithm to encrypt and decrypt the data. #include<stdio.h> typedef unsigned int uint; uint gcd(uint x,uint y) { return y==0? x:gcd(y,x%y); }

uint multi(uint txt, uint ed, uint n) { uint i,rem=1;

for(i=1; i<=ed; i++) rem=(rem*txt)%n; return rem; }

short prime(uint no) { uint i; for(i=2; i<=no/2; i++) if(no%i==0) return 1; return 0; }

int main() { char msg[100]; uint pt[100],ct[100],n,d,e,p,q,z,i,len;

do{ printf("\nEnter 2 large prime numbers p & q:\n"); scanf("%d %d",&p,&q); }while(prime(p) || prime(q)); n=p*q; z=(p-1)*(q-1);

do { printf("\nEnter prime value of e relative to %d(z):",z); scanf("%d",&e); }while(gcd(e,z)!=1 || e>n);

for(d=2;d<z;d++) if((e*d)%z == 1) break;

printf("Enter the Message\n"); len=read(1,msg,100)-1; printf("ASCII value of the given msg is:"); for(i=0;i<len;i++) { pt[i]=msg[i];

//get message from keybrd.

printf("%d\t", pt[i]);}

printf("\n Cipher Text="); for(i=0;i<len;i++) //convert plain to cipher text

printf("%d\t",ct[i]=multi(pt[i],e,n));

printf("\n Plain Text=");printf("%s",msg); for(i=0;i<len;i++) printf("%c",multi(ct[i],d,n)); //convert cipher to plain text

} OUTPUT: /*Enter 2 large prime numbers p & q: 13 19

Enter prime value of e relative to 216(z):5 Enter the Message hai ASCII value of the given msg is:104 Cipher Text=130 Plain Text=hai*/ 184 79 97 105

7.Write a program for Hamming Code generation for error detection and correction. #include<stdio.h> #include<stdlib.h> short calcevenparity(short num) {short bitmask=1,countones=0,i; for(i=0;i<8;i++) { if(num & bitmask) countones++; bitmask<<=1; } return countones%2?1:0; } int main() { short data,code,errpos=0,e,f=1,r1,r2,r3; printf("SENDER\n"); printf("Enter the integer b/n 0 and 15:"); scanf("%x",&data); r1=calcevenparity(0xb & data); r2=calcevenparity(0xd & data); r3=calcevenparity(0xe & data); code=data<<3; if(data%2) code|=0x4; code &=0x74; if(r1) code|=0x01;

if(r2) code|=0x02; if(r3) code|=0x08; printf("The hamming code is :%x(hex)\n",code); printf("\n Enter the position to introduce error (1-7)"); scanf("%d",&e); if(e>=1 && e<=7) f<<=(e-1); code^=f; printf("The erroneous code is %x(hex)\n",code); printf("RECIVER\n"); if(calcevenparity(code & 0x55)) errpos++; if(calcevenparity(code & 0x66)) errpos+=2; if(calcevenparity(code & 0x78)) errpos+=4; if(errpos) printf("Error detected at position %d\n",errpos); else printf("Error free code \n\n"); f=1; f<<=errpos-1; printf("Corrected code is :%x(hex)",f^code); } OUTPUT: /*SENDER * Enter the integer b/n 0 and 15:7 * The hamming code is :34(hex) * * Enter the position to introduce error (1-7)5

* The erroneous code is 24(hex) * RECIVER * Error detected at position 5 * Corrected code is :34(hex) SENDER Enter the integer b/n 0 and 15:9 The hamming code is :4c(hex)

Enter the position to introduce error (1-7)5 The erroneous code is 5c(hex) RECIVER Error detected at position 5 Corrected code is :4c(hex) */

another method
#include<stdlib.h> #include<stdio.h> #include<string.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"};

void main() { int i,j; 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("\n"); printf("Encoded 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 received: ");

for(i=0;i<7;i++) scanf("%d",&edata[i]); for(i=0;i<3;i++) { for(j=0;j<7;j++) syndrome[i]=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("Data is error free!!!\n"); else { printf("Error received at bit number %d of the data\n",j+1); edata[j]=!edata[j]; printf("The correct data should be : "); for(i=0;i<7;i++) printf(" %d ",edata[i]); } } /*output Hamming Code --- Encoding Enter 4 bit data: 1010

Generator Matrix: 0111000 1010100 1100010 1110001

Encoded Data: 1 0 1 1 0 1 0 Hamming Code --- Decoding Enter Encoded bits as received: 1 0 1 0 0 1 0 Error received at bit number 4 of the data The correct data should be : 1 0 1 1 0 1 0 */

8.Write a program for congestion control using Leaky bucket algorithm. #include<stdio.h> #include<stdlib.h> int op,i,packets[5],bucketsize; void bktinput(int a,int b) { if(a>bucketsize) printf("\n\t\t bucket overflow\n"); else { while(a>b)

{ printf("\n\t\t %d bytes outputed",b); a-=b; } if(a>0) printf("\n\t\t last %d bytes sent",a); printf("\n\t\t leaky bucket successful\n"); } } int main() { printf("enter the bucket size:"); scanf("%d",&bucketsize); printf("enter the output rate:"); scanf("%d",&op); printf("enter 5 packets in streams:"); for(i=0;i<5;i++) scanf("%d",&packets[i]); for(i=0;i<5;i++) { printf("\n packet no=%d\t packet size=%d",i,packets[i]); bktinput(packets[i],op); } }

OUTPUT: /*enter the bucket size:100 * enter the output rate:60 * enter 5 packets in streams:50 60 79 98 105 * packet no=0 * * * packet no=1 * * *packet no=2 * * * *packet no=3 * * * *packet no=4 * */ packet size=50 last 50 bytes sent leaky bucket successful packet size=60 last 60 bytes sent leaky bucket successful packet size=79 60 bytes outputed last 19 bytes sent leaky bucket successful packet size=98 60 bytes outputed last 38 bytes sent leaky bucket successful packet size=105 bucket overflow

S-ar putea să vă placă și