TIdTCPServer not reading data from socket sometimes

时间秒杀一切 提交于 2019-12-10 23:08:21

问题


I have the following code in the OnExecute of a TIdTCPServer (Delphi 2009 and Indy 10 that came with the installation) which is very similar to other examples on this site;

   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;

It doesn't read the data in sometimes as CheckForDataOnSource(10) returns False. However if I stop the debugger at that line I can see the data I sent in the InputBuffer's bytes. Are there any other setup things I should do or other ways to force this to work all the time. This code is run bunch of times but always fails on the CheckForDataOnSource(10).

Also as a side note, I notice in code for Indy around the place that some people grab the AContext.Connection.IOHandler instead of the AContext.Connection.Socket and do the same things as the code above, what is the "right" one to use.

Thanks

Bruce


回答1:


The code should be more like this:

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;



回答2:


It looks like your code should look like this;

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;

It doesn't matter what IOHandler you grab, so the generic one seems like the go.

Sorry about answering my own question but it might be hepful for someone... maybe.



来源:https://stackoverflow.com/questions/1507136/tidtcpserver-not-reading-data-from-socket-sometimes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!