Delphi 计算IP地址范围


本文整理自网络,侵删。

 
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses Winsock;

type
  IPv4 = record
   Octet: array [1..4] of Byte ;
 end;

 const
 LISTESIRA     = 0;
 LISTEIP       = 1;
 LISTEMAC      = 2;
 LISTEPROTOKOL = 3;
 LISTEPORT     = 4;

 OKTET1  = 1;
 OKTET2  = 2;
 OKTET3  = 3;
 OKTET4  = 4;

var

 BaslangicIP : IPv4;
 BitisIP     : IPv4;

function Ip2Int(i1,i2,i3,i4:byte):integer;
begin
 result := (i1 shl 24) + (i2 shl 16) + (i3 shl 8) + (i4);
end;

function Int2Ip(ip:integer):string;
var
 dizi:array[1..4] of byte absolute ip;
begin

 if ((Dizi[4] = 0) or (Dizi[4] = 255)  or
     (Dizi[3] = 0) or (Dizi[3] = 255)  or
     (Dizi[2] = 0) or (Dizi[2] = 255)  or
     (Dizi[1] = 0) or (Dizi[1] = 255)) then
 begin
   result := '' ;
 end
  else
 begin
 result := dizi[4].ToString + '.' +
           dizi[3].ToString + '.' +
           dizi[2].ToString + '.' +
           dizi[1].ToString;

 end;
end;

procedure ListeEkle(var Liste:TStringGrid;pip,pmac,pprotokol,pport:string);
var
 eklenensatir : integer;
begin
 Liste.RowCount := Liste.RowCount + 1;
 eklenensatir := Liste.RowCount-1;

 Liste.Cells[LISTESIRA,eklenensatir]     := inttostr(eklenensatir);
 Liste.Cells[LISTEIP,eklenensatir]       := trim(pip);
 Liste.Cells[LISTEMAC,eklenensatir]      := trim(pmac);
 Liste.Cells[LISTEPROTOKOL,eklenensatir] := trim(pprotokol);
 Liste.Cells[LISTEPORT,eklenensatir]     := trim(pport);
 Liste.Row := eklenensatir;

end;

procedure IPListOlustur(var Baslama,bitis:IPv4;var Liste:TStringGrid);
var
IPilk  : integer;
IPSon  : integer;
sira   : integer;
IPADDR : String;
 begin

 IPilk := Ip2Int(Baslama.Octet[Oktet1],Baslama.Octet[Oktet2],
                 Baslama.Octet[Oktet3],Baslama.Octet[Oktet4]);

 IPSon := Ip2Int(Bitis.Octet[Oktet1],Bitis.Octet[Oktet2],
                 Bitis.Octet[Oktet3],Bitis.Octet[Oktet4]);

 for sira := IPilk to IPSon do
  begin
    IPADDR := Int2Ip(sira);
    if (IPADDR <> '') then
     ListeEkle(Liste,IPADDR,'','','');
     Application.ProcessMessages;
  end;

end;



procedure TForm1.FormCreate(Sender: TObject);
var
IPList:string;
begin
//caption:=IntToStr(Ip2Int(127,0,0,1));
IPListOlustur(BaslangicIP,BitisIP,StringGrid1);
end;

end.

相关阅读 >>

Delphi10.3.1安卓照相

Delphi 简单的旋转图像角度代码

Delphi 如何将access的ole对象字段存储的bmp图象显示出来

Delphi 实现文件上传下载

Delphi代码获取网卡物理地址三种方法

Delphi webbrowser设置自己定义user-agent

Delphi启动屏幕保护

Delphi中sendmessage给一个edit控件发送wm_settext消息

Delphi 检测文件数字签名

Delphi在richedit控件中插入gif动画表情

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...