Anda di halaman 1dari 35

Network Lab Manual

10CSL77

VISVESVARAYA TECHNOLOGICAL
UNIVERSITY
Belgaum, Karnataka

NETWORKS LAB MANUAL

VII semester
CSE

Prepared by
Ms. RASAGNA REDDY Asst. Prof.

Department of Computer Science & Engineering


CAMBRIDGE INSTITUTE OF TECHNOLOGY
KR Puram, Bangalore

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 1

Network Lab Manual

Ms. Rasagna Reddy, AP, Dept. Of CSE

10CSL77

Page 2

Network Lab Manual

10CSL77

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.

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 3

Network Lab Manual

10CSL77

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:

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 4

Network Lab Manual

10CSL77

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)

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 5

Network Lab Manual

10CSL77

2. Simulate a four node point-to-point network with the links connected as follows:
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.

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 6

Network Lab Manual

10CSL77

IN Throughput:

OUT Throughput at SENDER 1

OUT Throughput at SENDER 2

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

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 7

Network Lab Manual

10CSL77

INFERENCE:
Sender 1: OUT Throughput: 587 to 1124 bits/s.
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 1s IP)

Sender 2: stcp p 23 l 1024 1.0.1.4

(Note: RECEIVER 2s 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:
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 8

Network Lab Manual

Network Lab Manual

10CSL77

06CSL77

Sender 2 Out Throughput:

Receiver 1 IN Throughput

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 9

Network Lab Manual

10CSL77

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
Sender: stcp p 2000 l 1024 1.0.1.6
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 10

Network Lab Manual

10CSL77

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.

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 11

Network Lab Manual

10CSL77

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 receivers windows and OUT throughputs in Senders windows.
Run MODE:
Run the model
PLAY MODE:
Play the model and plot graphs for above parameters
Sender 1 OUT throughput

Receiver 1 In Throughput

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 12

Network Lab Manual

10CSL77

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.
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:
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 13

Network Lab Manual

10CSL77

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:

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.

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 14

Network Lab Manual

EDIT MODE:
Sender 1: stcp p 2000 l 1024 1.0.1.3
Sender 2: stcp p 2000 l 1024 1.0.1.2
Sender 3: stcp p 2000 l 1024 1.0.1.4
Receiver 1: rtcp p 2000 l 1024
Receiver 2: rtcp p 2000 l 1024
Receiver 3: rtcp p 2000 l 1024
Node Editor:

10CSL77

(Receiver 1 IP)
(Receiver 2 IP)
(Receiver 3 IP)

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.
Drop: 606 to 1190 bits/s.
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 15

Network Lab Manual

10CSL77

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

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 16

Network Lab Manual

10CSL77

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:

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 17

Network Lab Manual

10CSL77

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.

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 18

Network Lab Manual

10CSL77

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)
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)
{
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';
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 19

Network Lab Manual

10CSL77

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
Position where error should be created is above the length of Codeword...Hence No
error detected
[root@god ~]#

2. Write a program for distance vector algorithm to find suitable path for
transmission.
#include<stdio.h>
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 20

Network Lab Manual

10CSL77

#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]);
}
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]);
}
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 21

Network Lab Manual

10CSL77

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

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
34705
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
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 22

Network Lab Manual

10CSL77

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
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 ~]#

3. 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>
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 23

Network Lab Manual

10CSL77

#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>
#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);
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 24

Network Lab Manual

10CSL77

printf("File requested is %s\n", buf1);


if((fp=open(buf1,O_RDONLY))<0)
{
printf("File not found\n");
strcpy(buf2,"File not found");
}
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");
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 25

Network Lab Manual

10CSL77

printf("CLIENT:Enter the filename to be displayed: ");


scanf("%s",buf1);
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 ~]#

4. 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>
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 26

Network Lab Manual

10CSL77

#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");

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 27

Network Lab Manual

10CSL77

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");

printf("%s",c);
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 28

Network Lab Manual

10CSL77

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 ~]

5. Write a program for simple RSA algorithm to encrypt and decrypt the data.

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 29

Network Lab Manual

10CSL77

#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);
printf("%s\n",text);
do
{
p=rand()%30;
}while(!prime(p));
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 30

Network Lab Manual

10CSL77

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';
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**************
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 31

Network Lab Manual

10CSL77

Encoded message is.............


165751478322726875981162279876111268123114248481947598187280184116248
116
3911675982272898228758313280227147227194238
************Decoding message**************
Decoded message is.............
Welcome to Cambridge Institute of Technology [root@god ~]#
6. 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++;
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]);
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 32

Network Lab Manual

10CSL77

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;
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

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 33

Network Lab Manual

10CSL77

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
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
Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 34

Network Lab Manual

10CSL77

CLK::34
CLK::35
Transmitted packet is Netw
[root@god ~]#

Ms. Rasagna Reddy, AP, Dept. Of CSE

Page 35