Indy 10中的双向TCP通信? [英] Two-way TCP communication in Indy 10?

查看:65
本文介绍了Indy 10中的双向TCP通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TIdCmdTCPClient和TIdCmdTCPServer.突然我发现我想进行双向通信.

I am using TIdCmdTCPClient and 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?

这是一个非常小的系统.两个客户端和十个服务器,每天一次,几分钟一次,每30至60秒爆发一次,因此轮询的开销是无关紧要的.

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.

如果有一种正确"的方法,我只是混蛋.

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.

<事件类型>[< 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?)

使用两个TIdCmdTCPServer是否有可能(会过大杀伤力)?

Would it be possible (would it be overkill) to use two TIdCmdTCPServer?

推荐答案

在连接有效期内, TIdCmdTCPClient TIdCmdTCPServer 都会连续轮询其套接字端点以获取入站数据.您不必为此做任何额外的事情.因此,一旦 TIdCmdTCPClient 连接到 TIdCmdTCPServer ,两个组件将首先处于读取状态,直到其中一个向另一个发送命令为止.

Both TIdCmdTCPClient and TIdCmdTCPServer continuously poll their socket endpoints for inbound data during the lifetime of the connection. 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 是不明智的.您应该将 TIdTCPClient TIdCmdTCPServer 一起使用,或者将 TIdCmdTCPClient TIdTCPServer 一起使用.根据您的协议的实际情况,您可能不得不完全放弃使用 TIdCmdTCPClient TIdCmdTCPServer ,而仅将 TIdTCPClient TIdTCPServer ,这样您就可以更好地控制两端的读写.在不首先知道通信协议是什么样的情况下,很难用实际的代码来回答.

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. You should either use TIdTCPClient with TIdCmdTCPServer, or use TIdCmdTCPClient with TIdTCPServer. 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. It is hard to answer with actual code without first knowing what the communication protocol should look like.

这篇关于Indy 10中的双向TCP通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆