Sie sind auf Seite 1von 9

COMPUTER NETWORKS

Lab aSSESSMENT-4

NAME : P.JASWANTH

REG.NO : 17MIS0379

SLOT : L59+L60

FACULTY : PROF.USHA DEVI.G


1.Program to determine class, Network and Host
ID of an IPv4 address
Input : 1.4.5.5
Output :
Given IP address belongs to Class A
Network ID is 1
Host ID is 4.5.5
Input : 130.45.151.154
Output :
Given IP address belongs to Class B
Network ID is 130.45
Host ID is 151.154

Code:
import java.io.*;
import java.util.*;
class Networkid
{
static String findClass(String str)
{
int index = str.indexOf('.');
String ipsub = str.substring(0,index);
int ip = Integer.parseInt(ipsub);
if (ip>=1 && ip<=126)
return "A";
else if (ip>=128 && ip<=191)
return "B";
else if (ip>=192 && ip<223)
return "C";
else if (ip >=224 && ip<=239)
return "D";
else
return "E";
}

static void seprate(String str, String ipClass)


{
String network = "", host = "";
if(ipClass == "A")
{
int index = str.indexOf('.');
network = str.substring(0,index);
host = str.substring(index+1,str.length());
}
else if(ipClass == "B")
{
int index = -1;
int dot = 2;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='.')
{
dot -=1;
if(dot==0)
{
index = i;
break;
}
}
}
network = str.substring(0,index);
host = str.substring(index+1,str.length());
}
else if(ipClass == "C")
{
int index = -1;
int dot = 3;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='.')
{
dot -=1;
if(dot==0)
{
index = i;
break;
}
}
}
network = str.substring(0,index);
host = str.substring(index+1,str.length());
}
else if(ipClass == "D" || ipClass == "E")
{
System.out.println("In this Class, IP address"+
" is not divided into Network and Host IDs");
return;
}
System.out.println("Network ID is "+network);
System.out.println("Host ID is "+host);
}
public static void main(String[] args)
{
String str = "144.212.11.11";
String ipClass = findClass(str);
System.out.println("Given IP address belongs to Class "+ipClass);
seprate(str,ipClass);
}
}
Output:

2.Program to implement subnetting and find the subnet masks.


Sample Input and 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 (Network address) is = 100.110.150.8
Last address (Broadcast address) is = 100.110.150.15
Code:
import java.util.Scanner;
class Plant
{
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("\\.");
String split_bip[] = new String[4];
String bip = "";
for(int i=0;i<4;i++)
{
split_bip[i] = appendZeros(Integer.toBinaryString(Integer.parseInt(split_ip[i])));
bip += split_bip[i];
}
System.out.println("IP in binary is "+bip);
System.out.print("Enter the number of addresses: ");
int n = sc.nextInt();
int bits = (int)Math.ceil(Math.log(n)/Math.log(2));
System.out.println("Number of bits required for address = "+bits);
int mask = 32-bits;
System.out.println("The subnet mask is = "+mask);
int fbip[] = new int[32];
for(int i=0; i<32;i++) fbip[i] = (int)bip.charAt(i)-48;
for(int i=31;i>31-bits;i--)
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;
for(int i=31;i>31-bits;i--)
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;
}
}

Output:

Das könnte Ihnen auch gefallen