就是ip的最后一个字段 比如 192.168.1.1  最后的1每次循环自动加1 最简单的编写方法是什么!!  递增后的结果必须是字符串!!

解决方案 »

  1.   

    很简单,你就算好一个长整数,每次变化,将变化的结果转成ip地址就可以了。1)将一个长整数从本机格式转成网络格式,好像是htonl
    2)将长整数转成ip地址用inet_ntoawinsock   现成的   api:   
      inet_addr   
      inet_ntoa   
      注意intel的int,1.2.3.4转换成整数,前面的位数是低位,即   $04030201   
      用ntohl和ntohs转换主机、网络的long和short类型   unit   Unit1;   
        
      interface   
        
      uses   
          Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs,   
          StdCtrls,winsock;   
        
      type   
          TForm1   =   class(TForm)   
              Edit1:   TEdit;   
              Edit2:   TEdit;   
              Button1:   TButton;   
              procedure   Button1Click(Sender:   TObject);   
          private   
              {   Private   declarations   }   
          public   
              {   Public   declarations   }   
          end;   
        
      var   
          Form1:   TForm1;   
        
      implementation   
        
      {$R   *.DFM}   
        
      function   IPStrToInt(s:String):Integer;   
      begin   
          result:=inet_addr(pchar(s));   
      end;   
        
      function   IPIntToStr(I:Integer):String;   
      var   
      sinaddr:in_addr;   
      begin   
          sinaddr.S_addr:=i;   
          result:=String(inet_ntoa(sinaddr));     
      end;   
        
      procedure   TForm1.Button1Click(Sender:   TObject);   
      begin   
      showmessage(inttostr(IPStrToInt('2.1.0.0')));   
      showmessage(IPIntToStr(258));   
      end;   
        
      end.   
      

  2.   

    function PosRight(c:char;str:string):integer;
    var
    i:integer;
    begin
    for i:=length(str) downto 1 do
       begin
       if str[i]=c then
          begin
          result:=i;
          break;
          end;
       end;
    end;procedure IncIP(var IP:string);
    var
    s:string;
    i,j:integer;
    begin
    i:=PosRight('.',s);
    s:=copy(IP,i+1,length(IP)-i-1);
    j:=strtoint(s);
    inc(j);
    Delete(IP,i+1,length(IP)-i-1);
    IP:=IP+inttostr(j);
    end;
      

  3.   

    其实就用一个整数就行了
    i:integer;IP :=IntToStr((i and $FF000000) shr 24)+'.'+
         IntToStr((i and $FF0000) shr 16)+'.'+
         IntToStr((i and $FF00) shr 8)+'.'+
         IntToStr((i and $FF);
    inc(i);
    当最后一位=$FF时,就是广播地址。
      

  4.   

    我也来个uses WinSock;
    递增
    Function IncStrIP(Const IP: String; Const Count: Integer = 1): String;
    Var
      IntIP: Integer;
      StrIP: String;
    Begin
      IntIP := inet_addr(PChar(IP));
      StrIP := IntToHex(IntIP, 8);
      StrIP := Copy(StrIP, 7, 2) + Copy(StrIP, 5, 2) + Copy(StrIP, 3, 2) + Copy(StrIP, 1, 2); //==> C0 A8 00 01
      StrIP := IntToHex(StrToInt(('$' + StrIP)) + Count, 8);
      StrIP := IntToStr(StrToInt('$' + Copy(StrIP, 1, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 3, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 5, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 7, 2)));
      Result := StrIP;
    End;递减
    Function DecStrIP(Const IP: String; Const Count: Integer = 1): String;
    Var
      IntIP: Integer;
      StrIP: String;
    Begin
      IntIP := inet_addr(PChar(IP));
      StrIP := IntToHex(IntIP, 8);
      StrIP := Copy(StrIP, 7, 2) + Copy(StrIP, 5, 2) + Copy(StrIP, 3, 2) + Copy(StrIP, 1, 2); //==> C0 A8 00 01
      StrIP := IntToHex(StrToInt(('$' + StrIP)) - Count, 8);
      StrIP := IntToStr(StrToInt('$' + Copy(StrIP, 1, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 3, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 5, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 7, 2)));
      Result := StrIP;
    End;
      

  5.   

    ‘192.168.1.1’1、针对这个字符串,可以用Pos函数判断第三个‘.’所在的位置,然后用Copy就可以获取出来了。
    2、Str1为“192.168.1.” Str2为1
    3、Str2转换成strtoint(str2),Integer型,inc自动加一就可以了。
    4、最后合并str1+str2就是最后要的结果了。