i need to write a C++ or Java program that will receive as input an IP Address in decimal format

192.168.2.125

The program will identify and print the class of that IP according to the first octet. Then it will perform binary bitwise AND operation between the IP address and the corresponding Subnet Mask. In C++ you can use the bitwise “&“ operator to do that. Also you have to represent both the IP Address and the Subnet Mask in binary format. In C++ you have to declare a variable as an unsigned integer.
can u help me with this??

Recommended Answers

All 3 Replies

Although I am by no means a network administrator, this assignment doesn't really seem that overly complex...

Here is how one can determine the IP class:

Class A: Class A addresses are specified to networks with large number of total hosts. Class A allows for 126 networks by using the first octet for the network ID. The first bit in this octet, is always set and fixed to zero. And next seven bits in the octet is all set to one, which then complete network ID. The 24 bits in the remaining octets represent the hosts ID, allowing 126 networks and approximately 17 million hosts per network. Class A network number values begin at 1 and end at 127.

Class B: Class B addresses are specified to medium to large sized of networks. Class B allows for 16,384 networks by using the first two octets for the network ID. The two bits in the first octet are always set and fixed to 1 0. The remaining 6 bits, together with the next octet, complete network ID. The 16 bits in the third and fourth octet represent host ID, allowing for approximately 65,000 hosts per network. Class B network number values begin at 128 and end at 191.

Class C: Class C addresses are used in small local area networks (LANs). Class C allows for approximately 2 million networks by using the first three octets for the network ID. In class C address three bits are always set and fixed to 1 1 0. And in the first three octets 21 bits complete the total network ID. The 8 bits of the last octet represent the host ID allowing for 254 hosts per one network. Class C network number values begin at 192 and end at 223.

So, when we get our IP address, we can extract the first octet and perform tests to see which range it falls in:

if(first_octet >= 1 && first_octet <= 127)
{
     //This test for class A ip.
}
else if(first_octet >= 128 && first_octet <= 191)
{
     //This block for class B ip.
}
else if((first_octet >= 192 &&  <= 223
{
     //This block for class C ip.
}
else
{
     //Class D and E address are reserved, not for public use.
}

With little information, I am going to assume you are going to accept this IP from the user, in which case you will need to prompt the user for information and store it in a way that you can handle with great ease. I will suggest storing each octet seperately so we can later apply our subnet mask.

First, we will have to accept user input into a string:

string ip_address;

cout << "Enter IP address:  ";
cin >> ip_address;

Now get the subnet mask from the user:

string sub_mask;

cout << "Enter subnet mask:  ";
cin >> sub_mask;

Now we will have to extract each octet separately, using the '.' decimal as a delimiter:

string ip_octs[4];
int oct=0;

for(int i=0; i<ip_address.size(); i++)
{
     if(ip_address[i] == '.')
     {
          oct++;
     }
     else
     {
          ip_octs[oct] += ip_address[i];
     }
}

You will also have to extract the subnet mask into it's 4 individual octets also.

Now we need to perform 'string-to-int' converstions:

#include<sstream>

unsigned int ip_octets[4];

for(int i=0; i<4; i++)
{
     istringstream str_to_int(ip_octs[i]);
    
     str_to_int >> ip_octets[i];
}

(Now do the same converstion with your subnet mask)

Now at this point, you have your ip address and your subnet mask isolated and converted into individual octets of type 'unsigned int', you can perform the bitwise AND operation between individual octets:

for(int i=0; i<4; i++)
{
     ip_octets[i] & sub_octets[i] = subnet_ip[i];
}

Now you can display useful information to the user:

cout << "The entered IP address was: "  << ip_address  << endl;
cout << "Which is a class " << ip_class << " address." << endl;
cout << "The entered Subnet Mask was:  "  << sub_mask  << endl;
cout << "The calculated subnet IP is:  "
     << subnet_ip[0] << '.' << subnet_ip[1] << '.' << subnet_ip[2] << '.' << subnet_ip[3];

This is all untested/uncompiled code and is intended as a suggestion only. If anyone sees any errors or knows a better way to do this please let me know.

Is there no api like ntohs ,etc ?

Thanks man very much.i appreciate your help it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.