开发者

how to compare two IP belongs to same Class in MFC(VC++)?

how to compare two IP belongs to same Class(CLASS A,B,C,D) in MFC(VC++) ? I have one IP as 172.24.17.174 and other one as 172开发者_如何学运维.24.17.86 now i can compare whether they lie in same class or Not ?

Any code is highly appreciated. Thanks


IP address classes aren't used anymore and I don't think there is any built-in function to determine this. You can always write your own function to determine the class of an address (based on the definition of address classes):

enum IPClass { ClassA, ClassB, ClassC, ClassD, ClassE };

IPClass getClass(unsigned int addr) {
  char msb = char(addr >> 24);
  if (msb >> 7 == 0x0)
    return ClassA;
  if (msb >> 6 == 0x2)
    return ClassB;
  if (msb >> 5 == 0x6)
    return ClassC;
  if (msb >> 4 == 0xE)
    return ClassD;
  return ClassE;
}


First, keep in mind that the whole idea of class A, B, C and D nets has really been obsolete for quite a while. It was replaced with variable-length subnet masks something like 15-20 years ago (don't remember exactly, but early '90s).

In any case, the first bits of the address tell you the class -- 0 in the most significant bit means class A. 10 in the two most significant bits means Class B. 110 in the three most significant bits means class C. I believe anything else is class D.

As I said, however, the whole "class" system for IP addresses is obsolete, so nearly anything you could hope to accomplish with it probably needs to be done some other way to produce really meaningful results.


To check if 2 address are in the same network:

int result1 = ipaddress1 & subnetmask;
int result2 = ipaddress2 & subnetmask;
if(result1==result2)
{
    cout << "Both in Same network";
}
else
{
    cout << "not in same network";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜