1
votes

Is there any sample code for Indy 10 sockets in C++Builder?

The two sample links on the Indy Demos page are dead links and I have been unable to find any sample code after extensive searching.

I am writing a client that will send and receive JSON messages, no complicated protocols or SSL required.

I have been able to guess based on the member functions of TIdTCPClient to write id1->Socket->WriteLn to send something which gets received by the server but have not yet figured out how to receive the server's response.

Also, is there any overview documentation for Indy TCP client? In some Delphi snippets I saw id1.IOHandler.WriteLn used instead but I don't see any explanation of what IOHandler is for, which one I should use, what the difference is between IOHandler.WriteLn and Socket.WriteLn, etc.

1
@RemyLebeau OK. I see that I went the wrong way the first time on that demo and ended up here which only has Flashes and PDFs, instead of using the SVN link. That demo is only in Delphi but it's better than nothing I guess.M.M

1 Answers

3
votes

The two sample links on the Indy Demos page are dead links

The only dead link on that page is Ralph's TIdTCPClient/Server with SSL demo. The other links work fine, including the one to TCP/IP Delphi&Indy10 Client Server Demo.

I have been unable to find any sample code after extensive searching.

Then you are not searching very well, because there have been tons of examples posted in the Embarcadero and Indy forums, and even here on StackOverflow.

I have been able to guess based on the member functions of TIdTCPClient to write id1->Socket->WriteLn to send something which gets received by the server but have not yet figured out how to receive the server's response.

TIdTCPClient is not an asynchronous component. It reads only when you tell it to read. Assuming your WriteLn() is sending a request, you can call ReadLn() (or whatever reading method you want) immediately after WriteLn() exits, eg:

id1->Socket->WriteLn("JSON data here");
String response = id1->Socket->ReadLn();

If you want to read responses asynchronously, do the reading in a separate worker thread.

Also, is there any overview documentation for Indy TCP client?

The official documentation is on Indy's website:

http://www.indyproject.org/Sockets/Docs/index.aspx

http://www.indyproject.org/docsite/html

The documentation is a bit old, especially the class reference portion, but the overviews still largely apply.

In some Delphi snippets I saw id1.IOHandler.WriteLn used instead but I don't see any explanation of what IOHandler is for, which one I should use, what the difference is between IOHandler.WriteLn and Socket.WriteLn, etc.

The Socket property is provided for convenience. When the IOHandler property points at a TIdIOHandlerSocket object, the Socket property returns that same object. This avoids any need to type-cast the IOHandler to access any socket-specific functionality. Indy implements several IOHandlers other than for socket I/O, and you can write custom IOHandlers as well.

The IOHandler does all the real work. You should use the IOHandler property instead of the Socket property when accessing any IO-agnostic methods, like WriteLn() and ReadLn(). This way, you can swap out different IOHandler objects at will. This is useful, for instance, when capturing socket activity and replaying it for debugging purposes.