简体   繁体   English

带Indy 10的Delphi-TCP通讯的最佳方法

[英]Delphi with Indy 10 - Best approach for TCP communication

I am creating a client-server application using Delphi XE3 and Indy 10 (idTCPServer and idTCPClient). 我正在使用Delphi XE3和Indy 10(idTCPServer和idTCPClient)创建客户端-服务器应用程序。

Server side will show all connected clients, and I can select some clients on the list and send them commands or streams/files. 服务器端将显示所有已连接的客户端,我可以在列表中选择一些客户端,然后向其发送命令或流/文件。 For this I created a message queue, as sugested by Mr. Remy Lebeau. 为此,我创建了一个消息队列,由Remy Lebeau先生提出。 Here is what I am doing: 这是我在做什么:

在此处输入图片说明

What I would like to know is: 我想知道的是:

  1. Is this a good approach for what I am trying to do? 这是我尝试做的一个好方法吗?

  2. When one side starts read/writing, it expects other side to write/read? 当一侧开始读取/写入时,它期望另一侧进行写入/读取吗? What if other side can't? 如果另一边不能怎么办? Supose SERVER requests a file, but it doesn't exists, must CLIENT write an "empty" stream anyway to avoid problems? 假设SERVER请求一个文件,但该文件不存在,那么CLIENT是否必须编写一个“空”流来避免出现问题?

BTW, I can't find any good example of this (Indy 10 TCP Communication), using queue, error handling, etc. On Indy's website there are many broken links. 顺便说一句,在队列,错误处理等方面,我找不到任何很好的示例(Indy 10 TCP通信)。在Indy的网站上,有很多断开的链接。 Can you sugest me a website with good examples? 你能给我一个很好的例子的网站吗?

Thanks for any help! 谢谢你的帮助!

When one side starts read/writing, it expects other side to write/read? 当一侧开始读取/写入时,它期望另一侧进行写入/读取吗? What if other side can't? 如果另一边不能怎么办? Supose SERVER requests a file, but it doesn't exists, must CLIENT write an "empty" stream anyway to avoid problems? 假设SERVER请求一个文件,但该文件不存在,那么CLIENT是否必须编写一个“空”流来避免出现问题?

Make the client send a reply accepting/rejecting the request before the file can then be transferred. 让客户端发送一个答复,接受/拒绝该请求,然后再传输文件。 Also make the receiver send a reply after the transfer is finished so the sender knows whether the whole file was received or not on the receiver's end. 还要使接收方在传输完成后发送回复,以便发送方在接收方知道是否接收到整个文件。

Server: I will send a file
Client: OK
Server: FileStream
Client: OK

Server: Send me a file
Client: OK
Client: FileStream
Server: OK

Server: I will send a file
Client: Not Ready

Server: Send me a file
Client: Not Found

Server: Send me a file
Client: OK
Client: FileStream (error midway)
Server: FAILED

With that said, since your server is the one sending commands to a client, consider using TIdCmdTCPClient on the client side instead of TIdTCPClient . 话虽如此,由于服务器是向客户端发送命令的服务器,因此请考虑在客户端使用TIdCmdTCPClient而不是TIdTCPClient That will provide you with a dedicated thread to receive the server commands, and you can create OnCommand handlers for your commands and use the provided TIdCommand objects to send replies. 这将为您提供一个专用线程来接收服务器命令,并且您可以为命令创建OnCommand处理程序,并使用提供的TIdCommand对象发送答复。

Consider using the TIdTCPConnection.SendCmd() method to send commands and read their initial responses, and TIdTCPConnection.GetResponse() to read the final responses. 考虑使用TIdTCPConnection.SendCmd()方法发送命令并读取其初始响应,并使用TIdTCPConnection.GetResponse()读取最终响应。

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

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