Indy tcp client

来源:互联网 发布:微信视频打赏平台源码 编辑:程序博客网 时间:2024/05/16 04:27
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBAccess, MyAccess, StdCtrls, ComCtrls, ExtCtrls, MemDS,
  MyRec, OBRARCompress, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, jpeg , inifiles;

type
  TForm1 
= class(TForm)
    MyConnection1: TMyConnection;
    pb1: TProgressBar;
    
Next: TButton;
    s: TButton;
    Panel1: TPanel;
    MyTable1: TMyTable;
    MyTable1id: TIntegerField;
    MyTable1path: TStringField;
    MyTable1pic: TStringField;
    OBRARCompress1: TOBRARCompress;
    IdTCPClient1: TIdTCPClient;
    Image1: TImage;
    procedure sClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure NotBt;
    procedure OBRARCompress1Compressing(Sender: TObject;
      Progress: 
Integer);
    procedure upload(
const FFileName : string);  
    procedure NextClick(Sender: TObject);
  
private
    { 
Private declarations }
  
public
    { 
Public declarations }
  
end;

var
  Form1: TForm1;
  doing: 
Integer;
  inifile : Tinifile;
implementation

{$R 
*.dfm}

procedure TForm1.sClick(Sender: TObject);
begin
  Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  doing :
= 1;
  inifile :
= Tinifile.Create(ExtractFilePath(paramstr(0))+'config.ini');
end;

procedure TForm1.NotBt;
begin
  
Next.Enabled:= not Next.Enabled;
end;

procedure TForm1.OBRARCompress1Compressing(Sender: TObject;
  Progress: 
Integer);
begin
  pb1.Position :
= Progress;
  Application.ProcessMessages;
end;

procedure TForm1.upload(
const FFileName : string);
var
  Buf : array[
0..1023of Byte;
  aSize, ReadCount, Tmpint : 
Integer;
  aStream : TFileStream;
  aIdTCPClient: TIdTCPClient;
begin
try
  aStream :
= TFileStream.Create(FFileName, fmOpenRead or fmShareDenyWrite);
  aIdTCPClient :
=  TIdTCPClient.Create(Application);
  aIdTCPClient.Port :
= StrToInt(inifile.readstring('Server','Port','3303'));
  aIdTCPClient.Host := inifile.readstring('Server','Host','59.36.101.91');
  aIdTCPClient.Connect;
  pb1.Position :
= 0;
    
//发送文件名
  aIdTCPClient.WriteLn(
'SEND:'+ExtractFileName(FFileName));
    //等待接受确认
    aIdTCPClient.ReadLn(#
13#101000);
    
//写文件长度和文件流
    aSize :
= aStream.Size;
    pb1.Max :
= aSize;
    aIdTCPClient.WriteBuffer(aSize, 
4);
        
while aStream.Position < aStream.Size do
        begin
          
if aStream.Size - aStream.Position >= SizeOf(Buf) then
              ReadCount :
= sizeOf(Buf)
          
else ReadCount := aStream.Size - aStream.Position;
          pb1.Position :
=  aStream.Position;
          aStream.ReadBuffer(Buf, ReadCount);
          aIdTCPClient.WriteBuffer(Buf, ReadCount);
        
end;
        aIdTCPClient.ReadLn;
        aIdTCPClient.Disconnect;
  
finally
  aIdTCPClient.Disconnect;
  
if aStream<>nil then FreeAndNil(aStream);
  
if aIdTCPClient<>nil then FreeAndNil(aIdTCPClient);
  
end;
end;

procedure TForm1.NextClick(Sender: TObject);
var
  Myrec:TMyrec;
  Rec:TPersonRec;
  Tp:TSearchRec;
  Tct:_SystemTime;
  filename:
string;
begin
case doing of
1:
  begin
  NotBt;
  MyTable1.Active :
= True;
  Myrec :
= TMyrec.Create('rec.dat',fmCreate or fmShareDenyWrite);
  pb1.Max := MyTable1.RecordCount;
  
while not MyTable1.Eof do
    begin
    Rec.id :
=varToStr(MyTable1.FieldValues['id']);
    Rec.path :=varToStr(MyTable1.FieldValues['path']);
    Rec.pic :=varToStr(MyTable1.FieldValues['pic']);
    if Rec.pic <>'' then
      begin
      filename :
= inifile.readstring('CONFIG','DataPath','E:yipartswwwDATAxG5ARCZH6c') + '/'+ varToStr(MyTable1.FieldValues['path']) + '/' + varToStr(MyTable1.FieldValues['pic']);
      FindFirst(filename,faAnyFile,Tp);
      FileTimeToSystemTime(Tp.FindData.ftLastWriteTime,Tct);
      Rec.tdate :
= FormatDateTime('YYYYMMDDHHMMSS',SystemTimeToDateTime(Tct));
      end;
    Myrec.AppandRec(Rec);
    pb1.Position :
= pb1.Position +1;
    MyTable1.Next;
    
end;//while
    Myrec.Free;
    Panel1.Caption :
= '开始压缩';
    pb1.Position := 0;
    pb1.Max :
= 100;
    OBRARCompress1.OnCompressing:
=OBRARCompress1Compressing;
    OBRARCompress1.SourceFiles.Add(ExtractFilePath(paramstr(
0))+'rec.dat');
    OBRARCompress1.RARFileName := ExtractFilePath(paramstr(0))+'rec.rar';
    OBRARCompress1.Compress;
    Panel1.Caption :
= '记录文件生成完成,开始上传至服务器。';
    upload(ExtractFilePath(paramstr(0))+'rec.rar');
    Panel1.Caption := '记录文件已上传至服务器,服务器开始分析。';
  end;//case 1:
end;//case doing of
end;

end.
 
原创粉丝点击