这是ini文件的内容:
[server]
address=211.65.0.168
cmdport=8089
fileport=8088
[local]
path=d:/asst/
run=asst.exe
ini=asst.ini
[files]
count=2
file1=asst.exe
file2=asst.ini
这是客户端程序:
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ShellAPI, StdCtrls, IniFiles, Buttons, RXCtrls, RxGrdCpt, ScktComp;
type
TMainForm = class(TForm)
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
RxLabel1: TRxLabel;
RxLabel2: TRxLabel;
RxLabel3: TRxLabel;
RxGradientCaption1: TRxGradientCaption;
RxLabel4: TRxLabel;
RxLabel5: TRxLabel;
ClientSocket1: TClientSocket;
ClientSocket2: TClientSocket;
procedure FormActivate(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocket2Read(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
ServerAddress,cmdPort,filePort,File1,File2,LocalPath,RunName,IniName:string;
FileSize,RecvSize:Integer;
implementation
{$R *.DFM}
function CanRun:Boolean;
begin
//是否可以运行
if FileExists(LocalPath+RunName) and FileExists(LocalPath+IniName) then
Result:=True
else Result:=False;
end;
procedure GetInfo;
var
InfoFile:TIniFile;
begin
//获取配置信息
InfoFile:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'/checker.ini');
with InfoFile do
begin
ServerAddress:=ReadString('server','address','211.65.0.168');
cmdPort:=ReadString('server','cmdport','8089');
filePort:=ReadString('server','fileport','8088');
LocalPath:=ReadString('local','path','d:/asst/');
RunName:=ReadString('local','run','asst.exe');
IniName:=ReadString('local','ini','asst.ini');
File1:=ReadString('files','file1','asst.exe');
File2:=ReadString('files','file2','asst.ini');
Free;
end;
end;
procedure TMainForm.FormActivate(Sender: TObject);
begin
//获取配置信息
FileSize:=0;
RecvSize:=0;
GetInfo;
with ClientSocket1 do
begin
Address:=ServerAddress;
Port:=StrToInt(filePort);
Open;
end;
with ClientSocket2 do
begin
Address:=ServerAddress;
Port:=StrToInt(cmdPort);
Open;
end;
SpeedButton2.Enabled:=CanRun;
end;
procedure TMainForm.SpeedButton1Click(Sender: TObject);
begin
//更新上机助手
if FileExists(LocalPath+RunName) then DeleteFile(LocalPath+RunName);
ClientSocket2.Socket.SendText('/download -'+File1);
end;
procedure TMainForm.SpeedButton2Click(Sender: TObject);
begin
//运行上机助手
Winexec(PChar(LocalPath+RunName),SW_SHOW);
Close;
end;
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ClientSocket1.Active then ClientSocket1.Close;
if ClientSocket2.Active then ClientSocket2.Close;
end;
procedure TMainForm.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
Buffer
ointer;
nRetr:integer;
fs:TFileStream;
const
BufferSize=8760;
begin
GetMem(Buffer,BufferSize);
nRetr:=Socket.ReceiveBuf(Buffer^,BufferSize);
if not FileExists(LocalPath+File1) then
begin
fs :=TFileStream.Create(LocalPath+File1,fmCreate or fmShareDenyNone);
fs.Seek(0,soFromBeginning);
end else
begin
fs :=TFileStream.Create(LocalPath+File1,fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;
fs.WriteBuffer(Buffer^,nRetr);
fs.Destroy;
FreeMem(Buffer);
RecvSize:=RecvSize+nRetr;
if RecvSize=FileSize then SpeedButton2.Click;
end;
procedure TMainForm.ClientSocket2Read(Sender: TObject;
Socket: TCustomWinSocket);
var
SizeMsg:string;
begin
SizeMsg:=UpperCase(Trim(Socket.ReceiveText));
if Pos('FILESIZE=',SizeMsg)=1 then
FileSize:=StrToInt(Copy(SizeMsg,10,Length(SizeMsg)-9));
end;
end.