【发布时间】:2011-07-08 13:24:20
【问题描述】:
我在使用 Indy (idTCPServer 组件) 读取客户端发送的数据时遇到一些问题,数据本身是十六进制格式,所以我不能使用 AThread.Connection.ReadLn( ); 为此...
这是我的客户端发送的示例数据
24 24 00 11 12 34 56 FF FF FF FF 50 00 8B 9B 0D 0A
或
24 24 00 13 12 34 56 FF FF FF FF 90 02 00 0A 8F D4 0D 0A
PS:它以十六进制字节为单位(数据长度可能因命令而异,最大 160 字节)由于 $00 转换为 null,我无法获得字符串表示(这意味着我不能使用 ReadLn)
这是我的示例代码
procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
Msg : Array[0..255] of Byte;
begin
AThread.connection.ReadBuffer(Msg,SizeOf(Msg));
AThread.connection.WriteBuffer(Msg,MsgSize,true);
end;
如果客户端不发送 255 字节数据,此代码将不起作用,而在我的情况下,数据长度可能会有所不同,我已经尝试过,但没有输出响应
procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
Msg : Array of Byte;
MsgSize : integer;
begin
MsgSize := AThread.connection.ReadInteger; //doesn't actually get packet length?
SetLength(Msg, MsgSize);
AThread.connection.ReadBuffer(Msg,MsgSize);
AThread.connection.WriteBuffer(Msg,MsgSize,true);
end;
那么我如何准确计算客户端发送了多少字节数据(数据包长度)?或者有人可以告诉我读取数据的正确代码吗?
【问题讨论】:
-
你为什么使用原始 TCP 组件?您尝试通过 TCP 进行通信的协议是什么?当提出这样的位级问题时,这是一个非常重要的问题。