如何判断所输入的字符是本机的IP或计算机名?

解决方案 »

  1.   

    function IsIP(CheckIP:string):boolean;
    var tempIP,tempString:string;
        IPRec:Integer;
    begin
        try
            tempIP:=CheckIP;//判断用户输入的IP地址的格式是否正确
            if pos('.',tempIP)=0 then
            result:=False                       //如果字符串中没有.隔开
            else
            begin
                    tempString:=leftStr(tempIP,pos('.',tempIP)-1);  // 取得字符串的第一个.前面的数字
                    IPRec:=StrToInt(tempString);
                    if not ((IPRec>=0) and (IPRec<256)) then        // 如果第一个数字超出ip数字的范围
                    result:=False
                    else
                    begin
                            tempIP:=rightStr(tempIP,length(tempIP)-pos('.',tempIP));  // 更新字符串
                            if pos('.',tempIP)=0 then
                            result:=False
                            else
                            begin
                                    tempString:=leftStr(tempIP,Pos('.',tempIP)-1);
                                    IPRec:=StrToInt(tempString);        //取得第二个数字
                                    if (IPRec<0) or (IPRec>255) then
                                    result:=False
                                    else
                                    begin
                                            tempIP:=rightStr(tempIP,length(tempIP)-Pos('.',tempIP));
                                            if Pos('.',tempIP)=0 then
                                            result:=False
                                            else
                                            begin
                                                    tempString:=leftStr(tempIP,Pos('.',tempIP)-1);
                                                    IPRec:=StrToInt(tempString);
                                                    if (IPRec<0) or (IPRec>255) then
                                                    result:=False
                                                    else
                                                    begin
                                                            tempIP:=rightStr(tempIP,length(tempIP)-Pos('.',tempIP));
                                                            IPRec:=StrToInt(tempIP);
                                                            if (IPRec<0) or (IPRec>255) then
                                                            result:=False
                                                            else
                                                            result:=True;
                                                    end;
                                            end;
                                    end;
                            end;
                    end;
            end;
        except
            result:=False;
        end;
    end;
      

  2.   

    二楼的搞的太复杂了,既然楼主要的是本机的IP和计算机名;
    楼主可以取本机IP和你输入的IP进行比较,取本机IP你应该会吧,直接比较就行了;
    不要判断有没有点了什么的,那是程序内部自己比较的,不用你操心!
      

  3.   

    问题解决了,这是我刚找到的,谢谢大家的支持!uses winsock;function MyForm.NetGetLocalIP(var LocalIp,LocalHostName: string): Boolean;
    var
        HostEnt: PHostEnt;
        Ip: string;
        addr: pchar;
        strName: string;
        Buffer: array [0..63] of char;
        GInitData: TWSADATA;
    begin
      Result := False;
      try
        WSAStartup(2, GInitData);
        GetHostName(Buffer, SizeOf(Buffer));
        HostEnt := GetHostByName(buffer);    if HostEnt = nil then Exit;
        LocalHostName:=StrPas(HostEnt^.h_name);
        addr := HostEnt^.h_addr_list^;
        ip := Format('%d.%d.%d.%d', [byte(addr [0]),
              byte (addr [1]), byte (addr [2]), byte (addr [3])]);
        LocalIp := Ip;
        Result := True;
      finally
        WSACleanup;
      end;
    end;