KEMBAR78
NS2 Lab2 | PDF | Integer (Computer Science) | Ip Address
0% found this document useful (0 votes)
116 views11 pages

NS2 Lab2

The document describes a program to simulate subnetting and find subnet masks. It provides the objective, concepts, algorithm, required tools, sample code, and sample output of the program. The program takes an IP address as input, converts it to binary, calculates the subnet mask and number of addresses based on user input, and finds the first and last addresses in the subnet block. It implements the concepts of subnetting to divide IP addresses and route traffic within a network.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views11 pages

NS2 Lab2

The document describes a program to simulate subnetting and find subnet masks. It provides the objective, concepts, algorithm, required tools, sample code, and sample output of the program. The program takes an IP address as input, converts it to binary, calculates the subnet mask and number of addresses based on user input, and finds the first and last addresses in the subnet block. It implements the concepts of subnetting to divide IP addresses and route traffic within a network.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Implementation of Subnetting(1)

Aim:

Write a program to implement subnetting and find the subnet masks.

Algorithm :

1.Start the program.

2.Get the frame size from the user

3.To create the frame based on the user request.

4.To send frames to server from the client side.

5.If your frames reach the server it will send ACK signal to client otherwise it will

send NACK signal to client.

6.Stop the program

Program

import java.util.Scanner;

class Subnet

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print(―Enter the ip address: ―);

String ip = sc.nextLine();

String split_ip[] = ip.split(―\\.‖);

//SPlit the string after every .

String split_bip[] = new String[4];

//split binary ip

String bip = ―‖;

for(int i=0;i<4;i++){

split_bip[i] = appendZeros(Integer.toBinaryString(Integer.parseInt(split_ip[i])));
// ―18‖ => 18 => 10010 => 00010010

bip += split_bip[i];

System.out.println(―IP in binary is ―+bip);

System.out.print(―Enter the number of addresses: ―);

int n = sc.nextInt();

//Calculation of mask

int bits = (int)Math.ceil(Math.log(n)/Math.log(2)); /*eg if address = 120, log 120/log 2 gives log

to the base 2 => 6.9068, ceil gives us upper integer */

System.out.println(―Number of bits required for address = ―+bits);

int mask = 32-bits;

System.out.println(―The subnet mask is = ―+mask);

//Calculation of first address and last address

int fbip[] = new int[32];

for(int i=0; i<32;i++) fbip[i] = (int)bip.charAt(i)-48; //convert cahracter 0,1 to integer 0,1

for(int i=31;i>31-bits;i–)//Get first address by ANDing last n bits with 0

fbip[i] &= 0;

String fip[] = {―‖,‖‖,‖‖,‖‖};

for(int i=0;i<32;i++)

fip[i/8] = new String(fip[i/8]+fbip[i]);

System.out.print(―First address is = ―);

for(int i=0;i<4;i++){

System.out.print(Integer.parseInt(fip[i],2));

if(i!=3) System.out.print(―.‖);

System.out.println();

int lbip[] = new int[32];


for(int i=0; i<32;i++) lbip[i] = (int)bip.charAt(i)-48; //convert cahracter 0,1 to integer 0,1

for(int i=31;i>31-bits;i–)//Get last address by ORing last n bits with 1

lbip[i] |= 1;

String lip[] = {―‖,‖‖,‖‖,‖‖};

for(int i=0;i<32;i++)

lip[i/8] = new String(lip[i/8]+lbip[i]);

System.out.print(―Last address is = ―);

for(int i=0;i<4;i++){

System.out.print(Integer.parseInt(lip[i],2));

if(i!=3) System.out.print(―.‖);

System.out.println();

static String appendZeros(String s){

String temp = new String(―00000000″);

return temp.substring(s.length())+ s;

www.vidyarthiplus.com

www.vidyarthiplus.com

Output

Enter the ip address: 100.110.150.10

IP in binary is 01100100011011101001011000001010

Enter the number of addresses: 7

Number of bits required for address = 3

The subnet mask is = 29

First address is = 100.110.150.8


Last address is = 100.110.150.15

Result :

Thus the Program was displayed implement subnetting and find the subnet masks.
IMPLEMENTATION OF SUBNETTING

Objective:

To find Subnet Mask and Network ID for given IP Address .

Learning Outcomes:

After the completion of this experiment, student will be able to

 Understand how to calculate subnets in binary and decimal.

 Understand how to do subnet mask calculations and break down IP address classes to route

traffic within network.

Problem Statement:

Write a program to implement subnetting and find the subnet masks.

Concept:

If an organization was granted a large block in class A or B, it could divide the addresses into

several contiguous groups and assign each group to smaller networks (called subnets) or, in rare
cases,

share part of the addresses with neighbours.

Algorithm:

Step1: Get the input from the user by using scanner method.

Step 2: Read the input by using nextLine() and store it.

Step 3: Split the string based on string by using split(“\\”)

Step4 : Convert it into binary.

Step 5: calculating the network mask by using math and logarithmic

Step 6: get the first address by ANDding the last n bits with 0.

Step7 : get the last address by ANDding the last n bits with 1.

System and Software tools required:

Package Required : Java Compiler


Operating System : UBUNTU

Minimum Requirement : Pentium III or Pentium IV with 2GB RAM 40 GB hard disk

43

Sample Coding:

//…Calculation of mask…//

int bits = (int)Math.ceil(Math.log(n)/Math.log(2));

/*eg if address = 120, log 120/log 2 gives log to the base 2 => 6.9068, ceil gives us upper integer */

System.out.println("Number of bits required for address = "+bits);

int mask = 32-bits;

System.out.println("The subnet mask is = "+mask);

//…Calculation of first address and last address…//

intfbip[] = new int[32];

for(int i=0; i<32;i++) fbip[i] = (int)bip.charAt(i)-48;

//convert cahracter 0,1 to integer 0,1

for(int i=31;i>31-bits;i--)//Get first address by ANDing last n bits with 0

fbip[i] &= 0;

String fip[] = {"","","",""};

for(int i=0;i<32;i++)

fip[i/8] = new String(fip[i/8]+fbip[i]);

System.out.print("First address is = ");

for(int i=0;i<4;i++){

System.out.print(Integer.parseInt(fip[i],2));

if(i!=3) System.out.print("."); }

Sample Output:

Testcase:

 The first address in the block can be found by setting the rightmost 32-n bits to 0s

 The last address in the block can be found by setting the rightmost 32-n bits to 1s.
 Try to find out the number of address in the block.

Simulation of Ethernet Lan

Experiment Specific Instructions

1. To analyze the given problem you have to write a Tcl script and simulate with ns2

2. Begin by specifying the trace files and the nam files to be created

3. Define a finish procedure

4. Determine and create the nodes that will be used to create the topology. Here in our

experiment we are selecting 6 nodes namely 0, 1, 2, 3, 4, 5

5. Create the links to connect the nodes

6. Set up the LAN by specifying the nodes, and assign values for bandwidth, delay,

queue type and channel to it

7. Optionally you can position and orient the nodes and links to view a nice video

output with Nam

8. Set up the TCP and/or UDP connection(s) and the FTP/CBR (or any other

application) that will run over it

9. Schedule the different events like simulation start and stop, data transmission start

and stop

10. Call the finish procedure and mention the time at what time your simulation will

end

11. Execute the script with ns

Simulation Script:

#Lan simulation

set ns [new Simulator]

#define color for data flows

$ns color 1 Blue


$ns color 2 Red

#open tracefiles

set tracefile1 [open out.tr w]

set winfile [open winfile w]

$ns trace-all $tracefile1

#open nam file

set namfile [open out.nam w]

$ns namtrace-all $namfile

#define the finish procedure

proc finish {} {

global ns tracefile1 namfile

$ns flush-trace

close $tracefile1

close $namfile

exec nam out.nam &

exit 0

} #create six nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]

$n1 color Red

$n1 shape box

#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 0.3Mb 100ms DropTail

$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail

set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail

MAC/Csma/Cd Channel]

#Give node position

$ns duplex-link-op $n0 $n2 orient right-down

$ns duplex-link-op $n1 $n2 orient right-up

$ns simplex-link-op $n2 $n3 orient right

$ns simplex-link-op $n3 $n2 orient left

#set queue size of link(n2-n3) to 20

$ns queue-limit $n2 $n3 20

#setup TCP connection

set tcp [new Agent/TCP/Newreno]

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink/DelAck]

$ns attach-agent $n4 $sink

$ns connect $tcp $sink

$tcp set fid_ 1

$tcp set packet_size_ 552

#set ftp over tcp connection

set ftp [new Application/FTP]

$ftp attach-agent $tcp

#setup a UDP connection

set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]


$ns attach-agent $n5 $null

$ns connect $udp $null

$udp set fid_ 2

#setup a CBR over UDP connection

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set type_ CBR

$cbr set packet_size_ 1000

$cbr set rate_ 0.01Mb

$cbr set random_ false

#scheduling the events

$ns at 0.1 "$cbr start"

$ns at 1.0 "$ftp start"

$ns at 124.0 "$ftp stop"

$ns at 125.5 "$cbr stop"

proc plotWindow {tcpSource file} {

global ns

set time 0.1

set now [$ns now]

set cwnd [$tcpSource set cwnd_]

puts $file "$now $cwnd"

$ns at [expr $now+$time] "plotWindow $tcpSource $file"

$ns at 0.1 "plotWindow $tcp $winfile"

$ns at 125.0 "finish"

$ns run
import java.io.*;

import java.util.*;
IMPLEMENT NETWORK TOPOLOGY

// Read only region start

class UserMainCode

public int topologytype(int input1,int input2,int[] input3,int[] input4){

// Read only region end

// Write code here...

if(input1 == 3){

if(input2 == 3){

return 3;

else{

return 1;

else if(input1 == 4){

return 2;

//throw new UnsupportedOperationException("topologytype(int input1,int


input2,int[] input3,int[] input4)");

EXPLAINATION:

Input Specification:

input1: The value N

input2: The value M


input3: Array of size M containing the starting point of the connections

input4: Array of size M containing the end point of the connections

Output Specification:

Return the type of topology the network represents.

Example 1:

input1: 3

input2: 3

input3: {1,2,3}

input3: {2,3,1}

Output: 3

Explanation:

It represents a ring topology.

Example 2:

input1: 3

input2: 2

input3: {1, 2}

input3: {2, 3}

Output: 1

Explanation:

It represents a bus topology.

You might also like