//是否是ip地址
int MainWindow::is_ip_address(const char *strip)
{
int section = 0;
int dot = 0;
int i = 0;
int len = strlen(strip);
if(len > 15)
{
return 0;
}
while (*strip) {
i++;
if(*strip == '.')
{
dot++;
if(dot > 3)
{
return 0;
}
if(section >= 0 && section <= 255 && i != 1)
{
section = 0;
}
else
{
return 0;
}
}
else if(*strip >= '0' && *strip <= '9')
{
section = section * 10 + *strip - '0';
}
else
{
return 0;
}
strip++;
}
if(section >= 0 && section <= 255)
{
if(dot == 3)
{
return 1;
}
}
return 0;
}
//ip地址是否合法
int MainWindow::is_ip_legal(QString &str)
{
char* strip = nullptr;
QByteArray ba = str.toLatin1(); // must
strip=ba.data();
unsigned int n1;
unsigned int n2;
unsigned int n3;
unsigned int n4;
if(!MainWindow::is_ip_address(strip))
{
return 0;
}
if(sscanf(strip, "%d.%d.%d.%d", &n1,&n2,&n3,&n4) != 4)
{
return 0;
}
if((n1 > 0 && n1< 127) || (n1 >= 128 && n1 <= 223))
{
return 1;
}
return 0;
}
c语言检测ip是否合法
最新推荐文章于 2024-03-20 17:39:53 发布