1
A
回答
1
由於IP地址就是分裂成4個字節的32位數字,你可以簡單地重複一個整數,並使用例如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;
+0
謝謝TLana!順便說一句,你有一些編程技巧! – 2013-03-16 18:59:10
+0
很高興能幫到你! – TLama 2013-03-17 04:04:34
1
我重寫了更多FPC樣式的TLama樣本。請注意,這也應該是安全的:
{$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;
相關問題
- 1. 通過IP地址
- 2. 循環工人通過基於代理的IP地址列表
- 3. 通過netsh爲特定接口循環,並將IP地址存儲到變量
- 4. 通過IP地址加入文件
- 5. 通過純javascript比較IP地址
- 6. 通過ping網關IP地址查找
- 7. WCF通過IIS問題IP地址
- 8. 通過IP地址從bash訪問AWS
- 9. 如何將數據通過IP地址
- 10. 警予accessRules通過IP地址
- 11. 網站IP地址,通過終端
- 12. 的HttpWebRequest通過IP地址(WebProxy)
- 13. Kerberos:通過IP地址訪問主機
- 14. 點對點通過ip地址爲iOS
- 15. 通過java中的IP地址發送HTTP GET請求到目標IP地址
- 16. PASCAL:通過數組循環來創建文件夾?
- 17. ip地址更改通知
- 18. Openstack IP地址過濾器
- 19. 如何通過IP地址知道IP版本
- 20. Pascal功能是循環的
- 21. 在預先寫好的代碼中循環IP地址替換
- 22. 使用for循環創建列表以增加IP地址
- 23. 使用for循環和執行命令Ping多個IP地址
- 24. Unix FOR循環不能與IP地址一起使用
- 25. 如何在python中循環訪問IP地址範圍
- 26. 如何循環遍歷IP地址列表,每行一列Python
- 27. 如何循環訪問IP地址範圍?
- 28. 在for循環中使用Node.js child_process.exec() ping一個IP地址
- 29. 通過地址
- 30. 通過循環
您是否還需要IPv6支持? – Thomas 2013-03-16 03:39:29
@Thomas,只是ipv4。 – 2013-03-16 04:01:09