В Freepascal, как я могу пройти через ряд IP-адресов?loop through ip address pascal
Любые юниты, которые делают ip специфические вещи, которые могут справиться с этим? Я пробовал один вызов inetaux, но он испорчен и не работает.
В Freepascal, как я могу пройти через ряд IP-адресов?loop through ip address pascal
Любые юниты, которые делают ip специфические вещи, которые могут справиться с этим? Я пробовал один вызов inetaux, но он испорчен и не работает.
Как IP-адрес только 32-битное число разбивается на 4 байта, вы можете просто перебирать целое и использовать, например, absolute
директиву, чтобы разделить этот итератор в 4 байта:
type
TIPAddress = array[0..3] of Byte;
procedure TForm1.Button1Click(Sender: TObject);
var
S: string;
I: Integer;
IPAddress: TIPAddress absolute I;
begin
// loop in some range of IP addresses, here e.g. from 127.0.0.1 to 127.0.1.245
for I := 2130706433 to 2130706933 do
begin
// now you can build from that byte array e.g. well known IP address string
S := IntToStr(IPAddress[3]) + '.' + IntToStr(IPAddress[2]) + '.' +
IntToStr(IPAddress[1]) + '.' + IntToStr(IPAddress[0]);
// and do whatever you want with it...
end;
end;
Или вы можете делать то же самое с побитовым оператором сдвига, для чего требуется немного больше работы. Например, тот же пример, что и выше, будет выглядеть так:
type
TIPAddress = array[0..3] of Byte;
procedure TForm1.Button1Click(Sender: TObject);
var
S: string;
I: Integer;
IPAddress: TIPAddress;
begin
// loop in some range of IP addresses, here e.g. from 127.0.0.1 to 127.0.1.245
for I := 2130706433 to 2130706933 do
begin
// fill the array of bytes by bitwise shifting of the iterator
IPAddress[0] := Byte(I);
IPAddress[1] := Byte(I shr 8);
IPAddress[2] := Byte(I shr 16);
IPAddress[3] := Byte(I shr 24);
// now you can build from that byte array e.g. well known IP address string
S := IntToStr(IPAddress[3]) + '.' + IntToStr(IPAddress[2]) + '.' +
IntToStr(IPAddress[1]) + '.' + IntToStr(IPAddress[0]);
// and do whatever you want with it...
end;
end;
Спасибо, TLana! Кстати, у вас есть навыки программирования! –
Рад, что я могу помочь! – TLama
Я переписал образец TLama в более стиле FPC. Обратите внимание, что это также должно быть безопасным для конечных пользователей:
{$mode Delphi}
uses sockets;
procedure Button1Click(Sender: TObject);
var
S: string;
I: Integer;
IPAddress: in_addr;
begin
// loop in some range of IP addresses, here e.g. from 127.0.0.1 to 127.0.1.24
for I := 2130706433 to 2130706933 do
begin
IPAddress.s_addr:=i;
s:=HostAddrToStr(IPAddress);
....
end;
end;
Нужна ли вам поддержка IPv6 или просто IPv4? – Thomas
@ Томас, просто ipv4. –