How do i check a ip address range whether it falls in Class A Class B Class C

0 votes
import java.net.*;
import java.io.*;

public class ip_host {
    public static void main(String args[]) throws Exception {
        System.out.println("Enter the host name :");
        String n = new DataInputStream(System.in).readLine();

        InetAddress ipadd = InetAddress.getByName(n);

        System.out.println("IP address :" + ipadd);
    }

I have this program which finds the IP address. i want this to check whether it falls in Class A,Class B,Class C

Feb 16, 2022 in Cyber Security & Ethical Hacking by Edureka
• 12,730 points
• 2,175 views

1 answer to this question.

0 votes
class NetworkId{

static String findClass(String str){

int index = str.indexOf('.');

String ipsub = str.substring(0,index);

int ip = Integer.parseInt(ipsub);

// Class A

if (ip>=1 && ip<=126)

return "A";

// Class B

else if (ip>=128 && ip<=191)

return "B";

// Class C

else if (ip>=192 && ip<223)

return "C";

// Class D

else if (ip >=224 && ip<=239)

return "D";

// Class E

else

return "E";

}

static void separate(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 = "192.226.12.11";

String ipClass = findClass(str);

System.out.println("Given IP address belings to Class "+ipClass);

separate(str,ipClass);

}

}
answered Feb 16, 2022 by Edureka
• 13,800 points

Related Questions In Cyber Security & Ethical Hacking

0 votes
1 answer

How do I check if a port is open in Nmap?

To check if a specific port is ...READ MORE

answered Jan 8, 2025 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 31,220 points
• 1,015 views
0 votes
1 answer
0 votes
1 answer

How can I force the login to a specific ip address?

Try to access the router's default page. It's ...READ MORE

answered Feb 15, 2022 in Cyber Security & Ethical Hacking by Edureka
• 12,730 points
• 2,967 views
+1 vote
1 answer

How do I find and exploit an insecure API endpoint in a mobile app?

In order to locate and test insecure ...READ MORE

answered Oct 24, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 31,220 points
• 1,743 views
0 votes
1 answer

Should a class file always be inside package for importing it in jsp page?

Hey there! It is not necessary for the ...READ MORE

answered May 24, 2019 in Java by Karan
• 3,300 views
0 votes
1 answer

no 'access-control-allow-origin' header is present on the requested resource

The java <class-name> command syntax is one ...READ MORE

answered Feb 8, 2022 in Java by Soham
• 9,730 points
• 1,955 views
0 votes
0 answers

Track the exact location of IP address

I looked into IP addresses and a ...READ MORE

Feb 14, 2022 in Cyber Security & Ethical Hacking by Edureka
• 13,800 points
• 1,734 views
0 votes
0 answers

Can an outer class access the members of inner class?

If the inner class is defined as ...READ MORE

May 9, 2022 in Java by Kichu
• 19,040 points
• 1,444 views
0 votes
1 answer

how to find ip address in cmd

if you are using windows then there ...READ MORE

answered Feb 26, 2023 in Cyber Security & Ethical Hacking by subhasish

edited Mar 5, 2025 • 51,621 views
0 votes
2 answers

IP Address Class C network and its gateway

The class C network has less than ...READ MORE

answered Feb 21, 2022 in Cyber Security & Ethical Hacking by Edureka
• 13,800 points
• 4,612 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP