Indy TCP客户端示例? [英] Indy TCP client example?
问题描述
在C ++ Builder中是否有用于Indy 10套接字的示例代码?
Is there any sample code for Indy 10 sockets in C++Builder?
Indy演示页面上的两个示例链接均为无效链接经过大量搜索后,我一直找不到任何示例代码.
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.
我正在编写一个将发送和接收JSON消息的客户端,不需要复杂的协议或SSL.
I am writing a client that will send and receive JSON messages, no complicated protocols or SSL required.
我已经能够根据TIdTCPClient的成员函数来猜测,编写id1->Socket->WriteLn
来发送服务器已接收到的内容,但尚未弄清楚如何接收服务器的响应.
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.
此外,是否有有关Indy TCP客户端的概述文档?在某些Delphi片段中,我看到使用id1.IOHandler.WriteLn
代替,但是我没有看到有关IOHandler的用途,应该使用哪个,IOHandler.WriteLn和Socket.WriteLn之间的区别等的任何解释.
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.
推荐答案
Indy演示页面上的两个示例链接均为无效链接
The two sample links on the Indy Demos page are dead links
该页面上唯一无效的链接是Ralph的TIdTCPClient/Server with SSL
演示.其他链接也可以正常工作,包括指向TCP/IP Delphi&Indy10 Client Server Demo
的链接.
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.
然后,您的搜索效果并不理想,因为在Embarcadero和Indy论坛上甚至在StackOverflow上都发布了很多示例.
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.
我已经能够根据TIdTCPClient的成员函数来猜测,编写
id1->Socket->WriteLn
来发送服务器已接收到的内容,但尚未弄清楚如何接收服务器的响应.
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
不是异步组件.仅当您告诉它读取时,它才会读取.假设您的WriteLn()
正在发送请求,则可以在WriteLn()
退出后立即调用ReadLn()
(或所需的任何读取方法),例如:
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.
还有,有关Indy TCP客户端的概述文档吗?
Also, is there any overview documentation for Indy TCP client?
官方文档在Indy的网站上:
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.
在一些Delphi片段中,我看到使用了
id1.IOHandler.WriteLn
,但是我没有看到有关IOHandler的用途,应该使用哪个,IOHandler.WriteLn和Socket.WriteLn之间的区别等的任何解释.
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.
为方便起见,提供了Socket
属性.当IOHandler
属性指向TIdIOHandlerSocket
对象时,Socket
属性将返回同一对象.这避免了为访问任何特定于套接字的功能而进行类型转换IOHandler
的任何需要.除了用于套接字I/O之外,Indy还实现了几种IOHandler
,您也可以编写自定义的IOHandler
.
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 IOHandler
s other than for socket I/O, and you can write custom IOHandler
s as well.
IOHandler
完成所有实际工作.访问任何与IO不相关的方法(如WriteLn()
和ReadLn()
)时,应使用IOHandler
属性而不是Socket
属性.这样,您可以随意换出不同的IOHandler
对象.例如,在捕获套接字活动并出于调试目的对其进行重放时,这很有用.
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.
这篇关于Indy TCP客户端示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!