Mca Cn Lab Manual 2019 Vtu

  • Uploaded by: Hemanth Aradhya
  • 0
  • 0
  • March 2021
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Mca Cn Lab Manual 2019 Vtu as PDF for free.

More details

  • Words: 3,462
  • Pages: 22
Loading documents preview...
VISVESVARAYA TECHNOLOGICAL UNIVERSITY “Jnana Sangama”, Belgaum-590018

COMPUTER NETWORKS LABORATORY (18MCA28)

LAB MANUAL For

II SEMESTER M C A PREPARED BY

Rama Satish K V Assistant Professor, Department of MCA,

RNS Institute of Technology, Bengaluru

An Institute with a difference Estd: 2001

DEPARTMENT OF MASTER OF COMPUTER APPLICATIONS (M C A)

Computer Networks Laboratory [As per Choice Based Credit System (CBCS) scheme] SEMESTER –III Laboratory Code 16MCA36 CIE Marks Number of LAB Hours/Week 01 Hr Tutorial/Instructions SEE Marks 02 Hours Laboratory SEE Hours PART – A Note: Implement the following Computer Networks concepts using C/C++

20 80 03

1. Write a program for distance vector algorithm to find suitable path for transmission.

#include<stdio.h> #include<stdlib.h> int a[8][8],n; void floyd() { int i,j,k; for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { a[i][j]=min(a[i][j],a[i][k]+a[k][j]); } } } } int min(int a,int b) { return a
2

{ scanf("%d",&a[i][j]); if(a[i][j]==0) a[i][j]=999; if(i==j) a[i][j]=0; } floyd(); printf("\n Distance Vector Matrix \n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf(" %2d",a[i][j]); } printf("\n"); } } Output: Enter the number of routers:4 Enter the distance matrix values 0 999 3 999 2 0 999 999 999 7 0 1 6 999 999 0 Distance Vector Matrix is 0 10 3 4 2 0 5 6 7 7 0 1 6 16 9 0

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

3

2. 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.

Algorithm (Client Side) 1. 2. 3. 4. 5. 6.

Start. Create a socket using socket() system call. Connect the socket to the address of the server using connect() system call. Send the filename of required file using send() system call. Read the contents of the file sent by server by recv() system call. Stop.

Algorithm (Server Side) 1. 2. 3. 4. 5. 6. 7.

Start. Create a socket using socket() system call. Bind the socket to an address using bind() system call. Listen to the connection using listen() system call. accept connection using accept() Receive filename and transfer contents of file with client. Stop.

Program /*Server.c*/ #include #include #include #include

<stdio.h> <arpa/inet.h>

int main() { int welcome, new_soc, fd, n; char buffer[1024], fname[50]; struct sockaddr_in addr; welcome = socket(PF_INET, SOCK_STREAM, 0); addr.sin_family = AF_INET; addr.sin_port = htons(7891); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); bind(welcome, (struct sockaddr *) &addr, sizeof(addr)); printf("\nServer is Online"); /* listen for connections from the socket */ listen(welcome, 5); /* accept a connection, we get a file descriptor */ new_soc = accept(welcome, NULL, NULL); /* receive the filename */ recv(new_soc, fname, 50, 0); printf("\nRequesting for file: %s\n", fname);

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

4

/* open the file and send its contents */ fd = open(fname, O_RDONLY); if (fd < 0) send(new_soc, "\nFile not found\n", 15, 0); else while ((n = read(fd, buffer, sizeof(buffer))) > 0) send(new_soc, buffer, n, 0); printf("\nRequest sent\n"); close(fd); return 0; } /*Client.c*/ #include #include #include #include

<stdio.h> <arpa/inet.h>

int main() { int soc, n; char buffer[1024], fname[50]; struct sockaddr_in addr; /* socket creates an endpoint for comn. & returns a descriptor */ soc = socket(PF_INET, SOCK_STREAM, 0); /* * sockaddr_in is used for ip manipulation * we define the port and IP for the connection. */ addr.sin_family = AF_INET; addr.sin_port = htons(7891); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); /* keep trying to esatablish connection with server */ while(connect(soc, (struct sockaddr *) &addr, sizeof(addr))) ; printf("\nClient is connected to Server"); printf("\nEnter file name: "); scanf("%s", fname); /* send the filename to the server */ send(soc, fname, sizeof(fname), 0);

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

5

printf("\nRecieved response\n"); /* keep printing any data received from the server */ while ((n = recv(soc, buffer, sizeof(buffer), 0)) > 0) printf("%s", buffer); return 0; } Output (Server) [root@localhost CN Lab] ./s.o Socket Created.. Binding Socket.. Now Listening for Request.. The Client 127.0.0.1 is trying to connect… A request for filename alpha received.. Requested completed.. [root@localhost CN Lab]

Output (Client) [root@localhost CN Lab] ./c.o 127.0.0.1 Socket Created.. Connetion is accepted by 127.0.0.1 .. Enter File Name to Request : alpha Requestion for file alpha.. Request accepted. Receiving file… The contents of file are :This a demo of client server using Sockets Just for trial. Now End of file EOF

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

6

3. Write a program for Hamming code generation for error detection and correction.

#include<stdio.h> void main() { int data[10]; int dataatrec[10],c,c1,c2,c3,i; printf("Enter 4 bits of data one by one\n"); scanf("%d",&data[0]); scanf("%d",&data[1]); scanf("%d",&data[2]); scanf("%d",&data[4]); //Calculation of even parity data[6]=data[0]^data[2]^data[4]; data[5]=data[0]^data[1]^data[4]; data[3]=data[0]^data[1]^data[2]; printf("\nEncoded data is\n"); for(i=0;i<7;i++) printf("%d",data[i]); printf("\n\nEnter received data bits one by one\n"); for(i=0;i<7;i++) scanf("%d",&dataatrec[i]); c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0]; c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0]; c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0]; c=c3*4+c2*2+c1 ; if(c==0) { printf("\nNo error while transmission of data\n"); } else { printf("\nError on position %d",c); printf("\nData sent : "); for(i=0;i<7;i++) printf("%d",data[i]);

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

7

printf("\nData received : "); for(i=0;i<7;i++) printf("%d",dataatrec[i]); printf("\nCorrect message is\n"); //if errorneous bit is 0 we complement it else vice versa if(dataatrec[7-c]==0) dataatrec[7-c]=1; else dataatrec[7-c]=0; for (i=0;i<7;i++) { printf("%d",dataatrec[i]); } } } Output

4.

Write a program for congestion control using leaky bucket algorithm.

Theory The congesting control algorithms are basically divided into two groups: open loop and closed loop. Open loop solutions attempt to solve the problem by good design, in essence, to make sure it does not

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

8

occur in the first place. Once the system is up and running, midcourse corrections are not made. Open loop algorithms are further divided into ones that act at source versus ones that act at the destination. In contrast, closed loop solutions are based on the concept of a feedback loop if there is any congestion. Closed loop algorithms are also divided into two sub categories: explicit feedback and implicit feedback. In explicit feedback algorithms, packets are sent back from the point of congestion to warn the source. In implicit algorithm, the source deduces the existence of congestion by making local observation, such as the time needed for acknowledgment to come back. The presence of congestion means that the load is (temporarily) greater than the resources (in part of the system) can handle. For subnets that use virtual circuits internally, these methods can be used at the network layer. Another open loop method to help manage congestion is forcing the packet to be transmitted at a more predictable rate. This approach to congestion management is widely used in ATM networks and is called traffic shaping. The other method is the leaky bucket algorithm. Each host is connected to the network by an interface containing a leaky bucket, that is, a finite internal queue. If a packet arrives at the queue when it is full, the packet is discarded. In other words, if one or more process are already queued, the new packet is unceremoniously discarded. This arrangement can be built into the hardware interface or simulate d by the host operating system. In fact it is nothing other than a single server queuing system with constant service time. The host is allowed to put one packet per clock tick onto the network. This mechanism turns an uneven flow of packet from the user process inside the host into an even flow of packet onto the network, smoothing out bursts and greatly reducing the chances of congestion.

#include #include<dos.h> #include<stdlib.h> #define bucketSize 512 void bktInput(int a,int b) { if(a>bucketSize)

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

9

cout<<"\n\t\tBucket overflow"; else { delay(500); while(a>b){ cout<<"\n\t\t"<0) cout<<"\n\t\tLast "<>op; for(int i=1;i<=5;i++){ delay(random(1000)); pktSize=random(1000); cout<<"\nPacket no "<<<"\tPacket size = "<
Packet no 3 Packet no 4

Packet size = 3 Bucket output successful Last 3 bytes sent Packet size = 33 Bucket output successful Last 33 bytes sent Packet size = 117 Bucket output successful 100 bytes outputted. Last 17 bytes sent Packet size = 95 Bucket output successful Last 95 bytes sent Packet size = 949 Bucket overflow

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

10

PART – B Note: Simulate the following Computer Networks concepts using any network simulators. 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.

#Create simulator set ns [new Simulator] set tracefile [open lab1.tr w] $ns trace-all $tracefile set namfile [open lab1.nam w] $ns namtrace-all $namfile #create node set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] #create link $ns duplex-link $n0 $n1 2Mb 10ms DropTail $ns duplex-link $n1 $n2 0.5Mb 10ms DropTail #Set Queue Size $ns queue-limit $n0 $n1 05 $ns queue-limit $n1 $n2 05 #setup tcp connection set tcp [new Agent/TCP] $ns attach-agent $n0 $tcp #set sink to node set sink [new Agent/TCPSink] $ns attach-agent $n2 $sink #connect tcp src and sink $ns connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set packetSize_ 1024 proc finish {} { global ns tracefile namfile $ns flush-trace close $tracefile close $namfile Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

11

set awkCode { BEGIN {} { if ($1 == "d" && $4 = 2 ) { dcount = dcount + $6 print $2, dcount >> "lab1.data" } } END {} } exec awk $awkCode lab1.tr exec xgraph -bb -tk -x Time -y Bytes lab1.data -bg white & exec nam lab1.nam & exit 0 } $ns $ns $ns $ns

at 0.0 "$ftp start" at 4.0 "$ftp stop" at 5.0 "finish" run

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

12

2. Simulate the network with five nodes n0, n1, n2, n3, n4, forming a star topology. The node n4 is at the center. Node n0 is a TCP source, which transmits packets to node n3 (a TCP sink) through the node n4. Node n1 is another traffic source, and sends UDP packets to node n2 through n4. The duration of the simulation time is 10 seconds.

# Create a new instance of a Simulator named "ns" set ns [new Simulator] set namfile [open ex_01.nam w] $ns namtrace-all $namfile set tracefile [open ex_01.tr w] $ns trace-all $tracefile set set set set set

n0 n1 n2 n3 n4

[$ns [$ns [$ns [$ns [$ns

node] node] node] node] node]

# Setting up links to design the $ns duplex-link $n0 $n4 1Mb 10ms $ns duplex-link $n1 $n4 1Mb 10ms $ns duplex-link $n4 $n3 1Mb 10ms $ns duplex-link $n4 $n2 1Mb 10ms

given topology DropTail DropTail DropTail DropTail

# Setting up TCP Source set tcp [new Agent/TCP] $ns attach-agent $n0 $tcp # Setting up TCP Destination (also known as TCP Sink) set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink # Setup connection between TCP Source and Destination $ns connect $tcp $sink # Enable FTP application on TCP Source set ftp [new Application/FTP] $ftp attach-agent $tcp # Setting up UDP Source set udp [new Agent/UDP] $ns attach-agent $n1 $udp # Setting up a dummy UDP Destination (There are no ACKs in UDP!) Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

13

set null [new Agent/Null] $ns attach-agent $n2 $null # Setup connection between UDP Source and Destination $ns connect $udp $null # Enable CBR application on UDP Source set cbr [new Application/Traffic/CBR] $cbr set packetSize_ 500 $cbr set interval_ 0.005 $cbr attach-agent $udp # Schedule $ns at 0.0 $ns at 0.0 $ns at 9.0 $ns at 9.0

traffic in the network by starting and stopping "$cbr start" ;# CBR application started "$ftp start" ;# FTP application started "$cbr stop" ;# CBR application stopped "$ftp stop" ;# FTP application stopped

# User defined procedure to terminate the simulation proc finish {} { global ns namfile tracefile $ns flush-trace close $namfile close $tracefile exec nam ex_01.nam & exit 0 } $ns at 10.0 "finish" $ns run

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

14

3. Simulate to study transmission of packets over Ethernet LAN and determine the number of packets drop destination.

# Create a new instance of a Simulator named "ns" set ns [new Simulator] # Open a new file to store Network Animator (NAM) set namfile [open lab3.nam w] $ns namtrace-all $namfile # Open a new file to store trace data which is used for *Analysis* set tracefile [open lab3.tr w] $ns trace-all $tracefile # Before we proceed, set the TCP packet size to 1500 bytes Agent/TCP set packetSize_ 1460 # Creating five instances of *node* inside the "ns" instance. set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] $ns duplex-link $n0 $n1 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns make-lan "$n2 $n3 $n4" 100Mb 1ms LL Queue/DropTail Mac/802_3 Channel Phy/WiredPhy # The link between node 1 and node 2 to behave as a faulty link set errmodel [new ErrorModel] $errmodel set rate_ 0.2 ;# error rate in the link is fixed to 0.2 $errmodel ranvar [new RandomVariable/Uniform] $errmodel drop-target [new Agent/Null] $ns lossmodel $errmodel $n1 $n2 # Setting up TCP Source set tcp [new Agent/TCP] $ns attach-agent $n0 $tcp # Setting up TCP Destination (also known as TCP Sink) set sink [new Agent/TCPSink] $ns attach-agent $n4 $sink # Setup connection between TCP Source and Destination $ns connect $tcp $sink Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

15

# Enable FTP application on TCP Source set ftp [new Application/FTP] $ftp attach-agent $tcp set filesize [expr 4*1024*1024] $ns at 0.0 "$ftp send $filesize" # User defined procedure to terminate the simulation proc finish {} { global ns namfile tracefile $ns flush-trace close $namfile ;# Closes ex_04.nam close $tracefile ;# Closes ex_04.tr set awkCode { BEGIN{} { if ($1 == "d" && $5 == "tcp" && $6 > 1460) { count_packets++; print $2, count_packets >> "lab3.data" } } END{} } exec awk $awkCode lab3.tr exec nam lab3.nam & exec xgraph -bb -tk -x Time -y Dropped_Packets lab3.data -bg white & exit 0 } $ns at 100.0 "finish" $ns run

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

16

4. Write a TCL Script to simulate working of multicasting routing protocol and analyze the throughput of the network.

set ns [new Simulator] $ns rtproto DV set namfile [open ex_08.nam w] $ns namtrace-all $namfile set tracefile [open ex_08.tr w] $ns trace-all $tracefile for {set i 0} {$i < 7} {incr i} { set n($i) [$ns node] } for {set i 0} {$i < 7} {incr i} { $ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail } $ns rtmodel-at 1.0 down $n(1) $n(2) $ns rtmodel-at 2.0 up $n(1) $n(2) $ns rtmodel-at 3.0 down $n(3) $n(2) $ns rtmodel-at 3.5 up $n(3) $n(2) # Setting up UDP Source set udp [new Agent/UDP] $ns attach-agent $n(0) $udp set null [new Agent/Null] $ns attach-agent $n(3) $null $ns connect $udp $null # Enable CBR application on UDP Source and set its parameters set cbr [new Application/Traffic/CBR] $cbr set packetSize_ 500 ;# packet size is set to 500 bytes $cbr set interval_ 0.005 ;# interval of 0.005 seconds $cbr attach-agent $udp ;# Configure the application to use UDP $ns at 0.5 "$cbr start" $ns at 4.5 "$cbr stop"

;# CBR application started ;# CBR application stopped

# User defined procedure to terminate the simulation proc finish {} { Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

17

global ns namfile tracefile $ns flush-trace close $namfile close $tracefile set awkCode { BEGIN{} { if ($1 == "r" && $4 == 3 && $6 >= 500) { count = count + $6 ; print $2, count >> "ex_08.data" } } END{} } exec awk $awkCode ex_08.tr exec xgraph -bb -tk -x Time -y Bytes ex_08.data -bg white & exec nam ex_08.nam & exit 0 } $ns at 10.0 "finish" $ns run 5. Simulate the different types of internet traffic such as FTP and TELNET over a wired network and analyze the throughput of the network.

#Create Simulator set ns [new Simulator] #Open Trace and NAM Trace File set ntrace [open prog5.tr w] $ns trace-all $ntrace set namfile [open prog5.nam w] $ns namtrace-all $namfile #Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

18

#Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns simplex-link $n2 $n3 1Mb 10ms DropTail $ns simplex-link $n3 $n2 1Mb 10ms DropTail #Set queue size and Monitor the queue $ns queue-limit $n0 $n2 10 $ns simplex-link-op $n0 $n2 queuePos 0.5 #Set TCP Connection between n0 and n3 set tcp0 [new Agent/TCP] $ns attach-agent $n0 $tcp0 set sink0 [new Agent/TCPSink] $ns attach-agent $n3 $sink0 $ns connect $tcp0 $sink0 #Attach FTP Application over TCP set ftp0 [new Application/FTP] $ftp0 attach-agent $tcp0 $ftp0 set type_ FTP #Set UDP Connection between n1 and n3 set udp0 [new Agent/UDP] $ns attach-agent $n1 $udp0 set null0 [new Agent/Null] $ns attach-agent $n3 $null0 $ns connect $udp0 $null0 #Attach Telnet Application over UDP set telnet [new Application/Telnet] $telnet attach-agent $udp0 $telnet set type_ Telnet #Finish Procedure proc Finish {} { global ns ntrace namfile #Dump all trace data and close the files $ns flush-trace close $ntrace close $namfile #Execute the nam animation file exec nam prog5.nam & Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

19

#Calculate throughput = (No of packets received/time taken) set numTcp [exec grep "^r" prog5.tr | grep "tcp" | tail -n 1 | cut -d " " -f 6] set TcpSize [exec grep "^r" prog5.tr | grep -c "tcp"] set tcpTime 24.0 set numUdp [exec grep "^r" prog5.tr | grep "udp" | tail -n 1 | cut -d " " -f 6] set UdpSize [exec grep "^r" prog5.tr | grep -c "udp"] set udpTime 23.9 puts "The throughput of FTP is " puts "[expr ($numTcp*$TcpSize)/$tcpTime] bytes per second" puts "The throughput of Telnet is " puts "[expr ($numUdp*$UdpSize)/$udpTime] bytes per second" exit 0 } #Schedule Events $ns at 0.1 "$telnet start" $ns at 0.5 "$ftp0 start" $ns at 23.9 "$telnet stop" $ns at 24.0 "$ftp0 stop" $ns at 25.0 "Finish" #Run Simulation $ns run

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

20

6. Simulate the transmission of ping messages over a network topology consisting of 6 nodes and find the round trip time.

#Create a simulator object set ns [new Simulator] #Open a trace file set nf [open lab6.nam w] $ns namtrace-all $nf set nd [open lab6.tr w] $ns trace-all $nd $ns color 1 Red $ns color 2 blue #Create six set n0 [$ns set n1 [$ns set n2 [$ns set n3 [$ns set n4 [$ns set n5 [$ns

nodes node] node] node] node] node] node]

#Connect the nodes with $ns duplex-link $n0 $n1 $ns duplex-link $n1 $n2 $ns duplex-link $n2 $n3 $ns duplex-link $n3 $n4 $ns duplex-link $n4 $n5 $ns $ns $ns $ns $ns

duplex-link-op duplex-link-op duplex-link-op duplex-link-op duplex-link-op

$n0 $n1 $n2 $n3 $n4

two links 0.1Mb 10ms 0.1Mb 10ms 0.1Mb 10ms 0.1Mb 10ms 0.1Mb 10ms $n1 $n2 $n3 $n4 $n5

orient orient orient orient orient

DropTail DropTail DropTail DropTail DropTail right right down left left

#Define a 'recv' function for the class 'Agent/Ping' Agent/Ping instproc recv {from rtt} { $self instvar node_ puts "node [$node_ id] received ping answer from \ #$from with round-trip-time $rtt ms." } #Create two ping agents and attach them to the nodes n0 and n2 Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

21

set p0 [new Agent/Ping] $ns attach-agent $n0 $p0 $p0 set fid_ 1 set p1 [new Agent/Ping] $ns attach-agent $n5 $p1 $p1 set fid_ 2 #Connect the two agents $ns connect $p0 $p1 #Define a 'finish' procedure proc finish {} { global ns nf nd $ns flush-trace close $nf close $nd exec nam lab6.nam & exit 0 } #Schedule events set time 0.01 set now [$ns now] $ns at [expr $now + $time] "$p0 send" $ns at [expr $now + $time] "$p1 send" $ns at 0.6 "$p0 send" $ns at 0.8 "$p1 send" $ns at 1.0 "finish" #Run the simulation $ns run

Prepared by : Rama Satish K V, Asst Professor, RNSIT [www.google.com/+ramasatishkv]

22

Related Documents

Lab Manual
January 2021 1
Biology Lab Manual
March 2021 0
Dbms Lab Manual
January 2021 1

More Documents from "Aamer"