定义
FReceiveBuf: PChar; //接收数据缓存指针
FReceiveSize: integer; //接收数据缓存的大小
FReceiveCount: integer; //接收到的数据字节计数(总共接收多少数据)
初始化:
FReceiveCount := 0;
FReceiveSize := 1024 * 16; // 16KB 的字节
if not Assigned(FReceiveBuf) then
GetMem(FReceiveBuf, FReceiveSize);
释放:
if Assigned(FReceiveBuf) then
begin
FreeMem(FReceiveBuf, FReceiveSize);
FReceiveBuf := nil;
end;
在 OnRead 事件:
NewLen := FReceiveCount + FClientSocket.ReceiveLength;
if NewLen >= FReceiveSize then
begin
if NewLen < (FReceiveSize + (1024 * 16)) then //按照 16 KB 字节递增分配字节
NewLen := FReceiveSize + (1024 * 16);
ReallocMem(FReceiveBuf, NewLen);
FReceiveSize := NewLen;
end;
Len := FClientSocket.ReceiveBuf(FReceiveBuf[FReceiveCount],
FReceiveSize - FReceiveCount - 1);
...相关判断数据边界或者其他处理方式。