Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Porting old Delphi2007 application using TServerSocket....
#13
(08-28-2020, 08:53 PM)BosseB Wrote: In this case I have found a number of examples which for some reason do not work (Lazarus complains)

In what way exactly does it complain?

(08-28-2020, 08:53 PM)BosseB Wrote: which use a thread to read incoming data and then use an event function to feed the main application.

That is generally the way to do it.

(08-28-2020, 08:53 PM)BosseB Wrote: What should I put into the thread execute function on data reception to transfer it into the event?
Should I read the data in the thread or should I just push a notification that data are available and let the implementation of the event procedure read from the socket?

That is more of a design issue than a technical issue.  Use whichever design suits your needs.

I will say, though, that you should not create the reading thread until after the TIdTCPClient has been connected first.  And then terminate and free the thread when the TIdTCPClient is being disconnected.

(08-28-2020, 08:53 PM)BosseB Wrote: Here is my current (incomplete) state of the code:

If you are going to have the event pass a string, then the reading thread should handle all of the necessary reading and partial-message buffering and such, and pass only completed messages to the event.  Also, this would only work for text-based protocols.

Otherwise, I would suggest having the reading thread just read whatever raw bytes are on the socket at a time and pass the bytes as-is to the event, and let the event handler decide what to do with the bytes as needed.

(08-28-2020, 08:53 PM)BosseB Wrote:
Code:
SetLength(DataBytes, Length(Data));
Move(Data[1], DataBytes[0], Length(Data));

FYI, Indy has RawToBytes() and ToBytes(string) functions in the IdGlobal unit for that purpose, eg:

Code:
function TTcpClientComm.WriteString(Data: string): boolean;
begin
  Result := WriteData(ToBytes(Data));
end;

Of course, TIdIOHanlder.Write() has separate overloads for sending a string vs a TIdBytes.

Reply


Messages In This Thread
RE: Porting old Delphi2007 application using TServerSocket.... - by rlebeau - 08-28-2020, 10:51 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)