我有一个TIdTCPServer的OnExecute以下代码(2009 Delphi和印地10与安装来到),这是非常相似的其它实例在此网站;

   Socket := AContext.Connection.Socket;
    if Socket.CheckForDataOnSource(10) then
    begin
      if not Socket.InputBufferIsEmpty then
      begin
        Socket.InputBuffer.ExtractToBytes(RawBytes, -1, False, -1);

        SetLength(Buffer, Length(RawBytes));
        Move(RawBytes[0], Buffer[1], Length(RawBytes));

        // Do stuff with data here...
      end;
    end;
    AContext.Connection.CheckForGracefulDisconnect;

它不会在有时CheckForDataOnSource(10)返回假读取数据。但是如果我在该行停止调试器,我可以看到我在INPUTBUFFER的字节发送的数据。是否有任何其他设置的东西我应该做的或其他方式来迫使这个工作所有的时间。此代码运行一堆次,但总是失败上CheckForDataOnSource(10)。

另外,作为一个侧面说明,我在代码中发现了印地周围的一些人抓住AContext.Connection.IOHandler代替AContext.Connection.Socket,做同样的事情,上面的代码的地方,什么是“右”之一来使用。

由于

布鲁斯

有帮助吗?

解决方案

代码应该更是这样的:

var
  IO: TIdIOHandler.
  Buffer: RawByteString;
begin
  IO := AContext.Connection.IOHandler;

  if IO.InputBufferIsEmpty then
  begin
    IO.CheckForDataOnSource(10);
    if IO.InputBufferIsEmpty then Exit;
  end;

  IO.InputBuffer.ExtractToBytes(RawBytes, -1, False, -1);     
  // or: IO.ReadBytes(RawBytes, -1, False);

  SetLength(Buffer, Length(RawBytes));
  BytesToRaw(RawBytes, Buffer[1], Length(RawBytes));
  // Do stuff with Buffer here...
end;

其他提示

它看起来像你的代码看起来应该是这样;

Socket := AContext.Connection.Socket;
Socket.CheckForDataOnSource(10);
if not Socket.InputBufferIsEmpty then
begin
  Socket.InputBuffer.ExtractToBytes(RawBytes, -1, False, -1);

  SetLength(Buffer, Length(RawBytes));
  Move(RawBytes[0], Buffer[1], Length(RawBytes));

  // Do stuff with data here...
end;
AContext.Connection.CheckForGracefulDisconnect;

这不要紧,IOHandler你抢,所以一般一个好像去了。

对不起回答我的问题,但它可能有人会hepful ......也许吧。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top