I need to write code that can determine if an IP address is in a subnet based one the subnet address and mask.

I don't even know where to begin with this, so any tips would be appreciated.


Recommended Answers

All 2 Replies

Okay, how about this.

Make a function that accepts an IP Address, and the Subnet (Subnet Address and Mask) and have it return a bool. It returns true if the IP Address exists in the subnet, or false if it doesn't.

bool ValidIP(const string& ipAddress, const string& subnetAddress, const string& mask){

// ...
   bool validIPAddress = false;
// ...

   return validIPAddress;


... Or are you asking about the algorithm to determine whether an IP Adress exists in the subnet or not?

Also, the following source(s) may help:





No, I need code to be able to check if a given IP address is within a certain network. Or, what the network address for a given IP is. Either way.

Be a part of the DaniWeb community

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