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,690 points
890 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,620 points

Related Questions In Cyber Security & Ethical Hacking

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,690 points
1,443 views
0 votes
0 answers

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

How do I find and exploit an ...READ MORE

Oct 14 in Cyber Security & Ethical Hacking by Anupam
• 3,890 points
59 views
0 votes
1 answer
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
2,203 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,710 points
772 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,620 points
870 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
393 views
0 votes
1 answer

how to find ip address in cmd

1. Open the Command Prompt.   a. Click the Start icon, ...READ MORE

answered Feb 9, 2022 in Cyber Security & Ethical Hacking by Edureka
• 13,620 points
47,514 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,620 points
2,719 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