Приветствую Вас Гость | RSS

Delphi заготовки

Понедельник, 29.04.2024, 19:43
Главная » 2012 » Май » 3 » Delphi. Модуль генерации IP адресов из диапазона
14:07
Delphi. Модуль генерации IP адресов из диапазона

  // проверка IP адреса на корректность
  Function GetIP4ToInt(s:string;var Value:int64):boolean;
  Function GetIP4FromInt(Value:integer):string;
  // получить список IP адресов между adres1,adres2
  Function GetListIp(adres1,adres2:string):TstringList;

// проверка IP адреса
Function GetIP4ToInt(s:string;var Value:int64):boolean;
var b1,b2,b3,b4:int64;
    x:integer;
    s2:string;
begin

result:=false;
try
  b1:=-1;
  b2:=-1;
  b3:=-1;
  b4:=-1;
    for x:=1 to length(s) do begin
    if (s[x]='.') or (x=length(s)) then begin
    if x=length(s) then s2:=s2+s[x];
    if b1=-1 then b1:=strtoint(s2)
    else if b2=-1 then
    b2:=strtoint(s2)
    else if b3=-1 then
      b3:=strtoint(s2)
    else if b4=-1 then
      b4:=strtoint(s2);
    s2:='';
    end else s2:=s2+s[x];
  end;
  Value:=b1*256*256*256+b2*256*256+b3*256+b4;
  except
    result:=false;
  end;
  if (b1>-1) and (b2>-1) and (b3>-1) and (b4>0) and
  (b1<256) and (b2<256) and (b3<256) and (b4<256) then
  result:=true;
end;

// Получить IP адрес из Integer значения
Function GetIP4FromInt(Value:integer):string;
var p1,p2,p3,p4:LongInt;
    s:string;
begin
     s:=inttohex(Value,8);
     p1:=strtoint('$'+copy(s,1,2));
     p2:=strtoint('$'+copy(s,3,2));
     p3:=strtoint('$'+copy(s,5,2));
     p4:=strtoint('$'+copy(s,7,2));
     result:=inttostr(p1)+'.'+inttostr(p2)+'.'+inttostr(p3)+'.'+inttostr(p4);
end;



// получение списка IP адресов


Function GetListIp(adres1,adres2:string):TstringList;
 var b1,b2,b3,b4:integer;
     e1,e2,e3,e4:integer;
     p1,p2,p3,p4:integer;
     x:Int64;
     s2,s:string;
     countb,counte:Int64;
begin
  result:=TstringList.Create;
  s:=adres1;
  s2:='';
  b1:=-1;e1:=-1;
  b2:=-1;e2:=-1;
  b3:=-1;e3:=-1;
  b4:=-1;e4:=-1;
  if GetIP4ToInt(adres1,countb) and GetIP4ToInt(adres2,counte) then begin
  X:=countb;
  while x<counte+1 do begin
     application.ProcessMessages;
     s:=inttohex(x,8);
     p1:=strtoint('$'+copy(s,1,2));
     p2:=strtoint('$'+copy(s,3,2));
     p3:=strtoint('$'+copy(s,5,2));
     p4:=strtoint('$'+copy(s,7,2));
     if p4=0 then begin p4:=1; x:=x+1; end;
    result.Add(inttostr(p1)+'.'+inttostr(p2)+'.'+inttostr(p3)+'.'+inttostr(p4));
    x:=x+1;
  end;
    // result.Add(inttostr(p1)+'.'+inttostr(p2)+'.'+inttostr(p3)+'.'+inttostr(p4))
 end else raise exception.Create('Ошибка ввода IP адреса ');

end;





Пример получения текущего  IP адреса
http://netsoftware.ucoz.ru/news/skachat_fajl_iz_interneta_v_peremennuju/2012-04-12-31

Просмотров: 2913 | Добавил: NetSoftWare | Рейтинг: 5.0/1
Всего комментариев: 1
1 NetSoftWare  
0

Имя *:
Email *:
Код *: