Indy TCP client example?

主宰稳场 提交于 2019-12-12 02:30:55

问题


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:


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.



来源:https://stackoverflow.com/questions/29528903/indy-tcp-client-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!