请教高手,能用Delphi做一个类似Dhcp的动态IP分配的软件吗,他的原理是什么?

解决方案 »

  1.   

    我做过一个类似的,但只是简单的分配c类地址{$R *.DFM}
    procedure TForm3.GetIPMessage(TmpStrA1,TmpStrA2:String);
    var
      A1,B1,C1,A2,B2,C2:string;
    begin
      A1:=Copy(TmpStrA1,1,Pos('.',TmpStrA1)-1);
      TmpStrA1:=Copy(TmpStrA1,Pos('.',TmpStrA1)+1,Length(TmpStrA1)-Pos('.',TmpStrA1));
      B1:=Copy(TmpStrA1,1,Pos('.',TmpStrA1)-1);
      TmpStrA1:=Copy(TmpStrA1,Pos('.',TmpStrA1)+1,Length(TmpStrA1)-Pos('.',TmpStrA1));
      C1:=Copy(TmpStrA1,1,Pos('.',TmpStrA1)-1);
      D1:=Copy(TmpStrA1,Pos('.',TmpStrA1)+1,Length(TmpStrA1)-Pos('.',TmpStrA1));         {获得IP地址段的起始地址的主机ID}
      StrA1:=A1+'.'+B1+'.'+C1+'.';                                    {获得IP地址段的起始地址的网络ID}
      A2:=Copy(TmpStrA2,1,Pos('.',TmpStrA2)-1);
      TmpStrA2:=Copy(TmpStrA2,Pos('.',TmpStrA2)+1,Length(TmpStrA2)-Pos('.',TmpStrA2));
      B2:=Copy(TmpStrA2,1,Pos('.',TmpStrA2)-1);
      TmpStrA2:=Copy(TmpStrA2,Pos('.',TmpStrA2)+1,Length(TmpStrA2)-Pos('.',TmpStrA2));
      C2:=Copy(TmpStrA2,1,Pos('.',TmpStrA2)-1);
      D2:=Copy(TmpStrA2,Pos('.',TmpStrA2)+1,Length(TmpStrA2)-Pos('.',TmpStrA2));     {获得IP地址段的结束地址的主机ID}
      StrA2:=A2+'.'+B2+'.'+C2+'.';                                                                {获得IP地址段的结束地址的网络ID}
    end;procedure TForm3.ssButton3Click(Sender: TObject);       {取消}
    begin
      Close;
    end;procedure TForm3.ssButton1Click(Sender: TObject);        {添加排除范围}
    var
      HostID1,HostID2,HostID3,HostID4,HostID5,HostID6:string;
      TmpStrA,TmpStrB,NetID1,TmpStrC,TmpStrD,TmpStr,TmpStrE,TmpStrF:string;
      i:integer;
    begin
      if (Edit3.Text='') and (Edit4.Text='') then exit;
      if Edit1.Text='' then
      begin
        MessageBox(Form3.Handle,'不存在可排除的有效范围!','提醒',MB_OK);
        exit;
      end;
      if Edit2.Text='' then
      begin
        MessageBox(Form3.Handle,'不存在可排除的有效范围!','提醒',MB_OK);
        exit;
      end;
      if Edit1.Text=Edit2.Text then
      begin
        MessageBox(Form3.Handle,'不存在可排除的有效范围!','提醒',MB_OK);
        exit;
      end;
      TmpStrA:=Edit1.Text;
      TmpStrB:=Edit2.Text;
      GetIPMessage(TmpStrA,TmpStrB);
      If StrA1=StrA2 then
      begin
        NetID1:=StrA1;
        HostID1:=D1;
        HostID2:=D2;
        StrA1:='';StrA2:='';D1:='';D2:='';
      end
      else
      begin
        StrA1:='';StrA2:='';D1:='';D2:='';
        MessageBox(Form2.Handle,'作用域地址范围太大或输入不是有效IP!','提醒',MB_OK);
        exit;
      end;
      TmpStrC:=Edit3.text;
      TmpStrD:=Edit4.text;
      if TmpStrC='' then TmpStrC:=TmpStrD;
      if TmpStrD='' then TmpStrD:=TmpStrC;
      GetIPMessage(TmpStrC,TmpStrD);
      If (StrA1=NetID1) and (StrA2=NetID1) then
      begin
        HostID3:=D1;
        HostID4:=D2;
        StrA1:='';StrA2:='';D1:='';D2:='';
      end
      else
      begin
        StrA1:='';StrA2:='';D1:='';D2:='';
        MessageBox(Form2.Handle,'排除范围越界!','提醒',MB_OK);
        exit;
      end;
      if StrToInt(HostID3)<StrToInt(HostID1) then
      begin
        MessageBox(Form3.Handle,'不存在可排除的有效范围!','提醒',MB_OK);
        exit;
      end;
      if StrToInt(HostID3)>StrToInt(HostID2) then
      begin
        MessageBox(Form3.Handle,'不存在可排除的有效范围!','提醒',MB_OK);
        exit;
      end;
      if StrToInt(HostID4)<StrToInt(HostID1) then
      begin
        MessageBox(Form3.Handle,'不存在可排除的有效范围!','提醒',MB_OK);
        exit;
      end;
      if StrToInt(HostID4)>StrToInt(HostID2) then
      begin
        MessageBox(Form3.Handle,'不存在可排除的有效范围!','提醒',MB_OK);
        exit;
      end;
      if ListBox1.Items.Text <>'' then
      begin
        for i:=0 to ListBox1.Items.Count-1 do
        begin
          TmpStr:=ListBox1.Items.Strings[i];
          TmpStrE:=Copy(TmpStr,1,Pos('到',TmpStr)-1);         {获取已排除地址段的起始IP}
          if TmpStrE='' then TmpStrE:=TmpStr;
          TmpStrF:=Copy(TmpStr,Pos('到',TmpStr)+2,Length(TmpStr)-Pos('到',TmpStr)); {获取已排除地址段的结束IP}
          If TmpStrF='' then TmpStrF:=TmpStr;
          GetIPMessage(TmpStrE,TmpStrF);
          HostID5:=D1;
          HostID6:=D2;
          StrA1:='';StrA2:='';D1:='';D2:='';
          if (StrToInt(HostID3)>=StrToInt(HostID5)) and (StrToInt(HostID3)<=StrToInt(HostID6)) then
          begin
            MessageBox(Form3.Handle,'与当前已排除的范围重叠!','提醒',MB_OK);
            exit;
          end;
          if (StrToInt(HostID4)>=StrToInt(HostID5)) and (StrToInt(HostID4)<=StrToInt(HostID6)) then
          begin
            MessageBox(Form3.Handle,'与当前已排除的范围重叠!','提醒',MB_OK);
            exit;
          end;
          if (StrToInt(HostID5)>=StrToInt(HostID3)) and (StrToInt(HostID5)<=StrToInt(HostID4)) then
          begin
            MessageBox(Form3.Handle,'与当前已排除的范围重叠!','提醒',MB_OK);
            exit;
          end;
        end;
      end;
      if TmpStrD=TmpStrC then
      begin
        ListBox1.Items.Add(TmpStrD);
        Edit3.Text:='';
        Edit4.Text:='';
      end
      else
      begin
        if StrToInt(HostID3)<StrToInt(HostID4) then
        begin
          ListBox1.Items.Add(TmpStrC+'到'+TmpStrD);
          Edit3.Text:='';
          Edit4.Text:='';
        end
        else
          MessageBox(Form3.Handle,'起始IP必须小于结束IP!','提醒',MB_OK);
      end;
    end;procedure TForm3.ssButton2Click(Sender: TObject);  {删除排除范围}
    begin
      ListBox1.Items.Delete(ListBox1.ItemIndex);
    end;procedure TForm3.ssButton4Click(Sender: TObject);
    var
      RCount,j:integer;
      IntIP1,IntIP2,IntIP3,IntIP4,Count,i,n:integer;
      NetID1,IP,A,B,TmpStr:string;
    begin
      if Edit1.Text='' then
      begin
        MessageBox(Form3.Handle,'不存在有效范围!','提醒',MB_OK);
        exit;
      end;
      if Edit2.Text='' then
      begin
        MessageBox(Form3.Handle,'不存在有效范围!','提醒',MB_OK);
        exit;
      end;
      if Edit1.Text=Edit2.Text then
      begin
        MessageBox(Form3.Handle,'不存在有效范围!','提醒',MB_OK);
        exit;
      end;
      GetIPMessage(Edit1.Text,edit2.Text);
      If StrA1=StrA2 then
      begin
        NetID1:=StrA1;
        IntIP1:=StrToInt(D1);
        IntIP2:=StrToInt(D2);
        if not(IntIP1<IntIP2) then
        begin
          MessageBox(Form3.Handle,'不存在有效范围!','提醒',MB_OK);
          exit;
        end;
        StrA1:='';StrA2:='';D1:='';D2:='';
        Count:=IntIP2-IntIP1+1;
        MyList:=TStringList.Create;
        for i:=1 to Count do
        begin
          IP:=NetID1+IntToStr(IntIP1);
          MyList.Add(IP);
          IntIP1:=IntIP1+1;
        end;
      end
      else
      begin
        StrA1:='';StrA2:='';D1:='';D2:='';
        MessageBox(Form3.Handle,'作用域地址范围太大或输入不是有效IP!','提醒',MB_OK);
        exit;
      end;
      if ListBox1.Items.Text <>'' then            {排除}
      begin
        for n:=0 to ListBox1.Items.Count-1 do
        begin
          TmpStr:=ListBox1.Items.Strings[n];
          A:=Copy(TmpStr,1,Pos('到',TmpStr)-1);
          if A='' then A:=TmpStr;
          B:=Copy(TmpStr,Pos('到',TmpStr)+2,Length(TmpStr)-Pos('到',TmpStr));
          if B='' then B:=TmpStr;
          GetIPMessage(A,B);
          IntIP3:=StrToInt(D1);
          IntIP4:=StrToInt(D2);
          RCount:=IntIP4-IntIP3+1;
          for j:=1 to RCount do
          begin
            IP:=StrA1+IntToStr(IntIP3);
            MyList.Delete(mylist.IndexOf(IP));
            IntIP3:=IntIP3+1;
          end;
          StrA1:='';StrA2:='';D1:='';D2:='';
        end;
      end;
      close;
    end;procedure TForm3.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if not(key in['0'..'9',#8,'.']) then
        if word(key)<>vk_back then
        begin
            key:=#0;
        end;
    end;procedure TForm3.Edit2KeyPress(Sender: TObject; var Key: Char);
    begin
      if not(key in['0'..'9',#8,'.']) then
        if word(key)<>vk_back then
        begin
            key:=#0;
        end;
    end;procedure TForm3.Edit3KeyPress(Sender: TObject; var Key: Char);
    begin
      if not(key in['0'..'9',#8,'.']) then
        if word(key)<>vk_back then
        begin
            key:=#0;
        end;
    end;procedure TForm3.Edit4KeyPress(Sender: TObject; var Key: Char);
    begin
      if not(key in['0'..'9',#8,'.']) then
        if word(key)<>vk_back then
        begin
            key:=#0;
        end;
    end;end.