转老赵同志的博客:Indy10记录及其他类型的传送

来源:互联网 时间:1970-01-01

idTcpClient及IdTcpServer传送记录时的方法:

先将记录转换成TBytes,

使用RawToBytes及BytesToRaw函数。

Type TMyData=record id:Integer; Name:Array[0..20] of Char; Sex:Array[0..10] of Char; Age:Byte; Address:Array[0..256] of Char; UpdateTime:double; end;

 客户端:

procedure SendMsg;varSendData:TMyData;beginSendData.ID:=10;StrPCopy(SendData.Name,'Wyatt');StrPCopy(SendData.Sex,'男');SendData.age:=25;StrPCopy(SendData.Address,'江苏省');SendData.InCom:=S.InSpeed;SendData.UpdateTime:=Now; MClient.IOHandler.Write(RawToBytes(SendData,SizeOf(SendData)););end;

服务端:

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);;varReadData:TMyData;buf:TBytes;beginSetLength(buf, 0);AContext.Connection.IOHandler.ReadBytes(buf, SizeOf(ReadData));BytesToRaw(buf, ReadData, SizeOf(ReadData));
with Memo1.line do
begin Add('ID:'+Inttostr(ReadData.Id)); Add(Name:'+StrPas(ReadData.Name)); Add('Sex:'+Inttostr(ReadData.sex)); Add('Age:'+Inttostr(ReadData.age)); Add('UpdateTime:'+DateTimeToStr(ReadData.UpdateTime)); end; end;


相关阅读:
Top