Delphi Indy TCP客户端/服务器通信最佳方法 [英] Delphi Indy TCP Client/Server communication best approach

查看:412
本文介绍了Delphi Indy TCP客户端/服务器通信最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端和一个可以正常通信的服务器应用程序,服务器中有一个TIdCmdTCPServer,客户端中有一个TIdTCPClient.

客户端必须在服务器中进行身份验证,客户端向服务器询问最新版本信息,并下载所有更新和其他通信.与TIdTCPClient.SendCmd()和TIdTCPClient.LastCmdResult.Text.Text的所有通信.

这样,服务器接收命令和答复,客户端仅接收答复,从不接收命令,我想实现一种使客户机接收命令的方法.但据我所知,如果客户端使用SendCmd,则它永远不应监听ReadLn()之类的数据,因为它会干扰SendCmd中预期的答复. 我想到要制作一条命令来检查命令,例如,客户端将发送"IsThereCommandForMe"之类的命令,而服务器将向每个客户端发送命令池,并且当客户端提出要求时,服务器会在答复中发送该命令,但是我认为这不是一个好方法,因为在可用命令和客户端要求之间会有很大的延迟.我还考虑过与新组件建立新的连接,例如TIdCmdTcpClient,但是每个客户端会有2个连接,我不喜欢这种想法,因为我认为它很容易在通信中引起问题.

我想要这个的原因是,我想在客户端中实现聊天功能,并且它应该一直从服务器接收消息,而无需一直询问它,想象所有客户端不断询问服务器是否有消息为他们.我希望能够在有可用更新时通知客户端,而不是客户端询问是否有更新.这样,我也可以向客户端发送更多命令.

您对此有何看法?我如何使服务器从客户端接收命令,但同时发送命令呢?

解决方案

TCP套接字在设计上是双向的.建立客户端"和服务器"之间的连接后,它们是对称的,并且可以随时通过同一套接字从任何一侧发送数据.

仅取决于所使用的通信模型的协议(通信仅被写为合同").例如,HTTP使用请求/回复模型.例如,使用Telnet,双方都可以启动数据传输. (如果您查看Telnet的Indy实现,您会发现它使用后台线程来侦听服务器数据,但是它在主线程中使用相同的套接字连接来将数据从客户端发送到服务器).

支持请求/响应和服务器推送并且对防火墙友好的全双工"协议是

如果使用TIdTCPClient/TIdCmdTCPServer,则企业防火墙可能会阻止通信.

I have a client and a server application that is communicating just fine, there is a TIdCmdTCPServer in the server and a TIdTCPClient in the client.

The client has to authenticate in the server, the client asks the server for the newest version information and downloads any updates, and other communications. All this communication with TIdTCPClient.SendCmd() and TIdTCPClient.LastCmdResult.Text.Text.

The way it is, the server receives commands and replies, the clients only receives replies, never commands, and I would like to implement a way to make the client receives commands. But as I heard, if the client uses SendCmd it should never be listening for data like ReadLn() as it would interfere with the reply expected in SendCmd. I thought of making a command to check for commands, for example, the client would send a command like "IsThereCommandForMe" and the server would have a pool of commands to each client and when the client asks, the server send it in the reply, but I think it would not be a good approach as there would be a big delay between the commands being available and the client asking for it. I also thought of making a new connection with new components, for example a TIdCmdTcpClient, but then there would be 2 connections for each client, I don't like that idea as I think it could easily give problems in the communication.

The reason I want this, is that I want to implement a chat functionality in the client, and it should be receiving messages from the server without asking for it all the time, imagine all clients continually asking the server if there is message for them. And I would like to be able to inform the client when there is an update available instead the client being asking if there is any. And with this I could send more commands to the client too.

what are your thoughts about this ? how can I make the server receiving commands from the clients, but also sends them ?

解决方案

TCP sockets are bidirectional by design. Once the connection between 'client' and 'server' has been established, they are symmetric and data can be sent at any time from any side over the same socket.

It only depends on the protocol (which is just written 'contract' for the communication) which communication model is used. HTTP for example uses a request/reply model. With Telnet for example, both sides can initate data transmissions. (If you take a look at the Indy implementation for Telnet, you will see that it uses a background thread to listen for server data, but it uses the same socket connection in the main thread to send data from client to server).

A "full duplex" protocol which supports both request/response and server push, and also is firewall-friendly, is WebSockets. With WebSockets (a HTTP upgrade), the server can send data to the connected client(s) any time. This would meet your 'chat' requirement.

If you use TIdTCPClient / TIdCmdTCPServer, corporate firewalls might block the communication.

这篇关于Delphi Indy TCP客户端/服务器通信最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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