18770

TIdTCPServer not reading data from socket sometimes

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

Answer1:

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;

Answer2:

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.

Recommend

  • Convert raw pixel data (as byte array) to BufferedImage
  • Remove attributes from XElement
  • Builder pattern
  • Is there a limit to DecodeBase64 from EncdDecd?
  • Divide two String into substrings and pair them
  • Debug application as a different user in VS2008
  • Hitting breakpoints in MonoDevelop 2.2 Beta 1 on OSX
  • Javascript variable assignment of object returns string
  • Eclipse and Zend Debugger breaks in the first line always, despite having unchecked that option
  • Visual Studio: Garbled debug watch of std::string's?
  • Select2 drop down change event not working
  • Rivets, Backbone.View and Backbone.Collection
  • Standard way for writing a debug mode in C++
  • Debug PHP in Atom While Using Vagrant
  • Edit assembly language code in Visual Studio while stepping through each statement
  • C++ String tokenisation from 3D .obj files
  • Determining the length of a read stream in node js
  • CUDA Debugging - VS on windows workstation, GPUs on Linux server?
  • RegistryKey.GetSubKeyNames returns names that are not in that subkey!
  • What is this strange character in chrome's resource css viewer?
  • incomplete type 'struct' error in C
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • Angular2 - Template reference inside NgSwitch
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • SonarQube: Cannot deactivate rule with missing quality profile
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Using a canvas object in a thread to do simple animations - Java
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • How to rebase a series of branches?
  • Debugging ASP.NET on a built-in web server suddenly stops
  • Trying to switch camera back to front but getting exception
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • apache spark aggregate function using min value
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs