views:

427

answers:

2

Hello.

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.

A: 

Write is a method (or whatever it is called in Delphi) of TCPClient. Here's working code of what you want to do: http://delphi.about.com/od/internetintranet/l/aa012004a.htm. Should get you up & running in no time :)

Mike Versteeg
Actually, that's *not* working code. At least not for Indy 10. `Write` is not a method of `TIdTCPClient` anymore.
Rob Kennedy
Thank you Mike, the problem is that I do not have such method in my Indy version for the TIdTcpClient...also the documentation for Indy doest say about those methods... I belive that example which you have given is quite old and not up to date.
Wodzu
Mike Versteeg
+3  A: 

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.

Rob Kennedy
Thanks Rob, the page confused me due to its formating. Thanks for the help.
Wodzu