运行客户端抽象错误时TCP IP服务器客户端delphi

时间:2015-07-24 09:34:29

标签: delphi tcp client server

您好我正在尝试在Delphi中构建tcp服务器客户端应用程序 我有这个代码

unit UnitClientServer;

interface

uses
  IdCustomTCPServer, IdTCPClient, IdContext,
  SysUtils, Classes, Forms, StdCtrls, Controls, System.Actions, Vcl.ActnList;

type
  TMyPushClientThread = class(TThread)
  private
    TCPClient: TIdTCPClient;
    FLog: TStrings;
  public
    constructor Create(AHost: string; APort: Word; ALog: TStrings);
    destructor Destroy; override;
    procedure Execute; override;
  end;

  TMyPushServer = class (TIdCustomTCPServer)
  protected
    function DoExecute(AContext: TIdContext): Boolean; override;
  end;

  TServerPushExampleForm = class(TForm)
    MemoLog: TMemo;

    aclMain: TActionList;
    Button1: TButton;
    actStartClient: TAction;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure actStartClientExecute(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    ExampleClient: TMyPushClientThread;
    ExampleServer: TMyPushServer;
  end;

var
  ServerPushExampleForm: TServerPushExampleForm;

implementation

uses
  IdGlobal;

{$R *.dfm}

procedure TServerPushExampleForm.actStartClientExecute(Sender: TObject);
//var MyClient : TMyPushClientThread;
var myTstring : TStrings;

begin
     FreeAndNil(ExampleClient);
     myTstring := TStrings.Create;
     myTstring.Add('My log');


     ExampleClient:= TMyPushClientThread.Create('localhost', 8088, myTstring)  ;
   //  MyClient.Execute;
end;

procedure TServerPushExampleForm.Button2Click(Sender: TObject);
var myTstring : TStrings;
begin
      FreeAndNil(ExampleClient);
     myTstring := TStrings.Create;
     myTstring.Add('My log stevilka 1');
     ExampleClient := TMyPushClientThread.Create('localhost', 8088,myTstring );
end;

procedure TServerPushExampleForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
     // MainWindow.actServer.Enabled := True;
end;

procedure TServerPushExampleForm.FormCreate(Sender: TObject);
begin
  ExampleServer := TMyPushServer.Create;
  ExampleServer.DefaultPort := 8088;
  ExampleServer.Active := True;

  ExampleClient := TMyPushClientThread.Create('localhost', 8088, MemoLog.Lines);
end;

procedure TServerPushExampleForm.FormDestroy(Sender: TObject);
begin
  ExampleServer.Free;
  ExampleClient.Terminate;
  ExampleClient.WaitFor;
  ExampleClient.Free;
end;

{ TMyPushServer }

function TMyPushServer.DoExecute(AContext: TIdContext): Boolean;
begin
  Result := inherited;

  // simulate hard work
  Sleep(Random(3000));

  AContext.Connection.IOHandler.WriteLn(
    'Completed at ' + TimeToStr(Now), TIdTextEncoding.UTF8 );
end;

{ TMyPushClientThread }

constructor TMyPushClientThread.Create(AHost: string; APort: Word; ALog: TStrings);
begin
  inherited Create(False);

  FLog := ALog;

  TCPClient := TIdTCPClient.Create;
  TCPClient.Host := AHost;
  TCPClient.Port := APort;
  TCPClient.ReadTimeout := 500;
end;

destructor TMyPushClientThread.Destroy;
begin
  TCPClient.Free;
  inherited;
end;

procedure TMyPushClientThread.Execute;
var
  S: string;
begin
  TCPClient.Connect;

  while not Terminated do
  begin
    S := TCPClient.IOHandler.ReadLn(TIdTextEncoding.UTF8 );

    if not TCPClient.IOHandler.ReadLnTimedout then
    begin
      TThread.Queue(nil,
        procedure
        begin
          FLog.Append(S);
        end);
    end;

  end;

  TCPClient.Disconnect;
end;

end.

当我调用此按钮功能时:

 procedure TServerPushExampleForm.actStartClientExecute(Sender: TObject);
//var MyClient : TMyPushClientThread;
var myTstring : TStrings;

begin
     FreeAndNil(ExampleClient);
     myTstring := TStrings.Create;
     myTstring.Add('My log');


     ExampleClient:= TMyPushClientThread.Create('localhost', 8088, myTstring)  ;
   //  MyClient.Execute;
end;

我收到错误摘要错误。 为什么我不能运行客户端有什么问题? 我在这篇文章中发现了这段代码How to continuously send messages with TIdTCPServer?

1 个答案:

答案 0 :(得分:4)

TStrings是一个抽象类。使用抽象类会产生编译器警告和运行时错误。您需要使用具体的后代,例如TStringList,例如:

myTstring := TStringList.Create;