network lab manual.............. rasagna rajesh

40
Network Lab Manual 06CSL77 Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 1 VISVESVARAYA TECHNOLOGICAL UNIVERSITY Belgaum, Karnataka NETWORKS LAB MANUAL VII semester CSE CSE CSE CSE Prepared by Mrs. RASAGNA REDDY Asst. Prof. Department of Computer Science & Engineering CAMBRIDGE INSTITUTE OF TECHNOLOGY KR Puram, Bangalore

Upload: rasagna

Post on 30-Oct-2014

846 views

Category:

Documents


13 download

DESCRIPTION

This manual contains both PART A and PART B programs. All the programs are copies of executed ones.NOTE: Please read every note given in the pdf.

TRANSCRIPT

Page 1: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 1

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

Belgaum, Karnataka

NETWORKS LAB MANUAL

VII semester CSECSECSECSE

Prepared by

Mrs. RASAGNA REDDY Asst. Prof.

Department of Computer Science & Engineering

CAMBRIDGE INSTITUTE OF TECHNOLOGY

KR Puram, Bangalore

Page 2: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 2

PART A Path Settings: cd /usr/local/nctuns/bin/ Open 3 terminals. Terminal 1: ./dispatcher (Minimise)

Terminal 2: ./coordinator (Minimise)

Terminal 3: ./nctunsclient (Minimise)

1. Simulate a three nodes point – to – point network with duplex links between them. Set the queue size and vary the bandwidth and find the number of packets dropped.

Design Mode:

a) Take a hub and two hosts.

b) Make a Point to Point connection between host1 and hub and also between host2 and hub.

c) Save it.

Edit Mode (E):

a) Double click on host 1. A new window will be opened click on add and type the following command

stg –u 1024 200 (1.0.1.2) Receiver IP address should be given in command.

b) Click on Node editor and select Out throughput, drop log.

c) Now double click on host 2, add the following command

rtg –u –w log1

Click on Node editor and click MAC 8023. Select In throughput and drop log.

Page 3: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 3

Run Mode (R)

Goto Simulation Tab>run

Play Mode (P):

Click on play button in the bar at the bottom.

Goto Tools>Plot Graph , and start plotting for graphs for in, out and drop logs.

(Note: Logs are present in your file name .results file)

Drop Graph:

Out Throughput of sender:

In Throughput of receiver:

Page 4: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 4

Go Back to edit mode:

Double click on Point to point link and change bandwidth to 5(which is by default 10 in previous case)

Go to Run Mode.

Goto Simulation menu>Run

Now play and plot the graph to see the drop, in and out throughput.

INFERENCE:

Bandwidth : 10

Out Throughput: 1174 to 1180 bits/s.

In Throughput: 1174 to 1180 bits/s.

Drop: 0 to 0.

Bandwidth:5

Out Throughput: 590 to 594 bits/s;

In Throughput: 590 to 592 bits/s.

Drop: 0 to 2 bits/s.

(NOTE: To View the Min- Max values of each parameter go down to the results folder i.e go to Computer on desktop, Click on File System, usr>local>nctuns>bin. In Bin folder you can find your simulations result folder. Open the folder to view the parameters which you have set. When you open each parameter file, you can view two columns one is time and other is number of bits. Note the least value and highest value)

2. Simulate a four node point-to-point network with the links connected as follows:

Page 5: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 5

n0 – n2, n1 – n2 and n2 – n3. Apply TCP agent between n0-n3 and UDP between n1-n3. Apply relevant applications over TCP and UDP agents changing the parameter and determine the number of packets sent by TCP / UDP.

DESIGN MODE

EDIT MODE:

TCP/IP Command at Sender 1

stcp –p 2000 –l 1024 1.0.1.3

UDP Command at Sender 2

stg –u 1024 1.0.1.3

(Note: The receiver IP should be given. To know IP of any host place turtle on blue point on POINT to Point Link near HOST)

Click on Node editor (of respective hosts) click MAC 8023 and set the parameter Out throughputs for Sender 1 and Sender 2. Select In Throughput for Receiver.

RUN MODE:

Goto Simulation>Run.

PLAY MODE:

Play the model and plot the graphs for IN Throughput of receiver and Out Throughputs of senders.

IN Throughput:

Page 6: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 6

OUT Throughput at SENDER 1

OUT Throughput at SENDER 2

(to know why nothing is seen in graph see inference for sender 2 below)

INFERENCE:

Sender 1: OUT Throughput: 587 to 1124 bits/s.

Page 7: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 7

Sender 2: OUT Throughput: 0 to 0 bits/s.

Receiver: IN Throughput: 587 to 1124 bits/s.

3. Simulate the different types of Internet traffic such as FTP and TELNET over a network and analyze the throughput.

DESIGN MODE

EDIT MODE

Sender 1: stcp –p 21 –l 1024 1.0.1.3 (Note: RECEIVER 1’s IP)

Sender 2: stcp –p 23 –l 1024 1.0.1.4 (Note: RECEIVER 2’s IP)

Receiver 1: rtcp –p 21 –l 1024

Receiver 2: rtcp –p 23 –l 1024

Node Editor:

Parameters:

Sender 1: OUT throughput.

Sender 2: OUT throughput.

Receiver 1: IN throughput.

Receiver 2: IN throughput.

Sender 1 Out Throughput:

Page 8: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 8

Sender 2 Out Throughput:

Receiver 1 IN Throughput

Page 9: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 9

Receiver 2 IN Throughput

Play Mode:

Plot the graphs for above parameters.

INFERENCE:

Sender 1: Out Throghput: 1 to 1038 bits/s.

Sender 2: Out Throghput: 3 to 1094 bits/s.

Receiver 1: In Throghput: 1 to 1038 bits/s.

Receiver 2: In Throghput: 3 to 978 bits/s.

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.

DESIGN MODE:

SELECT Subnet symbol (plus symbol). Click on window. Enter hosts: 6 and radius as 125.

EDIT MODE

Page 10: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 10

Sender: stcp –p 2000 –l 1024 1.0.1.6

Receiver: rtcp –p 2000 –l 1024

Run MODE

Goto Simulation>Run

When the bar is moving in run mode, click on any host other than receiver and click on COMMAND CONSOLE, when a prompt is seen in window type ping IP of receiver(1.0.1.6).

The messages are seen as below.

5. Simulate an Ethernet LAN using n nodes (6-10), change error rate and data rate and compare throughput.

DESIGN MODE:

Draw the topology as below.

Page 11: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 11

EDIT MODE:

Sender 1: stcp –p 2000 –l 1024 1.0.1.3 (Receiver 1 IP)

Sender 2: stcp –p 2000 –l 1024 1.0.1.2 (Receiver 2 IP)

Sender 3: stcp –p 2000 –l 1024 1.0.1.4 (Receiver 3 IP)

Receiver 1: rtcp –p 2000 –l 1024

Receiver 2: rtcp –p 2000 –l 1024

Receiver 3: rtcp –p 2000 –l 1024

Node Editor:

Set In throughputs in receiver’s windows and OUT throughputs in Sender’s windows.

Run MODE:

Run the model

PLAY MODE:

Play the model and plot graphs for above parameters

.

Sender 1 OUT throughput

Page 12: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 12

Receiver 1 In Throughput

INFERENCE:

Sender 1 out throughput: 1 to 1197 bits/s.

Sender 2 out throughput: 1 to 784 bits/s.

Sender 3 out throughput: 431 to 1103 bits/s.

Receiver 1 in Throughput: 1 to 1197 bits/s.

Receiver 2 in Throughput: 431 to 1100 bits/s.

Receiver 3 in Throughput: 1 to 783 bits/s.

GO BACK TO EDIT MODE CHANGE BER (Data and Error rate) by double clicking on receivers point to point links to 0.000001 Now save the topology goto run mode and run the topology.

Play and check for throughputs as above.

Page 13: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 13

INFERENCE:

Sender 1 out throughput: 3 to 443 bits/s.

Sender 2 out throughput: 22 to 447 bits/s.

Sender 3 out throughput: 1 to 481 bits/s.

Receiver 1 in Throughput: 7 to 766 bits/s.

Receiver 2 in Throughput: 22 to 447 bits/s.

Receiver 3 in Throughput: 1 to 428 bits/s.

6.Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and determine collision across different nodes.

DESIGN MODE:

Draw the topology as below.

EDIT MODE:

Sender 1: stcp –p 2000 –l 1024 1.0.1.3 (Receiver 1 IP)

Sender 2: stcp –p 2000 –l 1024 1.0.1.2 (Receiver 2 IP)

Sender 3: stcp –p 2000 –l 1024 1.0.1.4 (Receiver 3 IP)

Receiver 1: rtcp –p 2000 –l 1024

Receiver 2: rtcp –p 2000 –l 1024

Receiver 3: rtcp –p 2000 –l 1024

Node Editor:

Set Collision parameter for different nodes and check for the values.

Run MODE:

Run the model

PLAY MODE:

Play the model and plot graphs for above parameters.

Collisions:

Page 14: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 14

INFERENCE:

Collision: 606 to 1190 bits/s.

7. Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and plot congestion window for different source / destination.

DESIGN MODE:

Draw the topology as below.

EDIT MODE:

Sender 1: stcp –p 2000 –l 1024 1.0.1.3 (Receiver 1 IP)

Sender 2: stcp –p 2000 –l 1024 1.0.1.2 (Receiver 2 IP)

Sender 3: stcp –p 2000 –l 1024 1.0.1.4 (Receiver 3 IP)

Receiver 1: rtcp –p 2000 –l 1024

Page 15: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 15

Receiver 2: rtcp –p 2000 –l 1024

Receiver 3: rtcp –p 2000 –l 1024

Node Editor:

Set Collision and drop parameters for different nodes and check for the values.

Run MODE:

Run the model

PLAY MODE:

Play the model and plot graphs for above parameters.

Collisions:

Drop:

INFERENCE:

Collision: 606 to 1190 bits/s.

Page 16: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 16

Drop: 606 to 1190 bits/s.

8.Simulate simple ESS and with transmitting nodes in wire-less LAN by simulation and determine the performance with respect to transmission of packets.

DESIGN MODE:

Drag and drop Access Point, two--Mobile Node in Infrastructure Mode( yellow mobile symbol), router and one host.

Make a Point to Point Connection between Host and Router, And also Router to Access Point.

EDIT MODE:

Double click on access point, go to Wireless Interface tab.

At bottom select show transmission range.

Go to Node editor MAC 8023 and select Out Throughput parameter.

Now double click on MOBILE NODE 1 and go to Application tab. Click on ADD and add the following command:

ttcp –t –u –s –p 2000 1.0.1.1 (HOST IP)

Click OK.

Now double click on MOBILE NODE 2 and go to Application tab. Click on ADD and add the following command:

ttcp –t –u –s –p 2001 1.0.1.1 (HOST IP)

Click OK.

Page 17: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 17

Double Click on router and select Node editor, Click on MAC 8023 and select IN and OUT throughputs. Click on other MAC 8023 and do the same. (Since there are two MAC 8023 select parameters for both).

Now double click on host and add command as follows:

ttcp –r –u –s –p 2000

click ok

Once again click on ADD and add the command

ttcp –r –u –s –p 2001

Click ok.

Goto Node Editor and click MAC 8023, select IN throughput.

Now select 2 mobile nodes and the access point. Goto tools>WLAN mobile nodes>Generate Infrastructure mobile nodes>

In SUBNET ID give PORT ID of Router

And in GATEWAY IP give IP of Router.

RUN MODE:

Simulation>Run the Model.

PLAY MODE:

Play the model and plot graphs for OUT throughput of Access Point and IN throughputs for both Router and Host.

Access Point OUT Throughput:

Page 18: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 18

Router IN Throughput:

Host IN Throughput:

INFERENCE:

Access Point: OUT Throughput: 255 to 760 bits/s.

Router: IN Throughput: 255 to 760 bits/s.

Host: IN Throughput: 255 to 760 bits/s.

PART B

1. Write a program for error detecting code using CRC CCIT(16 bits).

#include<stdio.h> #include<string.h> #define N strlen(g)

Page 19: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 19

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() { int flag=0; system("clear"); printf("Enter polynomial:\n"); scanf("%s",&t); printf("Generated divisor is :%s\n",g); a=strlen(t); for(e=a;e<a+N-1;e++) t[e]='0'; printf("Modified polynomial is %s\n",t); crc(); printf("Checksum is :%s\n",cs); for(e=a;e<a+N-1;e++) t[e]=cs[e-a]; printf("Final codeword is %s\n",t); printf("Test error detection?\n1(yes) 0(no):\n"); scanf("%d",&flag); if(flag==0) return; if(flag==1) {

Page 20: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 20

printf("Enter position where error is to be inserted:\n "); scanf("%d",&e); if(e<a+N-1){ t[e]=(t[e]=='0')?'1':'0'; printf("err data : %s\n",t); } else printf("Position where error should be created is above the length of Codeword...Hence\n"); } 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"); return; } OUTPUT: [root@god ~]# vi crc.c [root@god ~]# cc crc.c [root@god ~]# ./a.out Enter polynomial: 11101 Generated divisor is :10001000000100001 Modified polynomial is 111010000000000000000 Checksum is :1100001110011100 Final codeword is 111011100001110011100 Test error detection? 1(yes) 0(no): 1 Enter position where error is to be inserted: 2 err data : 110011100001110011100 Error detected [root@god ~]# ./a.out Enter polynomial: 11101 Generated divisor is :10001000000100001 Modified polynomial is 111010000000000000000 Checksum is :1100001110011100 Final codeword is 111011100001110011100 Test error detection? 1(yes) 0(no): 1 Enter position where error is to be inserted: 21

Page 21: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 21

Position where error should be created is above the length of Codeword...Hence No error detected [root@god ~]# 2.Write a program for frame sorting techniques used in buffers. #include<stdio.h> #include<stdlib.h> struct frame { int fslno; char finfo[20]; }; struct frame arr[20]; int n; void sort() { int i,j; struct frame temp; for(i=0;i<n-1;i++) { 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; } } } int main() { int i; system("clear"); printf("Enter the number of frames:\n"); scanf("%d",&n); printf("Enter frame sequence number and frame elements:\n"); for(i=0;i<n;i++) scanf("%d%s",&arr[i].fslno,&arr[i].finfo); sort();

Page 22: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 22

printf("The frames in sequence are....\n"); for(i=0;i<n;i++) printf(" %d\t %s\n",arr[i].fslno,arr[i].finfo); } OUTPUT: [root@god ~]# vi frame.c [root@god ~]# cc frame.c [root@god ~]# ./a.out Enter the number of frames: 4 Enter frame sequence number and frame elements: 4 Technology 1 Cambridge 3 of 2 Institute The frames in sequence are.... 1 Cambridge 2 Institute 3 of 4 Technology [root@god ~]# 3. Write a program for distance vector algorithm to find suitable path for transmission. #include<stdio.h> #include<stdlib.h> #include<ctype.h> int min(int a,int b) { return (a<b)?a:b; } void dist(int p[10][10],int n) { int i,j,k; for(k=1;k<=n;k++) for(j=1;j<=n;j++) for(i=1;i<=n;i++) p[i][j]=min(p[i][j],p[i][k]+p[k][j]); }

Page 23: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 23

int main() { int a[10][10],i,j,n,flag,temp=1; printf("Enter number of nodes:\n"); scanf("%d",&n); printf("Enter the matrix 0 for self loop and 999 for no path\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&a[i][j]); for(i=1;i<=n;i++) { printf("Initial record of %d is\n",i); for(j=1;j<=n;j++) printf("%d to %d is %d \n",i,j,a[i][j]); } dist(a,n); for(i=1;i<=n;i++) { printf("Updated record of %d is \n",i); for(j=1;j<=n;j++) printf("%d to %d is %d\n",i,j,a[i][j]); } while(temp) { do{ printf("Enter 0 to find shortest path and 1 to exit\n"); scanf("%d",&flag); if(flag==0) { printf("Enter 2 nodes\n"); scanf("%d %d",&i,&j); printf("%d to %d is %d\n",i,j,a[i][j]); } else temp=0; break; }while(flag!=0); } exit(0); } OUTPUT: [root@god ~]# vi dva.c [root@god ~]# cc dva.c [root@god ~]# ./a.out Enter number of nodes: 5

Page 24: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 24

Enter the matrix 0 for self loop and 999 for no path 0 1 2 3 999 1 0 999 4 0 2 999 0 7 999 3 4 7 0 5 999 0 999 5 0 Initial record of 1 is 1 to 1 is 0 1 to 2 is 1 1 to 3 is 2 1 to 4 is 3 1 to 5 is 999 Initial record of 2 is 2 to 1 is 1 2 to 2 is 0 2 to 3 is 999 2 to 4 is 4 2 to 5 is 0 Initial record of 3 is 3 to 1 is 2 3 to 2 is 999 3 to 3 is 0 3 to 4 is 7 3 to 5 is 999 Initial record of 4 is 4 to 1 is 3 4 to 2 is 4 4 to 3 is 7 4 to 4 is 0 4 to 5 is 5 Initial record of 5 is 5 to 1 is 999 5 to 2 is 0 5 to 3 is 999 5 to 4 is 5 5 to 5 is 0 Updated record of 1 is 1 to 1 is 0 1 to 2 is 1 1 to 3 is 2 1 to 4 is 3 1 to 5 is 1 Updated record of 2 is 2 to 1 is 1 2 to 2 is 0 2 to 3 is 3 2 to 4 is 4 2 to 5 is 0 Updated record of 3 is 3 to 1 is 2

Page 25: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 25

3 to 2 is 3 3 to 3 is 0 3 to 4 is 5 3 to 5 is 3 Updated record of 4 is 4 to 1 is 3 4 to 2 is 4 4 to 3 is 5 4 to 4 is 0 4 to 5 is 4 Updated record of 5 is 5 to 1 is 1 5 to 2 is 0 5 to 3 is 3 5 to 4 is 4 5 to 5 is 0 Enter 0 to find shortest path and 1 to exit 0 Enter 2 nodes 4 5 4 to 5 is 4 Enter 0 to find shortest path and 1 to exit 0 Enter 2 nodes 2 5 2 to 5 is 0 Enter 0 to find shortest path and 1 to exit 1 [root@god ~]# 4. Using TCP/IP sockets, write a client – server program to make the client send the file name and to make the server send back the contents of the requested file if present. SERVER CODE: #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<errno.h> #include<string.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<netinet/in.h> #include<sys/socket.h> #include<arpa/inet.h>

Page 26: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 26

#include<sys/wait.h> #include<signal.h> #define MYPORT 6490 #define BACKLOG 10 int main(void) { int sockfd,fp,new_fd; struct sockaddr_in my_addr,their_addr; int sin_size,i=0; int yes=1; char buf1[20],buf2[20000]; if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("socket"); exit(1); } if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))==-1) { perror("setsockopt"); exit(1); } my_addr.sin_family=AF_INET; my_addr.sin_port=htons(MYPORT); my_addr.sin_addr.s_addr=INADDR_ANY; memset(&(my_addr.sin_zero), '\0', 8); if(bind(sockfd,(struct sockaddr *)&my_addr, sizeof(struct sockaddr)) ==-1) { perror("Bind"); exit(1); } if(listen(sockfd, BACKLOG) == -1) { perror("listen"); exit(1); } printf("\n SERVER is online! \n SERVER: Waiting for the client........\n"); sin_size=sizeof(struct sockaddr_in); if((new_fd=accept(sockfd,(struct sockaddr *)&their_addr, &sin_size))==-1) { perror("Accept"); exit(0); } printf("\n SERVER: Got connection from %s \n", inet_ntoa(their_addr.sin_addr)); recv(new_fd,&buf1,sizeof(buf1),0); printf("File requested is %s\n", buf1); if((fp=open(buf1,O_RDONLY))<0) { printf("File not found\n"); strcpy(buf2,"File not found"); }

Page 27: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 27

else { printf("SERVER: %s found and ready to transfer.\n",buf1); read(fp,&buf2,20000); close(fp); } send(new_fd,&buf2,sizeof(buf2),0); close(new_fd); close(sockfd); printf("Transfer success \n"); printf("\n"); return 0; } CLIENT CODE: #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<errno.h> #include<string.h> #include<netdb.h> #include<sys/types.h> #include<netinet/in.h> #include<sys/socket.h> #include<fcntl.h> #define PORT 6490 int main() { int i=0,sockfd,len; char buf1[40],buf2[20000]; FILE* fp; struct sockaddr_in their_addr; if((sockfd=socket(AF_INET, SOCK_STREAM,0))==-1) { perror("socket"); exit(1); } their_addr.sin_family=AF_INET; their_addr.sin_port=htons(PORT); their_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); memset(&(their_addr.sin_zero), '\0', 8); if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))==-1) { perror("connect"); exit(1); } printf("CLIENT is online!\n"); printf("CLIENT:Enter the filename to be displayed: "); scanf("%s",buf1);

Page 28: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 28

send(sockfd,buf1,sizeof(buf1),0); if(recv(sockfd,buf2,sizeof(buf2),0)==1) { perror("recv"); exit(1); } else { printf("Displyaing the contents of %s",buf1); printf("\n%s\n",buf2); } close(sockfd); return 0; } OUTPUT: [root@god ~]# cc sockser.c [root@god ~]# ./a.out SERVER is online! SERVER: Waiting for the client........ SERVER: Got connection from 127.0.0.1 File requested is /root/sample SERVER: /root/sample found and ready to transfer. Transfer success [root@god ~]# [root@god ~]# vi sockc.c [root@god ~]# cc sockc.c [root@god ~]# ./a.out CLIENT is online! CLIENT:Enter the filename to be displayed: /root/sample Displyaing the contents of /root/sample This is Network programming lab [root@god ~]#

5. Implement the above program using as message queues or FIFOs as IPC channels. SERVER CODE: #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h>

Page 29: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 29

#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[300],r,ch; int num1,num2,f1,fd1,fd2,i=0; mknod(FIFO1_NAME,S_IFIFO|0666,0); mknod(FIFO2_NAME,S_IFIFO|0666,0); printf("Server Online!!!!!!!\n"); fd1=open(FIFO1_NAME,O_RDONLY); printf("Client Online!!!!\n Waiting for request..........\n\n"); while(1) { if((num1=read(fd1,p,100))==-1) perror("Read Error\n"); else { p[num1]='\0'; if((f1=open(p,O_RDONLY))<0) { printf("\n Server %s not found!!!!\n",p); exit(1); } else { printf("Server %s found!!!\n Transferring contents............\n",p); stdin=fdopen(f1,"r"); while((ch=(char)getc(stdin))!=EOF) { c[i]=ch; i++; } c[i]='\0'; fd2=open(FIFO2_NAME,O_WRONLY); printf("File content in server is\n %s\n",c); if(num2=write(fd2,c,strlen(c))==-1) perror("Transfer error\n"); else printf("Transfer completed......\n");

Page 30: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 30

exit(1); } } } } CLIENT CODE: #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<fcntl.h> #include<sys/stat.h> #include<unistd.h> #define FIFO1_NAME "fifo1" #define FIFO2_NAME "fifo2" int main() { char p[100],f[100],c[300]; int num1,num2,f1,fd1,fd2; mknod(FIFO1_NAME,S_IFIFO|0666,0); mknod(FIFO2_NAME,S_IFIFO|0666,0); printf("Waiting for Server.....!!\n\n"); fd1=open(FIFO1_NAME,O_WRONLY); printf("File descriptor fd is %d\n\n",fd1); if(fd1<0) { printf("File Open Error....\n"); getchar(); exit(1); } else printf("\n Server Online....\n Client: Enter the path\n\n"); scanf("%s",p); while(!feof(stdin)) { if((num1=write(fd1,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 received!!!\n Displaying the contents.....!!!\n\n");

Page 31: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 31

printf("%s",c); exit(1); } } } } OUTPUT: (Note: Create a new file named sample and type some lines in it. Now open two terminals simultaneously and execute server in one window and then client in another window.) [root@god ~]# cc fserver.c [root@god ~]# ./a.out Server Online!!!!!!! Client Online!!!! Waiting for request.......... Server /root/sample found!!! Transferring contents............ File content in server is This is Network programming lab Transfer completed...... [root@god ~]# [root@god ~]# cc fclient.c [root@god ~]# ./a.out Waiting for Server.....!! File descriptor fd is 3 Server Online.... Client: Enter the path /root/sample Waiting for reply.... File received!!! Displaying the contents.....!!! This is Network programming lab [root@god ~]

6. Write a program for simple RSA algorithm to encrypt and decrypt the data. #include<stdio.h> #include<stdlib.h> #include<ctype.h>

Page 32: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 32

#include<string.h> long int e,d,n; long int val[50]; char decode(long int ch) { int i; long int temp=ch; for(i=1;i<d;i++) ch=temp *ch%n; return(ch); } int gcd(long a,long b) { if(b==0) return (a); else return (gcd(b,a%b)); } int prime(int a) { int i; for(i=2;i<a;i++) { if((a%i)==0) return 0; } return 1; } int encode(char ch) { int i; long int temp=ch; for(i=1;i<e;i++) temp=temp*ch%n; return temp; } int main() { int i; long int p,q,phi,c[50]; char text[50],ctext[50]; printf("Enter text to be encoded:\n"); scanf("%[^\n]",text);

Page 33: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 33

printf("%s\n",text); do { p=rand()%30; }while(!prime(p)); do { q=rand()%30; }while(!prime(q)); n=p*q; phi=(p-1)*(q-1); printf("\n p=%d\t q=%d\t n=%d\t phi=%d\n",p,q,n,phi); do { e=rand()%phi; }while(!gcd(e,phi)); do { d=rand()%phi; }while((d*e%phi)!=1); printf("\n************Encoding message**************\n"); sleep(2); for(i=0;text[i]!='\0';i++) val[i]=encode(text[i]); val[i]=-999; printf("\nEncoded message is.............\n"); for(i=0;val[i]!=-999;i++) printf("%ld",val[i]); printf("\n************Decoding message**************\n"); sleep(2); for(i=0;val[i]!=-999;i++) ctext[i]=decode(val[i]); ctext[i]='\0';

Page 34: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 34

printf("\nDecoded message is.............\n"); printf("%s\n",ctext); } OUTPUT: [root@god ~]# vi rsa.c [root@god ~]# cc rsa.c [root@god ~]# ./a.out Enter text to be encoded: Welcome to Cambridge Institute of Technology Welcome to Cambridge Institute of Technology p=13 q=23 n=299 phi=264 ************Encoding message************** Encoded message is............. 1657514783227268759811622798761112681231142484819475981872801841162481163911675982272898228758313280227147227194238 ************Decoding message************** Decoded message is............. Welcome to Cambridge Institute of Technology [root@god ~]#

7. Write a program for Hamming code generation for error detection and correction. #include<stdio.h> #include<math.h> #include<stdlib.h> int main() { int i,j,k,count,err_pos=0,flag=0; char dw[20],cw[20],data[20]; printf("Enter 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++; } }

Page 35: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 35

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\n Enter 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) err_pos=err_pos+(int)pow(2,i);

Page 36: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 36

} if(err_pos==0) printf("\n\n There is no error in received code word\n"); else { if(cw[err_pos]==dw[err_pos]) { printf("\n\n There are 2 or more errors in received code....\n\n"); printf("SORRy....!Hamming code cannot correct 2 or more errors.....\n"); flag=1; } else printf("\nThere is an error in bit position %d of received code word\n\n",err_pos); if(flag==0) { cw[err_pos]=(cw[err_pos]=='1') ? '0' : '1'; printf("\n\n Corrected code word is \n\n"); for(i=1;i<12;i++) printf("%c",cw[i]); } } printf("\n\n"); } OUTPUT: [root@god ~]# vi hamming.c [root@god ~]# gcc hamming.c -lm [root@god ~]# ./a.out Enter data as binary bit stream (7 bits): 1110110 Code word is 11101100110 Enter the received hamming code 10101100110 There is an error in bit position 2 of received code word

Page 37: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 37

Corrected code word is 11101100110 [root@god ~]# ./a.out Enter data as binary bit stream (7 bits): 1110110 Code word is 11101100110 Enter the received hamming code 00101100110 There are 2 or more errors in received code.... SORRy....!Hamming code cannot correct 2 or more errors..... [root@god ~]#

8. Write a program for congestion control using leaky bucket algorithm. #include<stdio.h> #include<string.h> struct frame { char msg[20]; int seq; }fr[40]; int fno,front=0,rear=-1,count=0; char q[10][20]; void insert(int i) { if(count==3) { printf("Bucket is full......\n"); printf("\nPacket lost is %s\n",fr[i].msg); sleep(3); } else { rear=(rear+1)%3; strcpy(q[rear],fr[i].msg); count++;

Page 38: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 38

printf("\n Inserted packet into the bucket is %s\n",fr[i].msg); sleep(5); } } void transmitted() { if(count==0) { printf("\n\nBucket is empty\n"); return; } printf("\n\nTransmitted packet is %s\n",q[front]); sleep(5); front=(front+1)%3; count--; } int main() { int i,j,k,arrival[40],clk=20,n; char str[100]; printf("Enter the message\n"); scanf("%[^\n]",str); for(i=0;str[i]!='\0';) { fr[fno].seq=fno; for(j=i,k=0;str[j]!='\0'&& k<5;j++,k++) fr[fno].msg[k]=str[j]; fr[fno].msg[k]='\0'; fno++; i=j; } printf("Show the packets\n"); for(i=0;i<fno;i++) printf("\n Packet %d is : %s\n",i,fr[i].msg); sleep(5); for(i=0;i<fno;i++) { n=random()%3; arrival[i]=arrival[i-1]+n; } printf("Arrival time of packets is.....\n"); for(i=0;i<fno;i++) printf("Packet %d %s : %d\n", i, fr[i].msg,arrival[i]); printf("Size of bucket is 3\n\n"); sleep(5); i=0;

Page 39: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 39

while(i<fno || count!=0) { printf("CLK::%d\n",clk); while(clk==arrival[i]) { insert(i); i++; } if((clk%5)==0) transmitted(); clk++; } } OUTPUT: [root@god ~]# cc leaky.c [root@god ~]# ./a.out Enter the message Welcome to Network lab Show the packets Packet 0 is : Welco Packet 1 is : me to Packet 2 is : Netw Packet 3 is : ork l Packet 4 is : ab Arrival time of packets is..... Packet 0 Welco : 21 Packet 1 me to : 22 Packet 2 Netw : 22 Packet 3 ork l : 23 Packet 4 ab : 25 Size of bucket is 3 CLK::20 Bucket is empty CLK::21 Inserted packet into the bucket is Welco CLK::22 Inserted packet into the bucket is me to Inserted packet into the bucket is Netw

Page 40: NETWORK LAB MANUAL.............. RASAGNA RAJESH

Network Lab Manual 06CSL77

Mrs. Rasagna Reddy, AP, Dept. Of CSE Page 40

CLK::23 Bucket is full...... Packet lost is ork l CLK::24 CLK::25 Bucket is full...... Packet lost is ab Transmitted packet is Welco CLK::26 CLK::27 CLK::28 CLK::29 CLK::30 Transmitted packet is me to CLK::31 CLK::32 CLK::33 CLK::34 CLK::35 Transmitted packet is Netw [root@god ~]#