1. 程式人生 > 實用技巧 >cross socket tcp client demo

cross socket tcp client demo

cross socket tcp client demo

/// <author>cxg 2020-8-13</author>
/// cross socket tcp client demo

unit Unit1;

interface

uses
  MsgPack, uCmd, uCrossTcpClient,
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.Edit, FMX.StdCtrls, System.Rtti, FMX.Grid.Style,
  FMX.ScrollBox, FMX.Grid, Data.DB, Datasnap.DBClient, Data.Bind.EngExt,
  Fmx.Bind.DBEngExt, Fmx.Bind.Grid, System.Bindings.Outputs, Fmx.Bind.Editors,
  Data.Bind.Components, Data.Bind.Grid, Data.Bind.DBScope;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    StringGrid1: TStringGrid;
    StringGrid2: TStringGrid;
    ClientDataSet1: TClientDataSet;
    ClientDataSet2: TClientDataSet;
    BindSourceDB1: TBindSourceDB;
    BindingsList1: TBindingsList;
    LinkGridToDataSourceBindSourceDB1: TLinkGridToDataSource;
    BindSourceDB2: TBindSourceDB;
    LinkGridToDataSourceBindSourceDB2: TLinkGridToDataSource;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    Timer1: TTimer;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
    fClient: TCrossTcpClient;
    procedure query(pack: TMsgPack);   //因為cross tcp是全非同步,所以回叫方法處理遠端方法返回結果
    procedure save(pack: TMsgPack);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
//連線中介軟體
begin
  fClient.Connect(Edit1.Text, StrToInt(Edit2.Text));
  Button1.Enabled := False;
end;

procedure TForm1.Button2Click(Sender: TObject);
//查詢
begin
  var pack: TMsgPack := TMsgPack.Create;
  pack.Force('accountno').AsString := '1';    //帳套號
  pack.Force('plugid').AsInteger :=9999;      //外掛編號
  pack.Force('funcid').AsInteger := cmd_query; //查詢
  pack.Force('sql1').AsString := 'select * from tgoods';
  pack.Force('sql2').AsString := 'select * from tunit';
  pack.Force('tablenum').AsInteger := 2;   //查詢2個表
  pack.Force('cmd').AsInteger := cmd_plugbin;  //二進位制序列
  fClient.Send(pack);
end;

procedure TForm1.Button3Click(Sender: TObject);
//上傳檔案
begin
  if OpenDialog1.Execute then begin
    var pack: TMsgPack := TMsgPack.Create;
    pack.Force('filenum').AsInteger := 1;     //上傳幾個檔案
    pack.Force('file1').LoadBinaryFromFile(OpenDialog1.FileName); //檔案
    pack.Force('filename1').AsString := ExtractFileName(OpenDialog1.FileName);  //檔名稱
    pack.Force('plugid').AsInteger := 8888;   //外掛編號
    pack.Force('funcid').AsInteger := cmd_up;  //上傳檔案
    pack.Force('cmd').AsInteger := cmd_plugbin;  //二進位制序列
    fClient.Send(pack);
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
//儲存修改的資料
begin
  if ClientDataSet2.State in [dsInsert, dsEdit] then
    ClientDataSet2.Post;
  if ClientDataSet2.ChangeCount = 0 then
    Exit;
  var pack: TMsgPack := TMsgPack.Create;
  pack.Force('accountno').AsString := '1';   //帳套號
  pack.Force('plugid').AsInteger :=9999;     //外掛編號
  pack.Force('funcid').AsInteger := cmd_save;  //儲存修改的資料
  pack.Force('tablenum').AsInteger := 1;       //儲存幾個表
  pack.Force('tablename1').AsString := 'tunit';  //待儲存表名
  pack.Force('delta1').AsVariant := ClientDataSet2.Delta;  //修改的資料
  pack.Force('cmd').AsInteger := cmd_plugbin; //二進位制序列
  fClient.Send(pack);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  fClient := TCrossTcpClient.Create;
  fClient.OnQuery := query;
  fClient.OnSave := save;
//  Button1Click(nil);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  fClient.Free;
end;

procedure TForm1.query(pack: TMsgPack);
//查詢返回結果處理
begin
  ClientDataSet1.Data := pack.Force('dataset1').AsVariant;
  ClientDataSet2.Data := pack.Force('dataset2').AsVariant;
end;

procedure TForm1.save(pack: TMsgPack);
//儲存資料返回結果處理
begin
  if pack.Force('return').AsBoolean then
  begin
    ClientDataSet2.MergeChangeLog;
    ShowMessage('儲存成功');
  end
  else
  begin
    ClientDataSet2.Cancel;
    ShowMessage('儲存失敗');
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Button2Click(nil);
end;

end.