I need to write a program that parses an ip address and subnet mask and prints out the associated network id and host id.

The program should accept the ip address and subnet mask as input parameters, e.g.,

ipcalc 192.168.1.129 255.255.255.240

The output should display the class of the address, along with the network and host id, e.g.,

Class C Address
Network ID: 192.168.1.128
Host ID : 0.0.0.1

Can someone please help me solving this question.

Thanks!

Which do you want, a C solution or C++? And what have you tried ?

Which do you want, a C solution or C++? And what have you tried ?

i have tried this but not coming..
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int ip[4];
int mask[4];
if (argc != 3) {
fprintf(stderr, "usage: ipcalc <ipaddr> <mask>\n");
exit(1);
}
sscanf(argv[1], "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
sscanf(argv[2], "%d.%d.%d.%d", &mask[0], &mask[1], &mask[2], &mask[3]);

if (mask[0] == 255) {
if (mask[1] == 255) {
if (mask[2] == 255) {
printf("Class C address\n");
} else {
printf("Class B address\n");
}
} else {
printf("Class A address\n");
}
} else {
printf("unknown ip mask class\n");
}

printf("Network ID: %d.%d.%d.%d\n", ip[0] & mask[0], ip[1] & mask[1],
ip[2] & mask[2], ip[3] & mask[4]);
printf("Host ID: %d.%d.%d.%d\n", ip[0] & ~mask[0], ip[1] & ~mask[1],
ip[2] & ~mask[2], ip[3] & ~mask[4]);
getch();
//exit(0);
}

Please edit your post and use code tags

Develop a program that parses an ip address and subnet mask and prints
out the associated network id and host id.

The program can be command line based and should accept the ip address
and subnet mask as input parameters, e.g.,

ipcalc.pl 192.168.1.129 255.255.255.240

The output should display the class of the address, along with the
network and host id, e.g.,

Class C Address
Network ID: 192.168.1.128
Host ID : 0.0.0.1

If no parameters are supplied, the program should print out a usage
message:

usage: ipcalc.pl <ip address> <subnet mask>

The same message should also be printed if an incorrect number of
parameters is supplied.

The format of the ip address and subnet mask should be
validated. Otherwise an error message should be generated.

For example,

ipcalc.pl 192.168.1.1000 255.255.255.0
Invalid IP Address entered

ipcalc.pl 192.168.1.1 255.255.255a
Invalid Subnet Mask entered

If a Class D or E address is entered the program should print:

Class D (Multicast) Address
Class E (Experimental) Address

please send me in c or c++ i have put the actual questioning

I meant for you to correct the code in your post #3, not to mearly restate the program requirements. The code you posted is nearly unreadable. Fix it up with proper intentations (spacing).

>>please send me in c or c++
No. You have to develop the program yourself.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main(int argc, char **argv)
 {
   int ip[4];
   int mask[4];
   if (argc != 3) 
    {
      fprintf(stderr, "usage: ipcalc <ipaddr> <mask>\n");
      exit(1);
    }
      sscanf(argv[1], "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
      sscanf(argv[2], "%d.%d.%d.%d", &mask[0], &mask[1], &mask[2], &mask[3]);

       if (mask[0] == 255) 
        {
          if (mask[1] == 255) 
            {
              if (mask[2] == 255)
                {
                  printf("Class C address\n");
                } 
              else 
                {
                  printf("Class B address\n");
                }
            } 
          else  
           {
             printf("Class A address\n");
           }
        } 
       else 
        {
            printf("unknown ip mask class\n");
        }

           printf("Network ID: %d.%d.%d.%d\n", ip[0] & mask[0], ip[1] & mask[1],
           ip[2] & mask[2], ip[3] & mask[4]);
           printf("Host ID: %d.%d.%d.%d\n", ip[0] & ~mask[0], ip[1] & ~mask[1],
           ip[2] & ~mask[2], ip[3] & ~mask[4]);

  }
commented: No code tags (again) -7

You failed to use code tags again.

When the program is given a multicast address it should
print the corresponding mac address, along with a list of the 32
overlapped multicast ip addresses.

./ipcalc.pl 224.1.1.1 255.255.255.255
Class D (Multicast) Address: 01-00-5e-01-01-01
Overlapped Addresses
224.1.1.1
224.129.1.1
225.1.1.1
225.129.1.1
226.1.1.1
226.129.1.1
227.1.1.1
227.129.1.1
228.1.1.1
228.129.1.1
229.1.1.1
229.129.1.1
230.1.1.1
230.129.1.1
231.1.1.1
231.129.1.1
232.1.1.1
232.129.1.1
233.1.1.1
233.129.1.1
234.1.1.1
234.129.1.1
235.1.1.1
235.129.1.1
236.1.1.1
236.129.1.1
237.1.1.1
237.129.1.1
238.1.1.1
238.129.1.1
239.1.1.1
239.129.1.1


please help me writing this prog

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.