判断用户输入的是否是合理ip地址的函数: function IsLegalIp(Str: string): Boolean; var I, K, DotCnt : Integer; Num: string; Arr: Array [1..4] of string; begin Result := False; DotCnt := 0; //由'0'..'9', '.'组成 For I := 1 to Length(Str) do begin if Not (Str[I] in ['0'..'9', '.']) then Exit else if Str[I] = '.' then inc(DotCnt); end; //点分隔符号数量应该=3 if DotCnt <> 3 then Exit; For K := 1 to 3 do begin I := Pos('.', Str); Num := Copy(Str, 1, I - 1); Delete(Str, 1, I); Arr[K] := Num; end; Arr[4] := Str; try DotCnt := 0; For I := 1 to 4 do begin K := StrToInt(Arr[I]); //每字节的值应介于0~255之间 if ((K >= 0) and (K <= 255)) then Inc(DotCnt); end; //四个字节都满足0~255之间,则合法 if(DotCnt = 4) then Result := True; except end; end;
function IsValidIP(astrIP:String):Boolean; var iCount:Integer; iIPLength:Integer; iFieldLength:Integer; iFieldStart:Integer; iDotCount:Integer; strTemp:String; begin iIPLength:=Length(astrIP); if (iIPLength>15)or(iIPLength<7)then begin Result:=False;//合法IPv4长度在7和15之间 Exit; end; iDotCount:=0; iFieldLength:=0; iFieldStart:=1; for iCount:=1 to iIPLength do begin case astrIP[iCount] of "0","1","2","3","4","5","6","7","8","9": begin iFieldLength:=iFieldLength+1; if (3<iFieldLength) then begin Result:=False;//IP的单域长度超过3 Exit; end; end; "." begin if 0=iFieldLength then begin Result:=False;//"."在开头或连续两个".",或在结尾 Exit; end; strTemp:=copy(astrIp,iFieldStart,iCount-iFieldStart); if(255<StrToInt(strTemp))then begin Result:=False;//单域值>255 Exit; end; iDotCount:=iDotCount+1; if 3<iDotCount then begin Result:=False;//超过4个域 Exit; end; iFieldLength:=0; iFieldStart:=iCount+1; end; else begin Result:=False;//非法字符 Exit; end; end; Result:=True; end;
/* 判断IP地址是否有效,如果无效返回0,有效返回1 WSY 2004-9-17 16:57 */ int isValidIP(char * sIP) { short i,j,l,da[3]={0,0,0}; char str[16 + 1]; l = strlen(sIP); if (l<7) return 0; j=0; for (i=0;i<l ;i++ ) { if ( *(sIP+i) =='.') { if (j<3) { da[j] = i; j++; } else {return 0;} } else if(isdigit(*(sIP+i))==0) { return 0; } } if (j!=3) { return 0; } memset(str, 0x00, sizeof(str)); strncpy(str,sIP,da[0] - 1); if (atoi(str)>255) { return 0; } memset(str, 0x00, sizeof(str)); strncpy(str,sIP + da[0] + 1, da[1]-da[0] -1); if (atoi(str)>255) { return 0; } memset(str, 0x00, sizeof(str)); strncpy(str,sIP + da[1] + 1, da[2]-da[1] -1); if (atoi(str)>255) { return 0; } memset(str, 0x00, sizeof(str)); strncpy(str,sIP + da[2] + 1, l - da[2] - 1); if (atoi(str)>255) { return 0; } return 1; }
Function IsValidIp(Const ip:String):Boolean; begin Result:=inet_addr(PChar(ip))<>INFINITE; end;
每个xxx为数字,不大于255的数字。
function IsLegalIp(Str: string): Boolean;
var
I, K, DotCnt : Integer;
Num: string;
Arr: Array [1..4] of string;
begin
Result := False;
DotCnt := 0;
//由'0'..'9', '.'组成
For I := 1 to Length(Str) do
begin
if Not (Str[I] in ['0'..'9', '.']) then
Exit
else
if Str[I] = '.' then
inc(DotCnt);
end;
//点分隔符号数量应该=3
if DotCnt <> 3 then Exit;
For K := 1 to 3 do
begin
I := Pos('.', Str);
Num := Copy(Str, 1, I - 1);
Delete(Str, 1, I);
Arr[K] := Num;
end;
Arr[4] := Str;
try
DotCnt := 0;
For I := 1 to 4 do
begin
K := StrToInt(Arr[I]);
//每字节的值应介于0~255之间
if ((K >= 0) and (K <= 255)) then
Inc(DotCnt);
end;
//四个字节都满足0~255之间,则合法
if(DotCnt = 4) then
Result := True;
except
end;
end;
var
iCount:Integer;
iIPLength:Integer;
iFieldLength:Integer;
iFieldStart:Integer;
iDotCount:Integer;
strTemp:String;
begin
iIPLength:=Length(astrIP);
if (iIPLength>15)or(iIPLength<7)then
begin
Result:=False;//合法IPv4长度在7和15之间
Exit;
end; iDotCount:=0;
iFieldLength:=0;
iFieldStart:=1;
for iCount:=1 to iIPLength do
begin
case astrIP[iCount] of
"0","1","2","3","4","5","6","7","8","9":
begin
iFieldLength:=iFieldLength+1;
if (3<iFieldLength) then
begin
Result:=False;//IP的单域长度超过3
Exit;
end;
end;
"."
begin
if 0=iFieldLength then
begin
Result:=False;//"."在开头或连续两个".",或在结尾
Exit;
end;
strTemp:=copy(astrIp,iFieldStart,iCount-iFieldStart);
if(255<StrToInt(strTemp))then
begin
Result:=False;//单域值>255
Exit;
end;
iDotCount:=iDotCount+1;
if 3<iDotCount then
begin
Result:=False;//超过4个域
Exit;
end;
iFieldLength:=0;
iFieldStart:=iCount+1;
end;
else
begin
Result:=False;//非法字符
Exit;
end;
end; Result:=True;
end;
判断IP地址是否有效,如果无效返回0,有效返回1
WSY 2004-9-17 16:57
*/
int isValidIP(char * sIP)
{
short i,j,l,da[3]={0,0,0};
char str[16 + 1];
l = strlen(sIP);
if (l<7) return 0;
j=0;
for (i=0;i<l ;i++ )
{
if ( *(sIP+i) =='.')
{
if (j<3)
{
da[j] = i;
j++;
}
else
{return 0;}
}
else if(isdigit(*(sIP+i))==0)
{
return 0;
}
}
if (j!=3)
{
return 0;
}
memset(str, 0x00, sizeof(str));
strncpy(str,sIP,da[0] - 1);
if (atoi(str)>255)
{
return 0;
}
memset(str, 0x00, sizeof(str));
strncpy(str,sIP + da[0] + 1, da[1]-da[0] -1);
if (atoi(str)>255)
{
return 0;
}
memset(str, 0x00, sizeof(str));
strncpy(str,sIP + da[1] + 1, da[2]-da[1] -1);
if (atoi(str)>255)
{
return 0;
}
memset(str, 0x00, sizeof(str));
strncpy(str,sIP + da[2] + 1, l - da[2] - 1);
if (atoi(str)>255)
{
return 0;
}
return 1;
}
begin
Result:=inet_addr(PChar(ip))<>INFINITE;
end;