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
• 11,450 points
233 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
• 12,880 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
• 11,450 points
390 views
+1 vote
1 answer

How to find IP address of nodes in my network?

The IP address of the nodes connected ...READ MORE

answered Feb 9, 2019 in Cyber Security & Ethical Hacking by Omkar
• 69,190 points
2,150 views
0 votes
0 answers

I do not have a real result about encryption file use AES mode CFB in pycrptodome on python

I'm using Ubuntu win. on python I ...READ MORE

Aug 23, 2019 in Cyber Security & Ethical Hacking by Ahmed
• 310 points

closed Aug 23, 2019 by Ahmed 494 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
1,485 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,700 points
209 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
• 12,880 points
211 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,050 points
70 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
• 12,880 points
15,635 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
• 12,880 points
604 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