Delphi 使用 Indy 10 - TCP 通信的最佳方法
Delphi with Indy 10 - Best approach for TCP communication
我正在使用 Delphi XE3 和 Indy 10(idTCPServer 和 idTCPClient)创建客户端-服务器应用程序。
服务器端将显示所有已连接的客户端,我可以 select 列表中的一些客户端并向它们发送命令或 streams/files。为此,我根据 Remy Lebeau 先生的建议创建了一个消息队列。这是我正在做的事情:
我想知道的是:
这对我想做的事情来说是个好方法吗?
当一侧开始 read/writing 时,它期望另一侧开始 write/read?对方不行怎么办?假设 SERVER 请求一个文件,但它不存在,CLIENT 必须写一个 "empty" 流来避免问题吗?
顺便说一句,我找不到任何关于此(Indy 10 TCP 通信)、使用队列、错误处理等的好例子。在 Indy 的网站上有许多损坏的链接。你能给我一个有很好例子的网站吗?
感谢您的帮助!
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?
让客户端在传输文件之前发送回复 accepting/rejecting 请求。还让接收方在传输完成后发送回复,以便发送方知道接收方是否收到了整个文件。
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
话虽如此,由于您的服务器是向客户端发送命令的服务器,请考虑在客户端使用 TIdCmdTCPClient
而不是 TIdTCPClient
。这将为您提供一个专用线程来接收服务器命令,您可以为您的命令创建 OnCommand
处理程序并使用提供的 TIdCommand
对象发送回复。
考虑使用 TIdTCPConnection.SendCmd()
方法发送命令并读取其初始响应,并使用 TIdTCPConnection.GetResponse()
读取最终响应。
我正在使用 Delphi XE3 和 Indy 10(idTCPServer 和 idTCPClient)创建客户端-服务器应用程序。
服务器端将显示所有已连接的客户端,我可以 select 列表中的一些客户端并向它们发送命令或 streams/files。为此,我根据 Remy Lebeau 先生的建议创建了一个消息队列。这是我正在做的事情:
我想知道的是:
这对我想做的事情来说是个好方法吗?
当一侧开始 read/writing 时,它期望另一侧开始 write/read?对方不行怎么办?假设 SERVER 请求一个文件,但它不存在,CLIENT 必须写一个 "empty" 流来避免问题吗?
顺便说一句,我找不到任何关于此(Indy 10 TCP 通信)、使用队列、错误处理等的好例子。在 Indy 的网站上有许多损坏的链接。你能给我一个有很好例子的网站吗?
感谢您的帮助!
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?
让客户端在传输文件之前发送回复 accepting/rejecting 请求。还让接收方在传输完成后发送回复,以便发送方知道接收方是否收到了整个文件。
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
话虽如此,由于您的服务器是向客户端发送命令的服务器,请考虑在客户端使用 TIdCmdTCPClient
而不是 TIdTCPClient
。这将为您提供一个专用线程来接收服务器命令,您可以为您的命令创建 OnCommand
处理程序并使用提供的 TIdCommand
对象发送回复。
考虑使用 TIdTCPConnection.SendCmd()
方法发送命令并读取其初始响应,并使用 TIdTCPConnection.GetResponse()
读取最终响应。