ClientThread.pas
上传用户:fh681027
上传日期:2022-07-23
资源大小:1959k
文件大小:3k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit ClientThread;
  2. interface
  3. uses
  4.   Classes, ScktComp, DBTables;
  5. type
  6.   TLogEvent = procedure(Sender: TObject; LogMsg: String) of object;
  7.   TSendThread = class(TThread)
  8.   private
  9.     ClientSocket: TClientSocket;
  10.     FTable: TTable;
  11.     FOnLog: TLogEvent;
  12.     FLogMsg: String;
  13.     FServerAddress: string;
  14.     procedure SetOnLog(const Value: TLogEvent);
  15.     procedure SetServerAddress(const Value: string);
  16.   protected
  17.     procedure Execute; override;
  18.     procedure DoLog;
  19.   public
  20.     constructor Create(ATable: TTable);
  21.     property OnLog: TLogEvent read FOnLog write SetOnLog;
  22.     property ServerAddress: string read FServerAddress write SetServerAddress;
  23.   end;
  24. implementation
  25. uses
  26.   ClientForm;
  27. constructor TSendThread.Create(ATable: TTable);
  28. begin
  29.   FTable := ATable;
  30.   inherited Create(True);
  31.   FreeOnTerminate := True;
  32. end;
  33. procedure TSendThread.DoLog;
  34. begin
  35.   if Assigned(FOnLog) then
  36.     FOnLog(self, FLogMsg);
  37. end;
  38. procedure TSendThread.Execute;
  39. var
  40.   I: Integer;
  41.   Data: TStringList;
  42.   Stream: TWinSocketStream;
  43.   Buf: String;
  44. begin
  45.   try
  46.     Data := TStringList.Create;
  47.     ClientSocket := TClientSocket.Create (nil);
  48.     Stream := nil;
  49.     try
  50.       ClientSocket.Address := ServerAddress;
  51.       ClientSocket.ClientType := ctBlocking;
  52.       ClientSocket.Port := 51;
  53.       ClientSocket.Active := True;
  54.       Stream := TWinSocketStream.Create(ClientSocket.Socket, 30000);
  55.       FTable.First;
  56.       while not FTable.Eof do
  57.       begin
  58.         // if the record is still not logged
  59.         if FTable.FieldByName('CompID').IsNull or (FTable.FieldByName('CompID').AsInteger = 0) then
  60.         begin
  61.           FLogMsg := 'Sending ' + FTable.FieldByName('Company').AsString;
  62.           Synchronize(DoLog);
  63.           Data.Clear;
  64.           // create strings with structure "FieldName=Value"
  65.           for I := 0 to FTable.FieldCount - 1 do
  66.             Data.Values [FTable.Fields[I].FieldName] :=
  67.               FTable.Fields [I].AsString;
  68.           // send the record
  69.           Buf := Data.Text + #10#13'.'#10#13;
  70.           ClientSocket.Socket.SendText(Buf);
  71.           // wait for reponse
  72.           if Stream.WaitForData(30000) then
  73.           begin
  74.             FTable.Edit;
  75.             SetLength(Buf, 256);
  76.             SetLength(Buf, Stream.Read(Buf[1], Length(Buf)));
  77.             FTable.FieldByName('CompID').AsString := Buf;
  78.             FTable.Post;
  79.             FLogMsg := FTable.FieldByName('Company').AsString +
  80.               ' logged as ' + FTable.FieldByName('CompID').AsString;
  81.           end
  82.           else
  83.             FlogMsg := 'No response for ' + FTable.FieldByName('Company').AsString;
  84.           Synchronize(DoLog);
  85.         end;
  86.         FTable.Next;
  87.       end;
  88.     finally
  89.       ClientSocket.Active := False;
  90.       ClientSocket.Free;
  91.       Stream.Free;
  92.       Data.Free;
  93.     end;
  94.   except
  95.     // trap exceptions
  96.   end;
  97. end;
  98. procedure TSendThread.SetOnLog(const Value: TLogEvent);
  99. begin
  100.   FOnLog := Value;
  101. end;
  102. procedure TSendThread.SetServerAddress(const Value: string);
  103. begin
  104.   FServerAddress := Value;
  105. end;
  106. end.