Delphi – Sending binary data with Indy through TCP\IP, how

delphiindytcptcpclient

How to send a binary data with Indy components? Which of them is most suitable for this task? I've tried to use TIdTcpClient but it allows only to send strings.

I've found one reponce for that problem here but I don't get it.
It says about method Write(TIdBytes), but the answer is not clear for me. Does he meant Write to some instance of TIdBytes, and how to connect that instance with TIdTcpClient?

Thanks for any help.

Best Answer

The page you cite doesn't reproduce the messages very well. Here's what Remy really wrote:

SendCmd() is designed for textual commands/parameters only. You would have to send the binary data after SendCmd() exited, and the server would have to read the binary data after sending a response back to the client. For example:

--- client ---

begin
  IdTCPClient1.SendCmd('DoIt', 200);
  // send binary data, such as with Write(TStream) or Write(TIdBytes)...
end;

The Write methods he was talking about are members of the TIdIOHandler class. Your TIdTCPConnection object has an instance of that class in its IOHandler property, and indeed that's what the SendCmd function uses to send its string.

The notation Write(TIdBytes) means to use the Write method that accepts a TIdBytes parameter for its input.

If the binary data is already in a stream or a dynamic array of bytes, then you can pass one of those directly to the Write method. There's also the WriteFile method that will send an entire external file if you provide the file's name. If you use the stream version, then you have the option of including the stream's length automatically.

If you don't have your data in one of those structures already, then you can either write the data piecemeal with the Write methods that accept variously sized integer types, or you can copy your data into a TMemoryStream and then pass that to Write.

Related Topic