为什么检测端口的程序运行一段时间程序就出错? ( 积分: 100 )

  • 主题发起人 主题发起人 lwgsyd
  • 开始时间 开始时间
L

lwgsyd

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是代码

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, inifiles, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, ComCtrls, StrUtils, bsSkinData,
BusinessSkinForm, ExtCtrls, bsSkinCtrls, Mask, bsSkinBoxCtrls;

type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Edit2: TEdit;
Edit1: TEdit;
Timer1: TTimer;
bsBusinessSkinForm1: TbsBusinessSkinForm;
bsSkinData1: TbsSkinData;
bsCompressedStoredSkin1: TbsCompressedStoredSkin;
Button1: TbsSkinButton;
Label1: TbsSkinLabel;
Label2: TbsSkinLabel;
Label3: TbsSkinLabel;
num: TbsSkinEdit;
Edit3: TbsSkinEdit;
Button2: TbsSkinButton;
ListView1: TbsSkinListView;
bsSkinLabel1: TbsSkinLabel;
bsSkinLinkImage1: TbsSkinLinkImage;
bsSkinStatusBar1: TbsSkinStatusBar;
bsSkinStatusPanel1: TbsSkinStatusPanel;
bsSkinStatusPanel2: TbsSkinStatusPanel;
bsSkinPanel1: TbsSkinPanel;
bsSkinLabel2: TbsSkinLabel;
ListView2: TbsSkinListView;
Timer2: TTimer;
bsSkinStatusPanel3: TbsSkinStatusPanel;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure FormatData;
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure check;
procedure Timer2Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Portini :Tinifile;
ip,portnum :string;
LvItems,LvItems1,Lvitems2,j,h :integer;
implementation

{$R *.dfm}

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
//showmessage(format('服务器%s上的端口%d已打开',[ClientSocket1.host,ClientSocket1.port]));
//LvItems:=ListView1.Items.Count-1;
if LvItems1<ListView1.Items.Count then
begin
ListView1.Items[LvItems1].SubItems.Add('正常');
ClientSocket1.Close;
LvItems1:=LvItems1+1;
end
else
begin
//Timer1.Enabled:=False;
//Num.Enabled:=True;
//Edit3.Enabled:=True;
ClientSocket1.Close;
bsSkinStatusPanel1.Caption:='检测完成';
//Button1.Caption:='开始检测';
//ListView2.Items.Clear;
LvItems:=0;
end;
end;

procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
//showmessage(Format('无法连接服务器%s的端口%d',[ClientSocket1.host,ClientSocket1.port]));
ErrorCode := 0;
if LvItems1<ListView1.Items.Count then
begin
ListView1.Items[LvItems1].SubItems.Add('超时');
ListView2.Items[LvItems2].Caption:=ListView1.Items[LvItems1].Caption;
ListView2.Items[LvItems2].SubItems.Add(ListView1.Items[LvItems1].SubItems.Strings[0]);
ListView2.Items[LvItems2].SubItems.Add('超时');
//ListView2.Items[LvItems1].Caption:=ListView1.Items[LvItems1].SubItems.Strings[0];
ClientSocket1.Close;
LvItems1:=LvItems1+1;
if LvItems2<ListView1.Items.Count-1 then
begin
LvItems2:=LvItems2+1;
end;
end
else
begin
ClientSocket1.Close;
LvItems1:=0;
bsSkinStatusPanel1.Caption:='检测完成';
end;
end;

procedure TForm1.FormatData;
var
filename :String;
test :array[0..999]of char;
i :integer;
begin
filename:=ExtractFilePath(paramstr(0))+'ipdate.ini';
portini:=Tinifile.Create(filename);
num.Text:=portini.Readstring('settings','amount','');
Edit3.Text:=Portini.ReadString('settings','timeout','');
for i:=0 to StrToInt(Num.Text) do
begin
GetPrivateProfileString('iplist', pchar(edit2.Text), '为空',test,999,pchar(filename));
Edit2.Text:=IntToStr(i);
Edit1.Text:=test;
ip:=Trim(MidStr(Edit1.Text,1,15));
portnum:=Trim(MidStr(Edit1.Text,17,22));
if ip<>'为空' then
begin
ListView1.Items.Add;
ListView2.Items.Add;
LvItems:=ListView1.Items.Count-1;
ListView1.Items[LvItems].Caption:=ip;
ListView1.Items[LvItems].SubItems.Add(portnum);
end;
end;
bsSkinStatusPanel1.Caption:='初始化完成';
bsSkinStatusPanel2.Caption:='当前主机数量:'+IntToStr(ListView1.Items.Count);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
bsSkinStatuspanel3.Caption:='执行次数:1';
check;
Timer2.Enabled:=True;
end;

procedure TForm1.check;
begin
if (Num.Text<>'') and (Edit3.Text<>'') then
begin
//num.Text:=inttostr(ListView1.Items.Count);
Timer1.Interval:=strtoint(Edit3.Text);
if (Timer1.Interval<100) or (Timer1.Interval>1000) or
(strtoint(Num.Text)<1) or (strtoint(Num.Text)>1000) then
begin
Showmessage('设置有误,请重新设置.');
end
else
begin
LvItems1:=0;
if Button1.Caption='开始检测' then
begin
Edit3.Enabled:=False;
Num.Enabled:=False;
Timer1.Enabled:=True;
Button1.Caption:='停止检测';
end
else
begin
Edit3.Enabled:=True;
Num.Enabled:=True;
Timer2.Enabled:=False;
Timer1.Enabled:=False;
Button1.Caption:='开始检测';
end;
end;
end
else
begin
Showmessage('检测数量或延时设置不能为空');
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//ListView1.Items.Clear;
ListView2.Items.Clear;
FormatData;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
with ClientSocket1 do
begin
close;
if LvItems1<ListView1.Items.Count then
begin
bsSkinStatusPanel1.Caption:='正在运行';
Host:=ListView1.Items[LvItems1].Caption;
Port:=strtoint(ListView1.Items[LvItems1].SubItems.Strings[0]);
open;
end
else
begin
LvItems1:=0;
end;
end;
end;


procedure TForm1.FormShow(Sender: TObject);
begin
FormatData;
LvItems2:=0;
h:=1;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
h:=h+1;
bsSkinStatuspanel3.Caption:='执行次数:'+inttostr(h);
Timer1.Enabled:=True;
end;

end.

这是ipdate.ini
[iplist]
1=124.116.176.80 :80
2=61.178.89.222 :3389
3=61.178.89.222 :21
4=61.178.89.222 :30000
7=124.116.176.66 :80
8=222.41.134.47 :80
9=222.41.134.47 :3389

[settings]
amount=100
timeout=300
 
后退
顶部