Indy10记录及其他类型的传送

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

先将记录转换成TBytes,

使用RawToBytes及BytesToRaw函数。这两个函数包含在IdGlobal单元中。
另外,在接收或发送的时候用到TIdContext类,这个类包含在IdConText单元中。
举例(TcpClient及TcpServer):

记录:

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;
var
SendData:TMyData;
begin


SendData.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);;
var
ReadData:TMyData;

buf:TBytes;
begin

SetLength(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;

//以上在Uses中要增加以下两个单元
uses    IdContext,IdGlobal

你可能感兴趣的:(java)