【问题标题】:TCP IP server client delphi when running client abstract error运行客户端抽象错误时的TCP IP服务器客户端delphi
【发布时间】:2015-07-24 09:34:29
【问题描述】:

您好,我正在尝试在 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?

【问题讨论】:

    标签: delphi tcp client server


    【解决方案1】:

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

    myTstring := TStringList.Create;
    

    【讨论】:

    • 我用这一行更改了代码,但我没有在服务器上收到消息
    • @TOndrej:我希望 Delphi 在尝试实例化抽象类时会在编译时失败,就像 C++ 那样。
    • @ververicka:那么您的代码中的某处显然仍然存在逻辑错误。你需要调试它。如果您仍然需要帮助,请发布一个关于它的新问题。这个问题已经回答了。
    猜你喜欢
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多