我用idtcpserver组件接受传入的信息,但是bRecBuffer的信息如果字节比较多(比如184个字节),就会出现数据不准的现象(184个字节总是中间缺少2个字节)。代码如下:
procedure TfrmNetCopyGPRS.IdTCPServer1Execute(AThread: TIdPeerThread);
var
 t:string;
 ilen:integer;
 bRecBuffer : TrecBuffer;
 i:integer;
 p:pointer;
 dataarr:tDataarr;
begin
  aThread.Connection.ReadFromStack;
  ilen := AThread.Connection.InputBuffer.Size;  if ilen<>0 then
  begin
     //memo1.Lines.Add('刚刚接收了'+inttostr(ilen)+'个数据');     AThread.Connection.ReadBuffer(bRecBuffer,ilen);
     UnitGPRSProtocol.ConvertRecBuffer(brecbuffer,dataarr,ilen);
     if (bRecBuffer[8]=5) and (bRecBuffer[10]=$20) then
     begin
        if(UnitGPRSProtocol.CompareCheckCode(dataarr,13)) and
                UnitGPRSProtocol.LeaderCharRight(dataarr,13) then      AThread.Connection.WriteBuffer(bRecBuffer,13);
      addToActiveCenterlist(bRecBuffer[9],AThread);
      exit; //退出来
     end
     else
     begin
        UnitGPRSProtocol.TreateRetData(iProtocoltype,G_FunctionCode,dataarr,self.edtValue.Text);
     end;
end

解决方案 »

  1.   

    我觉得楼主应该定制这样一个协议:每发数据前都将本次所发数据长度作为前导发出去,而后再发数据。接收方总是先接收这个定长的数据长度数据,确定即将接收到数据的长度,再读数据,这样就不用
    每次都检测当前接收栈的数据量。
    另外,如果是简单的字符串传输,完全可以使用WriteLn(字符串+终止符);ReadLn(终止符,等待时间);来传输,更有效。
      

  2.   

    支持使用WriteLn();ReadLn;但也有缓冲区大小的限制,可设置
      

  3.   

    我发现问题了,但是不知道怎么解决,我发现我收到的字节是08的时候就会,08的前一个字节和08就会消失,例如 发送的是 01,00,08,00,00,00,00但是我接受的却是01,00,00,00,00,我接受的是单片机通过gprs模块发送的信息,请问我如何解决整个问题