简体   繁体   English

在Indy TCP中发送消息

[英]Send a message in indy TCP

iam trying to send message like this 我试图发送这样的消息

strMsg := Memo.Text;
Tclient.IOHandler.WriteLn(strMsg);

But how do i recive the text inside memo.text in server execute event? 但是我该如何在服务器执行事件中获取memo.text中的文本?

Remy example is fine but how i do it with code like this 雷米的例子很好,但我如何用这样的代码

procedure TChatFo.SendClick(Sender: TObject);
var
  strMsg: string;
begin
if tmemo.Text = '' then Abort;
strMsg := tmemo.Text;
Tcli.IOHandler.WriteLn(strMsg);
usertype.Clear;
end;

Indy servers are multi-threaded. Indy服务器是多线程的。 Data is received in a worker thread, not the main UI thread. 数据是通过工作线程而不是主UI线程接收的。 Once you have read the incoming message data (using AContext.Connection.IOHandler.ReadLn , for instance), you must then synchronize with the main thread if you want to display the message in a UI component. 读取传入的消息数据后(例如,使用AContext.Connection.IOHandler.ReadLn ),如果要在UI组件中显示消息,则必须与主线程同步。 You can use Indy's TIdSync or TIdNotify class for that synching, or use the anonymous procedure overloads of TThread.Synchronize() or TThread.Notify() if your version of Delphi has them available. 您可以将Indy的TIdSyncTIdNotify类用于该同步,或者如果您的Delphi版本具有可用的,则可以使用TThread.Synchronize()TThread.Notify()的匿名过程重载。

For example: 例如:

uses
  ..., IdSync;

type
  TMemoSync = class(TIdSync)
  protected
    FText: string;
    procedure DoSynchronize; override;
  end;

procedure TMemoSync.DoSynchronize;
begin
  Form1.Memo1.Lines.Add(FText);
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
  Text: String;
begin
  Text := AContext.Connection.IOHandler.ReadLn; 

  with TMemoSync.Create do
  try
    FText := Text;
    Synchronize;
  finally
    Free;
  end;
end;

Or: 要么:

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
  Text: String;
begin
  Text := AContext.Connection.IOHandler.ReadLn; 
  TThread.Synchronize(nil,
    procedure
      Form1.Memo1.Lines.Add(Text);
    end
  );
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM