You are on page 1of 41

Computer Networks Lab Record

PART A
1) Simulate a three-node point-to-point network with a duplex link between them. Set the queue size and vary the bandwidth and find the number of packets dropped. STEPS:1. Draw the network topology as specified in the Draw Topology Mode of the simulator. Draw Topology Mode :a) Select the hub icon on the toolbar and drag it on the working window. b) Select the host icon on the toolbar and drag it onto the working window. Repeat this for another host icon. c) Select the link icon on the toolbar and drag it onto the screen from host1 to the hub and again from host2 to the hub. Here hub act as node 3 in the point to point network. This leads to the creation of the 3-node point-to-point network topology. 2. Go to Edit mode and save the topology as a .tpl file. 3. Double click on host1; a host dialog box will open up. Click on node editor and you can see the different layers. Select MAC layer and then select half duplex for hubs and in log statistics, select Number of Drop Packets, Number of Collisions, and Throughput of outgoing packets and press OK. Select FIFO, set the queue size to 50, and press OK. Then click on Add. Another dialog box pops up. Click on command box and type the following command stg u 1024 100 1.0.1.1 and click OK. 4. Double click on host2 and follow the same step as above with selecting Throughput of incoming packets in log statistics. The command used here is rtg u 5. Double click on the link between host1 and hub to set the bandwidth to some initial value, say 10 mbps. 6. Save the changes made to the topology. Now click on the R button to run the simulation. 7. Now go to Menu->Simulation->Run. Executing this command will submit the current simulation job to one available simulation server managed by the dispatcher. When the simulation server is executing, the user will see the time knot reflects the current virtual time of the simulation case.

Dept. of CSE, BTLIT Page 1

Computer Networks Lab Record

8. To start the playback, the user can left click the start icon (|>) of the time bar located at the bottom or P icon. The animation player will start the recorded packet animation. 9. You can plot the graph of performance curve by selecting Menu->Tools->Plot graph option. 10. To view the results, go to filename. Results folder. 11. Change the bandwidth say 9mbps and run the simulation and Compare the two results. The screenshot below explain the topology:-

Dept. of CSE, BTLIT Page 2

Computer Networks Lab Record

RESULTS :By using HUB: Commands used: Sender:stg -u 1024 100 1.0.1.2 Receiver:rtg -u Parameters:Drop Packets and Collision Packets. By setting the bandwidth as 10 Mbps on both the links and queue size as 50 we obtain the following results: Output throughput n1-p1= 1177 Input throughput n3-p1 = 1177 Collision and drop = 0 By changing bandwidth to 9Mbps in the destination link, we obtain the following results: Output throughput n1-p1 =1177 Input throughput n3-p1 = ~ 0 Collision and drop = 1100 By using SWITCH Commands used: Sender:stcp p 2000 l 1024 1.0.1.2 Receiver:rtcp p 2000 l 1024 Parameters:Drop Packets and Collision Packets. By setting the bandwidth as 10 Mbps on both the links and queue size as 50 we obtain the following results: output throughput n1-p1= 1190 input throughput n3-p1 = 1190 collision and drop = 0 By changing bandwidth to 9Mbps in the destination link, we obtain the following results: output throughput n1-p1 =1190 input throughput n3-p1 = varying collision and drop = ~0

Dept. of CSE, BTLIT Page 3

Computer Networks Lab Record

2) Simulate a Four node point-to-point network and connect the links as follows: n0n2,n1-n2 and n2-n3.Apply TCP agent between n0-n3 and UDP agent between n1n3.Apply relevant applications over TCP and UDP agents changing the parameters and determine the number of packets send by TCP/UDP. STEPS:1. Create the topology as specified in the question, in the draw mode of the simulator. 2. Go to edit mode and save the topology. 3. Setup TCP connection between node 1 and node 3 using the following command. stcp -p portno. -l buffersize ipaddress i.e stcp -p 3000 -l 1024 1.0.1.3 rtcp -p portno. -l buffersize i.e rtcp -p 3000 -l 1024 4. Setup UDP connection between node 2 and node 3 using the following command. stg -u payload_size duration ipaddress i.e stg -u 1024 100 1.0.1.3 rtg -u 5. Set the output throughput log to determine the number of packets sent by TCP/UDP. 6. To view the results,goto filename.results folder. The screenshot of the topology is shown below:

Dept. of CSE, BTLIT Page 4

Computer Networks Lab Record

RESULTS:Commands used: Sender:stcp p 3000 l 1024 1.0.1.3 stg u 1024 100 1.0.1.3 Receiver:rtcp p 3000 l 1024 rtg u Parameters:Throughput of incoming and outgoing Packets By setting the bandwidth as 100 Mbps on the TCP link and queue size as 50 we obtain the following results: Average no: of TCP packets sent = varying (348 to 1100) Average no: of UDP packets sent = 1180

Dept. of CSE, BTLIT Page 5

Computer Networks Lab Record

3) Simulate the different types of Internet Traffic such as FTP, TELNET over a network and analyze the throughput.*/ STEPS:To setup FTP connection. 1) Create the topology of three nodes; connect these nodes to a hub. 2) Go to edit mode and save the topology. 3) Setup FTP connection between node 1 and node 3 using the following commands. i.e stcp p 21 l 1024 1.0.13 rtcp p 21 l 1024 4) Set the input and output throughput log file as described in the previous experiments. 5) To view the results, go to filename.results folder. To setup TELNET connection. 1) Create the topology of three nodes; connect these nodes to a hub. 2) Go to edit mode and save the topology. 3) Setup TELNET connection between node 2 and node 3 using the following commands. stcp p 23 l 1024 1.0.1.3 rtcp p 23 l 1024 4) Set the input and output throughput log file as described in the previous experiments. 5) To view the results, go to filename.results folder. The screenshot of the topology is shown below:

Dept. of CSE, BTLIT Page 6

Computer Networks Lab Record

RESULTS:For FTP: Command used:Sender:stcp p 21 l 1024 1.0.1.3 Receiver:rtcp p 21 l 1024 Parameters:Throughput of incoming and outgoing Packets Output Throughput n1- p1= 680-1097 Input Throughput n2-p1 = 680-1097 For TELNET: Command used:Sender:stcp p 23 l 1024 1.0.1.3 Receiver:rtcp p 23 l 1024 Parameters:Throughput of incoming and outgoing Packets Output Throughput n1-p1= 495-1095 Input Throughput n2-p1= 495-1095

Dept. of CSE, BTLIT Page 7

Computer Networks Lab Record

4) Simulate the transmission of ping messages over a network topology consisting of 6 nodes and find the number of packets dropped due to congestion. STEPS: 1. Click on the subnet icon on the toolbar and then click on the screen of the working window. 2. Select the 6 nodes and a suitable radius between the host and the hub. 3. A single host is made to act as receiver and others as sender 4. Run the simulation. 5. In the run mode,from any sending node the reciving node is pinged say ping 1.0.1.6. The screenshot of the topology is shown below:-

Dept. of CSE, BTLIT Page 8

Computer Networks Lab Record

RESULTS:Sender:stg -u 1024 100 1.0.1.1 Receiver:rtg -u 1024 Command Console:Goto tools-> simulation time and change Simulation time to 100. During run mode, double click host 2 and then click command console. And execute the following command. ping 1.0.1.4 Parameters:Drop Packets and Collision Packets.

Dept. of CSE, BTLIT Page 9

Computer Networks Lab Record

5) Simulate an Ethernet LAN using N nodes (6-10) Change error rate and data rate and compare throughput. STEPS: 1. Connect one set of hosts with a hub and another set of hosts also through a hub and connect these two hubs with a switch. This forms an Ethernet LAN. 2. Setup a TCP connection between a host on one hub and host on another hub using following command. i.e. stcp p 2000 l 1024 1.0.1.4 rtcp p 2000 l 1024 3. Setup the error rate, data rate in the physical layer, I/P and O/P throughput in the MAC layer 4. Change error rate and data rate and compare throughputs. 5. View the results in filename results. The screenshot of the topology is shown below:

Dept. of CSE, BTLIT Page 10

Computer Networks Lab Record

RESULTS:Commands: Sender:stcp p 2000 l 1024 1.0.1.4 Receiver:rtcp p 2000 l 1024 Parameters:Throughput of outgoing Packets For first 6 nodes: Initial error rate: 0.0 Initial data rate: 10 Mbps Output Throughput: 654-1091 Input Throughput: 654-1091 Changed error rate: 1.0 Changed data rate: 10 Mbps Output Throughput: 654-1091 Input Throughput: 654-1091 Error rate: 1.0 Data rate: 100 Mbps Output Throughput: 1547-9765 Input Throughput: 1547-9765 For 6-10 nodes: Initial error rate: 0 Initial data rate: 10 Mbps Output Throughput: 654-1091 Input Throughput: 654-1091

Dept. of CSE, BTLIT Page 11

Computer Networks Lab Record

6) Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and determine collisions across different nodes.*/ STEPS: 1. Connect one set of hosts with a hub and another set of hosts also through a hub and connect these two hubs through a switch. This forms Ethernet LAN. 2. Setup multiple traffic connections between the hosts on one hub and hosts on another hub using the following command stcp p 2000 l 1024 1.0.1.4 rtcp p 2000 l 1024 3. Setup the collision log at the destination host in the MAC layer. 4. Give the results in filename results. The screenshot of the topology is shown below:

Dept. of CSE, BTLIT Page 12

Computer Networks Lab Record

RESULTS:Commands: Sender:stcp p 2000 l 1024 1.0.1.4 Receiver:rtcp p 2000 l 1024 Parameters:Collision Packets and Drop Packets (Optional) Drops at node 5 : 324-750 Drops at node 4 : 274-930

Dept. of CSE, BTLIT Page 13

Computer Networks Lab Record

7) Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and plot congestion window for different source and destination. STEPS:1. Connect one set of hosts with a hub and another set of hosts also through a hub and connect these two hubs through a switch. This forms Ethernet LAN. 2. Setup multiple traffic connections between the hosts on one hub and hosts on another hub using the following command stcp p 2000 l 1024 1.0.1.4 rtcp p 2000 l 1024 3. Setup the collision log at the destination host in the MAC layer. 4. To plot the congestion window, go to Menu->Tools-> Plotgraph->File->Open filename.results->filename.coll.log 5. Give the results in filename. results The screenshot of the topology is shown below:

Dept. of CSE, BTLIT Page 14

Computer Networks Lab Record

RESULTS:Sender:stg -u 1024 100 1.0.1.2 Receiver:rtg -u 1024 Parameters:Receiver side Collision Packets and Drop Packets Drops at node 5 : 324-750 Drops at node 4 : 274-930 Congestion window for source:-

Congestion window for destination:-

Dept. of CSE, BTLIT Page 15

Computer Networks Lab Record

8) Simulate simple BSS with the transmitting nodes in Wireless LAN by simulation and determine the performance with respect to transmission of packets. STEPS:1. Draw the topology consisting of a: router b:host c:WLAN d:WLAN access point (two) 3. Go to Tools Menu, select the option Wireless LAN->Mobile nodes 4, Generate infrastructure mobile nodes IP 5. A dialog box appears. Entries to be done are: Subnet ID and Gateway IP As subnetid= 2 and gateway ip=1.0.1.0 6. Here, mobile nodes are the senders and the host is the receiver. 7. For each mobile node, add a command, ttcp - t - u - s - p portno i.e. ttcp -t -u -s -p 7000 and ttcp -t -u -s -p 7001 for anther node 8. For the receiver, add the following command ttcp - r - u - s - p portno i.e. ttcp -r -u -s -p 7000 9. Run the simulation. 10. View the result in filename result and find the number of packets dropped due to congestion. The screenshot of the topology is shown below:-

Dept. of CSE, BTLIT Page 16

Computer Networks Lab Record

RESULT:Click on access point. Goto wireless interface and tick on show transmission range and then click OK. Double click on Router -> Node Editor and then Left stack -> throughput of Incoming packets Right stack -> throughput of Outgoing packets Select mobile hosts and access points then click on. Tools -> WLAN mobile nodes-> WLAN Generate infrastructure. Subnet ID: Port number of router (2) Gateway ID: IP address of router Mobile Host 1 ttcp t u s p 3000 1.0.1.1 Mobile Host 1 ttcp t u s p 3001 1.0.1.1 Host(Receiver) ttcp r u s p 3000 ttcp r u s p 3001

Dept. of CSE, BTLIT Page 17

Computer Networks Lab Record

Part B
1) Write a program for error detecting code using CRC-CCITT (16-bits). Program:#include<stdio.h> #include<string.h> #define N strlen(g) char t[128], cs[128], g[]="10001000000100001"; 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++)
Dept. of CSE, BTLIT Page 18

Computer Networks Lab Record

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.\n"); else printf("No Error Detected.\n"); } Procedure for execution:$ vi 1.c $ cc 1.c o 1 $ ./1

Dept. of CSE, BTLIT Page 19

Computer Networks Lab Record

Output1:Enter poly: 10101 Generating Polynomial is: 10001000000100001 Modified t[u] is: 101010000000000000000 Checksum is: 0100001010010100 Final Code word is: 101010100001010010100 Test Error detection 0(yes) 1(no)? : 1 No Error Detected. Output2:Enter poly : 10101 Generating Polynomial is : 10001000000100001 Modified t[u] is : 101010000000000000000 Checksum is : 0100001010010100 Final Codeword is : 101010100001010010100 Test Error detection 0(yes) 1(no) ? : 0 Enter position where error is to inserted : 1 Errorneous data : 111010100001010010100 Error detected.

Dept. of CSE, BTLIT Page 20

Computer Networks Lab Record

2) Write a program for frame sorting technique used in buffers. Program:#include<stdio.h> struct frame { int fslno; char finfo[20]; }; struct frame arr[10],temp; int n; void sort() { int i,j,ex; for(i=0;i<n;i++) { ex=0; for(j=0;j<n;j++) if(arr[j].fslno>arr[j+1].fslno) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; ex++; } if(ex==0) break; } } int main() { int i,j; system("clear"); printf("enter the number of frames\n"); scanf("%d",&n); printf("enter the frame sequence number and frame contents\n");
Dept. of CSE, BTLIT Page 21

Computer Networks Lab Record

for(i=1;i<=n;i++) { scanf("%s",&arr[i].finfo); arr[i].fslno=rand()%n; for(j=1;j<=n;j++) { if(i==j) continue; if(arr[i].fslno == arr[j].fslno) { arr[i].fslno=rand()%n; j=0; } } printf("%d\t%s\n",arr[i].fslno,arr[i].finfo); } printf("Before sorting\n"); for(i=1;i<=n;i++) printf("%d\t%s\n",arr[i].fslno,arr[i].finfo); sort(); printf("the frames in sequence\n"); for(i=1;i<=n;i++) printf("%d\t%s\n",arr[i].fslno,arr[i].finfo); }

Dept. of CSE, BTLIT Page 22

Computer Networks Lab Record

Output:Enter the number of frames 5 Enter the frame sequence number and frame contents a 3 a b 1 b c 2 c d 4 d e 0 e Before sorting 3 a 1 b 2 c 4 d 0 e The frames in sequence 0 e 1 b 2 c 3 a 4 d

Dept. of CSE, BTLIT Page 23

Computer Networks Lab Record

3) Write a program for distance vector algorithm to find suitable path for transmission. Program:#include<stdio.h> #define infinity 999 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; printf("\nEnter the number of nodes : "); scanf("%d",&n); printf("\nEnter the cost matrix :\n"); for(i=0;i<n;i++) for(j=0;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=0;i<n;i++) for(j=0;j<n;j++) for(k=0;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++; }
Dept. of CSE, BTLIT Page 24

Computer Networks Lab Record

}while(count!=0); for(i=0;i<n;i++) { printf("\n\nState value for router %d is \n",i+1); for(j=0;j<n;j++) { printf("\t\nnode %d via %d Distance %d",j+1,rt[i].from[j]+1,rt[i].dist[j]); } } printf("\n\n"); }

Dept. of CSE, BTLIT Page 25

Computer Networks Lab Record

Output:Enter the number of nodes : 4 Enter the cost matrix : 0 5 11 6 5 0 8 999 11 8 0 10 6 999 10 0

State value for router 1 is node 1 via 1 Distance 0 node 2 via 2 Distance 5 node 3 via 3 Distance 11 node 4 via 4 Distance 6 State value for router 2 is node 1 via 1 Distance 5 node 2 via 2 Distance 0 node 3 via 3 Distance 8 node 4 via 1 Distance 11 State value for router 3 is node 1 via 1 Distance 11 node 2 via 2 Distance 8 node 3 via 3 Distance 0 node 4 via 4 Distance 10 State value for router 4 is node 1 via 1 Distance 6 node 2 via 1 Distance 11 node 3 via 3 Distance 10 node 4 via 4 Distance 0

Dept. of CSE, BTLIT Page 26

Computer Networks Lab Record

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. Server side program:#include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<sys/stat.h> #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<fcntl.h> int main() { int cont,create_socket,new_socket,addrlen,fd; int bufsize = 1024; char *buffer = malloc(bufsize); char fname[256]; struct sockaddr_in address; if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0) printf("The socket was created\n"); address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(15000); if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0) printf("Binding Socket\n"); listen(create_socket,3); addrlen = sizeof(struct sockaddr_in); new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen); if (new_socket > 0) printf("The Client is Connected...\n"); recv(new_socket,fname, 255,0); printf("A request for filename %s Received..\n", fname); if ((fd=open(fname, O_RDONLY))<0) {
Dept. of CSE, BTLIT Page 27

Computer Networks Lab Record

perror("File Open Failed"); exit(0); } while((cont=read(fd, buffer, bufsize))>0) { send(new_socket,buffer,cont,0); } printf("Request Completed\n"); close(new_socket); return close(create_socket); } Client side program:#include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<unistd.h> #include<stdlib.h> #include<stdio.h> int main(int argc,char *argv[]) { int create_socket,cont; int bufsize = 1024; char *buffer = malloc(bufsize); char fname[256]; struct sockaddr_in address; if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0) printf("The Socket was created\n"); address.sin_family = AF_INET; address.sin_port = htons(15000); inet_pton(AF_INET,argv[1],&address.sin_addr); if (connect(create_socket,(struct sockaddr *) &address,sizeof(address)) == 0) printf("The connection was accepted with the server %s...\n",argv[1]); printf("Enter The Filename to Request : "); scanf("%s",fname); send(create_socket, fname, sizeof(fname), 0); printf("Request Accepted... Receiving File...\n\n");
Dept. of CSE, BTLIT Page 28

Computer Networks Lab Record

printf("The contents of file are...\n\n"); while((cont=recv(create_socket, buffer, bufsize, 0))>0) { write(1, buffer, cont); } printf("\nEOF\n"); return close(create_socket); } Server side output:/4s The socket was created Binding Socket The Client is Connected... A request for filename 4.txt Received.. Request Completed Client side output:./4c 127.0.0.1 The Socket was created The connection was accepted with the server 127.0.0.1... Enter The Filename to Request : 4.txt Request Accepted... Receiving File... The contents of file are... The contents of file are :This a demo of client server using Sockets Just for trial. Now End of file EOF

Dept. of CSE, BTLIT Page 29

Computer Networks Lab Record

5) Implement the above program using as message queues or FIFO as IPC channels. Server side program:#include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<string.h> #define FIFO1 "fifo1" #define FIFO2 "fifo2" #define PERMS 0666 char fname[256]; int main() { int readfd, writefd, fd; ssize_t n; char buff[512]; if (mkfifo(FIFO1, PERMS)<0) printf("Cant Create FIFO Files\n"); if (mkfifo(FIFO2, PERMS)<0) printf("Cant Create FIFO Files\n"); printf("Waiting for connection Request..\n"); readfd =open(FIFO1, O_RDONLY, 0); writefd=open(FIFO2, O_WRONLY, 0); printf("Connection Established..\n"); read(readfd, fname, 255); printf("Client has requested file %s\n", fname); if ((fd=open(fname,O_RDWR))<0) { strcpy(buff,"File does not exist..\n"); write(writefd, buff, strlen(buff)); } else { while((n=read(fd, buff,512))>0) write(writefd, buff, n); } close(readfd); unlink(FIFO1); close(writefd); unlink(FIFO2); } Client side program:Dept. of CSE, BTLIT Page 30

Computer Networks Lab Record

#include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<string.h> #include<fcntl.h> #define FIFO1 "fifo1" #define FIFO2 "fifo2" #define PERMS 0666 char fname[256]; int main() { ssize_t n; char buff[512]; int readfd,writefd; printf("Trying to Connect to Server..\n"); writefd = open(FIFO1, O_WRONLY, 0); readfd = open(FIFO2, O_RDONLY, 0); printf("Connected..\n"); printf("Enter the filename to request from server: "); scanf("%s",fname); write(writefd, fname, strlen(fname)); printf("Waiting for Server to reply..\n"); while((n=read(readfd,buff,512))>0) write(1,buff,n); close(readfd); close(writefd); return 0; }

Dept. of CSE, BTLIT Page 31

Computer Networks Lab Record

server side output:Waiting for connection Request.. Connection Established.. Client has requested file 5.txt client side output:Trying to Connect to Server.. Connected.. Enter the filename to request from server: 5.txt Waiting for Server to reply.. This a demo of client server using FIFO Just for trial. Now End of file

Dept. of CSE, BTLIT Page 32

Computer Networks Lab Record

6) Write a program for simple RSA algorithm to encrypt and decrypt the data. Program:#include<stdio.h> #include<string.h> #include<stdlib.h> int mult(int x,int y,int n) { long int k=1,j; for(j=1;j<=y;j++) k=(k*x)%n; return (int)k; } int main() { char msg[100]; int pt[100],ct[100],n,d,e,p,q,l,i,len; printf("\nEnter message : "); scanf("%s",&msg); len=strlen(msg); for(i=0;i<len;i++) pt[i]=msg[i]; n=253; d=17; e=13; printf("\nPublic key : (%d %d)\n",e,n); printf("\nPrivate key : (%d %d)\n",d,n); for(i=0;i<len;i++) ct[i]=mult(pt[i],e,n); printf("\n\tPlain text\t\tCipher text\n\n"); for(i=0;i<len;i++) printf("\t%c \t\t\t%d %d\n",pt[i],pt[i],ct[i]); for(i=0;i<len;i++) pt[i]=mult(ct[i],d,n); printf("\n\tCipher text\t\tPlain text\n"); for(i=0;i<len;i++) printf("\t%d \t\t\t%d %c\n",ct[i],pt[i],pt[i]); return 0; }
Dept. of CSE, BTLIT Page 33

Computer Networks Lab Record

Output:Enter message: college Public key: (13 253) Private key: (17 253) Plain text c o l l e g e Cipher text 66 122 3 3 173 86 173 Cipher text 99 111 108 108 101 103 101 66 122 3 3 173 86 173

Plain text 99 c 111 o 108 l 108 l 101 e 103 g 101 e

Dept. of CSE, BTLIT Page 34

Computer Networks Lab Record

7) Write a program for Hamming Code generation for error detection and correction. Program:#include<stdio.h> #include<stdlib.h> int main() { int dw[4]; int cw[3]; int synd[3]; int rcw[7]; int i,j; int hmatrix[3][8]={ {0,0,0,0,1,1,1,1}, {0,0,1,1,0,0,1,1}, {0,1,0,1,0,1,0,1}}; system("clear"); printf("\n\n\t\t\t*****Hamming Code*****"); printf("\n\nEnter the 4 bit dataword: "); for(i=0;i<4;i++) scanf(" %d",&dw[i]); cw[0]=(dw[2]+dw[3]+dw[0])%2; cw[1]=(dw[0]+dw[1]+dw[2])%2; cw[2]=(dw[1]+dw[2]+dw[3])%2; printf("\n\nEncoding, please wait........."); printf("\n\nThe codeword is: "); for(i=0;i<4;i++) printf(" %d",dw[i]); for(i=0;i<3;i++) printf(" %d",cw[i]); printf("\n\nTransmitting the codeword over the network....."); printf("\n\nCode word received."); printf("\n\nPlease enter the received codeword: "); for(i=0;i<7;i++) scanf("%d",&rcw[i]); printf("\n\nDecoding......."); synd[0]=(rcw[2]+rcw[3]+rcw[0]+rcw[4])%2;//s2 synd[1]=(rcw[0]+rcw[1]+rcw[2]+rcw[5])%2; synd[2]=(rcw[1]+rcw[2]+rcw[3]+rcw[6])%2; printf("\n\nThe syndrome is: ");
Dept. of CSE, BTLIT Page 35

Computer Networks Lab Record

for(i=0;i<3;i++) { printf(" %d",synd[i]); } for(j=0;j<8;j++) if((synd[0]==hmatrix[0][j])&&(synd[1]==hmatrix[1][j])&&(synd[2]==hmatrix[2][j])) break; switch(j) { case 0: printf("\n\nError! free\n\n"); break; case 1: printf("\n\nError! in the 7th bit(parity bit)"); printf("\n\nThe final dataword is:"); for(i=0;i<4;i++) printf(" %d",rcw[i]); printf("\n\n"); break; case 2: printf("\n\nError! in the 6th bit(parity bit)"); printf("\n\nThe final dataword is:"); for(i=0;i<4;i++) printf(" %d",rcw[i]); printf("\n\n"); break; case 3: printf("\n\nError! detected in the 2nd data bit:"); printf("\n\nAfter flipping the final dataword is:"); rcw[1]=!rcw[1]; for(i=0;i<4;i++) printf(" %d",rcw[i]); printf("\n\n"); break; case 4: printf("\n\nError! in the 5th bit(parity bit)");
Dept. of CSE, BTLIT Page 36

Computer Networks Lab Record

printf("\n\nThe final dataword is:"); for(i=0;i<4;i++) printf(" %d",rcw[i]); printf("\n\n"); break; case 5: printf("\n\nError! detected in the 4th data bit:"); printf("\n\nAfter flipping the final dataword is:"); rcw[3]=!rcw[3]; for(i=0;i<4;i++) printf(" %d",rcw[i]); printf("\n\n"); break; case 6: printf("\n\nError! detected in the 1st data bit:"); printf("\n\nAfter flipping the final dataword is:"); rcw[0]=!rcw[0]; for(i=0;i<4;i++) printf(" %d",rcw[i]); printf("\n\n"); break; case 7: printf("\n\nError! detected in the 3rd data bit:"); printf("\n\nAfter flipping the final dataword is:"); rcw[2]=!rcw[2]; for(i=0;i<4;i++) printf(" %d",rcw[i]); printf("\n\n"); break; } return 0; }

Dept. of CSE, BTLIT Page 37

Computer Networks Lab Record

Output:Enter the 4 bit dataword: 1 1 0 0 Encoding, please wait......... The codeword is: 1 1 0 0 1 0 1 Transmitting the codeword over the network..... Code word received. Please enter the received codeword: 1 1 0 0 1 0 1

Decoding....... The syndrome is: 0 0 0 Error! free Enter the 4 bit dataword: 0 1 0 1 Encoding, please wait......... The codeword is: 0 1 0 1 1 1 0 Transmitting the codeword over the network..... Code word received. Please enter the received codeword: 0 0 0 1 1 1 0 Decoding....... The syndrome is: 0 1 1 Error! detected in the 2nd data bit: After flipping the final dataword is: 0 1 0 1
Dept. of CSE, BTLIT Page 38

Computer Networks Lab Record

8) Write a program for congestion control using Leaky bucket algorithm. Program:#include<stdio.h> #include<math.h> #include<stdlib.h> int main() { int x1,ta,p[20],n,i,l,lct,a,b,it; int x=0; printf("\nEnter the bucket size: "); scanf("%d",&l); printf("\nEnter interarrival time: "); scanf("%d",&it); printf("\nEnter no.of packets: "); scanf("%d",&n); printf("\nEnter the arrival time of packets: "); for(i=0;i<n;i++) { scanf("%d",&p[i]); } lct=p[0]; for(i=0;i<n;i++) { ta=p[i]; printf("\nArrival time of packet--->%d",ta); a=ta-lct; b=x-a; x1=b; if(x1<0) { x1=0; x=x1+it; lct=ta; printf("\nConforming packet"); } else if(x1>l) { printf("\nNon conforming packet"); }
Dept. of CSE, BTLIT Page 39

Computer Networks Lab Record

else { x=x1+it; lct=ta; printf("\nConforming packet"); } } }

Output:Enter the bucket size: 6 Enter interarrival time: 4 Enter no.of packets: 11 Enter the arrival time of packets: 1 2 3 5 6 8 11 12 13 15 19 Arrival time of packet--->1 Conforming packet Arrival time of packet--->2 Conforming packet Arrival time of packet--->3 Conforming packet Arrival time of packet--->5 Non conforming packet Arrival time of packet--->6 Non conforming packet Arrival time of packet--->8 Conforming packet Arrival time of packet--->11
Dept. of CSE, BTLIT Page 40

Computer Networks Lab Record

Conforming packet Arrival time of packet--->12 Non conforming packet Arrival time of packet--->13 Non conforming packet Arrival time of packet--->15 Conforming packet Arrival time of packet--->19 Conforming packet

Dept. of CSE, BTLIT Page 41

You might also like