0% found this document useful (0 votes)
33 views

Write A Program To Demonstrate Subnetting and Find The Subnet Masks

The program demonstrates how to find the subnet mask for an IP address based on its class and then uses bitwise operations to find the network address and broadcast address for the subnet. It takes an IP address as input, determines the class, and outputs the subnet mask, network address, and broadcast address.

Uploaded by

Anuj Verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Write A Program To Demonstrate Subnetting and Find The Subnet Masks

The program demonstrates how to find the subnet mask for an IP address based on its class and then uses bitwise operations to find the network address and broadcast address for the subnet. It takes an IP address as input, determines the class, and outputs the subnet mask, network address, and broadcast address.

Uploaded by

Anuj Verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a program to demonstrate subnetting and find the subnet

masks.
import java.io.*;
import java.net.InetAddress;
public class Subnet1 {

public static void main(String[] args) throws IOException {

System.out.println("ENTER IP:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String ip = br.readLine();
String checkclass = ip.substring(0, 3);

int cc = Integer.parseInt(checkclass);
String mask = null;
if(cc>0)
{
if(cc<=127)
{
mask = "255.0.0.0";
System.out.println("Class A IP Address");
System.out.println("SUBNET MASK:\n"+mask);
}
if(cc>=128 && cc<=191)
{
mask = "255.255.0.0";
System.out.println("Class B IP Address");
System.out.println("SUBNET MASK:\n"+mask);
}
if(cc>=192 && cc<=223)
{
mask = "255.255.255.0";
System.out.println("Class C IP Address");
System.out.println("SUBNET MASK:\n"+mask);
}
if(cc>=224 && cc<=239)
{
mask = "255.0.0.0";
System.out.println("Class D IP Address Used for multicasting");
}
if(cc>=240 && cc<=254)
{
mask = "255.0.0.0";
System.out.println("Class E IP Address Experimental Use");
}
}

String networkAddr="";
String lastAddr="";
String[] ipAddrParts=ip.split("\\.");
String[] maskParts=mask.split("\\.");

for(int i=0;i<4;i++){
int x=Integer.parseInt(ipAddrParts[i]);
int y=Integer.parseInt(maskParts[i]);
int z=x&y;
networkAddr+=z+".";
int w=z|(y^255);
lastAddr+=w+".";
}

System.out.println("First IP of block: "+networkAddr);


System.out.println("Last IP of block: "+lastAddr);
}

/*OUTPUT
iotlab@iotlab-Veriton-M200-B360:~$ javac Subnet1.java
iotlab@iotlab-Veriton-M200-B360:~$ java Subnet1
ENTER IP:
226.35.65.23
Class D IP Address Used for multicasting
First IP of block: 226.0.0.0.
Last IP of block: 226.255.255.255.
iotlab@iotlab-Veriton-M200-B360:~$ java Subnet1
ENTER IP:
192.168.100.5
Class C IP Address
SUBNET MASK:
255.255.255.0
First IP of block: 192.168.100.0.
Last IP of block: 192.168.100.255.
iotlab@iotlab-Veriton-M200-B360:~$
*/

You might also like