我发现怎么找我就找不到获取TIDTCPServer.onexecute事件中athread.connection的已接收的数据长度呢?
athread.connection.currentreadbuffersize很像,但是一直是0,没变过。
当然我确定接收缓冲区是肯定有数据的。
怎么办好啊?

解决方案 »

  1.   

    看这个帖子是否会有帮助
    http://community.csdn.net/Expert/topic/4369/4369081.xml?temp=.600567
      

  2.   

    你可以在你发送的数据前面加一个长度,接收的时候先接收长度,然后根据长度接收数据:)发送端示例:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      buf: array[0..1023] of byte;
      count: integer;
    begin
      Randomize;
      count:=RandomRange(1, 1024);
      IdTcpClient1.WriteBuffer(count, sizeof(count));
      IdTcpClient1.WriteBuffer(buf, count);
    end;接收端示例:
    procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
    var
      buf: array[0..1023] of byte;
      count: integer;
    begin
      try
        AThread.Connection.ReadBuffer(count, sizeof(count));
        AThread.Connection.ReadBuffer(buf, count);
      finally  end;
    end;
      

  3.   

    呵呵,刚才看别人的帖子的时候找到答案了:)AThread.Connection.ReadFromStack();
      

  4.   

    重贴一次:)发送端示例:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      buf: array[0..1023] of byte;
      count: integer;
    begin
      Randomize;
      count:=RandomRange(1, 1024);
      IdTcpClient1.WriteBuffer(buf, count);
    end;接收端示例:
    procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
    var
      buf: array[0..1023] of byte;
      count: integer;
    begin
      try
        count:=AThread.Connection.ReadFromStack;
        if count<>0 then
        begin
          AThread.Connection.ReadBuffer(buf, count);
        end;
      finally  end;
    end;