C++ 判断IP是否属于局域网IP_哈士奇超帅的博客-CSDN博客
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
bool isLAN(const string& ip);
int main()
{
cout<<isLAN("192.168.1.1");
cout<<isLAN("10.1.1.1");
cout<<isLAN("172.18.2.2");
cout<<isLAN("3.3.3.3");
return 0;
}
/*-----------------------------------------
局域网IP地址范围
A类:10.0.0.0-10.255.255.255
B类:172.16.0.0-172.31.255.255
C类:192.168.0.0-192.168.255.255
-------------------------------------------*/
bool isLAN(const string& ipstring)
{
istringstream st(ipstring);
int ip[2];
for(int i = 0; i < 2; i++)
{
string temp;
getline(st,temp,'.');
istringstream a(temp);
a >> ip[i];
}
if((ip[0]==10) || (ip[0]==172 && ip[1]>=16 && ip[1]<=31) || (ip[0]==192 && ip[1]==168))
return true;
else return false;
}