简体   繁体   English

使用Delphi XE2中的TidTCPClient和TidTCPServer发送和接收流

[英]Send and Receive Stream with TidTCPClient and TidTCPServer in Delphi XE2

In Delphi XE2 I have a record type with the following structure : 在Delphi XE2中,我有一个具有以下结构的记录类型:

  TMachinInfoRec = record
    IPStr: string[15];
    Username: string[50];
    Computername: string[100];
    SentTime: TDateTime;
    HasCommand: integer; 
    ClientCommands: array[0..9] of TMachineCommand;
  end;

I define a variable on its and TMemoryStream variable on the Client side and send stream with TidTCPClient component: 我在客户端和TMemoryStream变量上定义了一个变量,并使用TidTCPClient组件发送流:

var
  MIRec: TMachinInfoRec;
  msRecInfo: TMemoryStream;

begin
  MIRec.IPStr = '192.168.100.101';
  MIRec.Username := 'user-a';
  MIRec.Computername := 'Computer-a';
  MIRec.SentTime := Now();

  idTCPClient.Host := '192.168.100.138';
  idTCPClient.Port := 6000;

  idTCPClient.Connect;

  msRecInfo := TMemoryStream.Create;
  msRecInfo.Write(msRecInfo, SizeOf(Client));

  msRecInfo.Position := 0;
  idTCPClient.IOHandler.Write(msRecInfo);
end;

and And get information on the server side with TidTCPServer : 并使用TidTCPServer获取服务器端的信息:

procedure TFrmMainServer.TCPServerExecute(AContext: TIdContext);
var
  MIRec: TMachinInfoRec;
  msRecInfo: TMemoryStream;
begin
  msRecInfo:= TMemoryStream.Create;

  AContext.Connection.IOHandler.ReadStream(msRecInfo, SizeOf(MIRec));

  msRecInfo.Read(msRecInfo, sizeOf(MIRec));

  ShowMessage(MIRec.IPStr);
  ShowMessage(MIRec.Computername)
end;

But a string that is displayed in this format: 但是以这种格式显示的字符串:

MZ?.........yy..,.......@...................................,.... MZ?......... .. YY,....... @ ........................... ........ ....

how can I solve this problem? 我怎么解决这个问题?

Shouldn't 不能

     msRecInfo.Write(msRecInfo, SizeOf(Client));

be

     msRecInfo.Write(miRec, SizeOf(miRec));

Same for read: 阅读相同:

     msRecInfo.Read(miRec, sizeOf(MIRec));

Note that there are several other uncertain factors with this code: 请注意,此代码还有其他几个不确定因素:

  • what is "client"? 什么是“客户”? OTOH, with the above corrections, this is eliminated. OTOH,通过上述修正,这被消除了。
  • We can't confirm from this code that TMachineCommand is not a pointer type 我们无法从此代码确认TMachineCommand不是指针类型

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

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