简体   繁体   English

Indy 10中的双向TCP通信?

[英]Two-way TCP communication in Indy 10?

I am using TIdCmdTCPClient and TIdCmdTCPServer. 我正在使用TIdCmdTCPClient和TIdCmdTCPServer。 Suddenly I find that I might like to have bi-directional communication. 突然间,我发现我想进行双向通信。

What would be best? 什么是最好的? Should I possibly use some other components? 我是否应该使用其他组件? If so, which? 如果是这样? Or should I kludge and have the 'client' poll the 'server' to ask if it wishes to communciate anything? 还是我应该笨拙地让“客户”对“服务器”进行调查,以询问是否希望进行任何通信?

This is a very small system. 这是一个非常小的系统。 Two clients and ten servers, with a burst of one tarnscation every 30 to 60 seconds for a few minutes once a day, so overhead for polling is inconsequential. 两个客户端和十个服务器,每天一次,几分钟一次,每30到60秒爆发一次,因此轮询的开销是无关紧要的。

I'm just woder if there is a 'correct' way. 如果有一种“正确”的方法,我只是混蛋。


Update: this really is an incredibly simple system. 更新:这确实是一个非常简单的系统。 Very little traffic and all of it simple. 流量很少,而且一切都很简单。 All transmissions are an indication of even type an an optional single parameter. 所有传输均指示偶数类型(可选的单个参数)。

<event type> [ <parameter>] eg "HERE_IS_SOME_DATA 42" <event type> [ <parameter>]例如"HERE_IS_SOME_DATA 42"

This can be sent in both directions, hover here is no "reply" as such. 可以双向发送,悬停在这里并不是“回复”。 Just fire off a message (and hope that it got there)? 只是开出一条消息(并希望它到达那里)? Receive an Ack with no data? 收到没有数据的确认? Non-catching of an exception indicates that message was successfully sent?) 未捕获到异常表示消息已成功发送?)

Would it be possible (would it be overkill) to use two TIdCmdTCPServer? 使用两个TIdCmdTCPServer是否可能(会过大)?

Both TIdCmdTCPClient and TIdCmdTCPServer continuously poll their socket endpoints for inbound data during the lifetime of the connection. 在连接有效期内, TIdCmdTCPClientTIdCmdTCPServer连续轮询其套接字端点以获取入站数据。 You do not have to do anything extra for that. 您不必为此额外做任何事情。 So, as soon as a TIdCmdTCPClient connects to the TIdCmdTCPServer , both components will initially be in a reading state until one of them sends a command to the other. 因此,一旦TIdCmdTCPClient连接到TIdCmdTCPServer ,两个组件将首先处于读取状态,直到其中一个组件向TIdCmdTCPServer组件发送命令为止。

Now, there is a problem with doing that - as soon as either component sends that first command, the receiving component will interpret it as a command and send back a reply, which the other component will interpret as a command and send back a reply, which will be interpretted as a command and send back a reply, and so on, causing an endless cycle of replies back and forth. 现在,这样做有一个问题-任何一个组件发送第一个命令,接收组件都会将其解释为命令并发送回一个答复,另一个组件会将其解释为命令并发送回一个答复,它将被解释为命令并发回答复,依此类推,导致无休止的来回答复循环。 For that reason, it is not wise to use TIdCmdTCPClient and TIdCmdTCPServer together. 因此,同时使用TIdCmdTCPClientTIdCmdTCPServer是不明智的。 You should either use TIdTCPClient with TIdCmdTCPServer , or use TIdCmdTCPClient with TIdTCPServer . 您应该将TIdTCPClientTIdCmdTCPServer使用,或者将TIdCmdTCPClientTIdTCPServer TIdCmdTCPClient使用。 Depending on what exactly your protocol looks like, you may have to forgo using TIdCmdTCPClient and TIdCmdTCPServer altogether and just use TIdTCPClient with TIdTCPServer so you have more control over reading and writing on both ends. 根据您的协议的确切外观,可能必须完全放弃使用TIdCmdTCPClientTIdCmdTCPServer而仅将TIdTCPClientTIdTCPServer一起使用,这样您就可以更好地控制两端的读写。 It is hard to answer with actual code without first knowing what the communication protocol should look like. 在不首先知道通信协议是什么样的情况下,很难用实际的代码来回答。

A single TCP socket connection can be used in two directions. 单个TCP套接字连接可以在两个方向上使用。 The server can send data asynchronously to the client at any time. 服务器可以随时将数据异步发送到客户端。 It is up to the client however to read the socket, for asynchronous processing this is done in a listener thread which reads from the socket and synchronizes incoming data operations with the main worker thread. 但是,由客户端决定是否读取套接字,对于异步处理,这是在侦听器线程中完成的,该侦听器线程从套接字读取并将输入的数据操作与主工作线程同步。

An example use case in the Indy components is the Telnet client component (TIdTelnet) which has a receive thread listening for server messages. Indy组件中的一个示例用例是Telnet客户端组件(TIdTelnet),该组件具有一个侦听服务器消息的接收线程。

But you also asked about the 'correct' way - and then the answer depends on other factors such as network stability, guaranteed delivery and how to handle temporary server outages. 但是您还询问了“正确”的方法-然后答案取决于其他因素,例如网络稳定性,保证的交付以及如何处理服务器临时中断。 In enterprise environments, one central messaging hub is preferred in many use cases, so that all parties connect only to this central server which is only responsible for reliable message delivery, and keeps messages until the recipient is available. 在企业环境中,在许多使用情况下,最好使用一个中央消息传递中心,以便所有各方仅连接到该中央服务器,该中央服务器仅负责可靠的消息传递,并保留消息直到收件人可用为止。

您可以在此处下载INDY 10 TCP server演示示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM